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.
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: