Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

i have finished making the scripts but..

 
johny doe
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but when i tried to run one of them i get
bad command error
regarding the first assigment
(1.Write a shell script program called valid that prints "yes" if the argument is a valid shell variable and "no" otherwise)

i wrote
=========
#!/bin/sh
if echo $1 | grep -q "[A-Za-z_][A-Za-z0-9_]*"
then
echo yes
else
echo no

=========
i saved the file as a.sh

i entered the directory in which the file is located and
wrote
a 123
i got a bad command
where i got it wrong

the second script :for the following assigment
Write a shell script program print_args that prints its arguments one per line and precedes each argument by its number.

i wrote:

#!/bin/sh
for i in $@; do
echo 1:$i
done

is it the coorect way??
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it looks like a school homework, you should probably test it yourself and see if you get the desired result. That way, you can learn more.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first script needs a 'fi' at the end - that way you close an if-statement.

Then it will start working, but it's not finished.

Grep will search inside $1 for a valid identifier.
123 no
aaa yes
a12 yes
1a1 yes <--- found: a1

You may use ^ (begin of expression) and $ (end of expression/line) to force the whole expression to match the pattern:
"^[A-Za-z_][A-Za-z0-9_]*$"

If the name of the script is a.sh, you have to call a.sh - not just a, because linux does not allow omitting the extension.
And if your current working directory is not in the path, you have to specify the path too:


./ is the current directory.

But probably your file is not executable.

Will CHange MODe for All users to be eXecutable.

To the second script: no. Try it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic