Write a program called valid that prints yes if its argument is a legal shell variable name and no otherwise:

$ valid foo_bar
yes
$ valid 123
no


if [ $# -ne 1 ] ; then
echo "Error: no variable name to test"
exit 1
fi

if [ -n "$(echo $1 | grep -E '^[a-zA-Z][a-zA-Z0-9_]{0,}$')" ] ; then
echo "yes"
else
echo "no"
fi

Note this works because the regular expression defines the valid language of a shell variable as an upper or lower case letter followed by zero or more alphanumeric values or an underscore. To work properly note you also need to use ^ to left root the expression to the beginning of the word and use $ to get the end of the expression, making sure the entire word given meets the regular expression's rules.

Computer Science & Information Technology

You might also like to view...

Answer the following statements true (T) or false (F)

1: The man command is issued to mange the computer resources. 2: Any user can change the system date and time using the date command. 3: Only the system administrator is authorized to change the system time. 4: Fields in the command line are separated by a space. 5: UNIX provides more than one shell and you can change your current shell to another available shell.

Computer Science & Information Technology

To manage the security information from the devices in a large network, you should establish a security incident _________________ team.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

A specific item of data or information located within a table is called a ________

A) field B) key C) report D) record

Computer Science & Information Technology

To display specific dates, you can use the DateSerial function, written as DateSerial(year,month,day) to manipulate the day, month, and year of a date

Indicate whether the statement is true or false

Computer Science & Information Technology