This shell program mainly demonstrate how to use $? to find out the exit status of command.
After a command executes, if you input to terminal: echo $?, you will get 0 or 1 to indicate the status of execution.
The following is a simple program with comments.
1. accept a string as name from command
2. use delimiter “:” to separate each line in etc/passwd file.
3. find if name you input is there
4. output a string.
echo -n "Enter a string: " read USR cut -d: -f1 /etc/passwd | grep "$USR" > /dev/null #On UNIX, this is a virtual-file that can be written to. #Data written to this file gets discarded. OUT=$? if [ $OUT -eq 0 ];then echo "Cool! The name you input is found!" else echo "The name you input doesn't exist in /etc/passwd file!" fi |