File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Linux / UNIX and the fly likes script problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "script problems" Watch "script problems" New topic
Author

script problems

johny doe
Ranch Hand

Joined: Dec 07, 2007
Posts: 78
1. Write a shell script program called valid that prints "yes" if the argument is a valid shell variable and "no" otherwise, for example:

->valid my_var
Yes
->valid 31pq_
No
a valid shell variable name begins with an alphabetic character (a-z and A-Z, or _), followed by zero or more alphanumeric (alphabetic and digits) or _ characters


i dont know how to do this argument test. in know that in the end
the script will look like this
i just dont know how to do the argument cheking

if $?=0
then
echo yes

else
echo no

i am not sure if this is the way to solve it


2. Write a shell script program print_args that prints its arguments one per line and precedes each argument by its number. It should look like this:

->print_args bb a xyz
1: bb
2: a
3: xyz

i know how to do this thing is it were given in the loop
again the problem whith the argument
i solved a similar problem like this

for i in a b c d e

do
echo i=$i
done

but here it is a constant value how do i change it into a argument setting??
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

For Question 2 look here:
http://tldp.org/LDP/abs/html/internalvariables.html#ARGLIST

For Question 1: Do you know regulare expressions?
might help.
Here is a brief introduction:
http://tldp.org/LDP/abs/html/x15576.html
Here is nearly a solution:
http://tldp.org/LDP/abs/html/testbranch.html#MATCHSTRING


http://home.arcor.de/hirnstrom/bewerbung
johny doe
Ranch Hand

Joined: Dec 07, 2007
Posts: 78
regarding problem 1. i found out that in order to get the variable name
i need to build "if" line on $1
but how do i check that $1
begins with an alphabetic character (a-z and A-Z, or _), followed by zero or more alphanumeric (alphabetic and digits) or _ characters??

regarding problem 2. there may be lots of words i cant predict how many
echo $1
echo $2
etc..

how do i solve it??
johny doe
Ranch Hand

Joined: Dec 07, 2007
Posts: 78
i also got this tips from other sources
if anbod can explain them further

for question 1:

Compose a regular expression that matches a valid variable name, then use grep and check its exit status

for question 2:

use for loop and $* variable to print all the script's arguments; to calculate the arguments number, use expr in back quotes (` �`)

i am realy new to this stuff i dont know how to build this
please help
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Well that looks like a homework assignment, and you should do your homework on your own.

Read books.
Go through tutorials.

We may help clarifying special questions, but we don't solve other peoples homework.

You're just repeating the assignment so far.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: script problems