This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

script problems

 
johny doe
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
johny doe
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic