Creating new commands - csh allows you to specify aliases alias h history alias w 'who|sort' alias j jobs -l alias ll 'ls -l' - Bourne shell and all the other shells can use shell scripts to create new commands - Shell scripts are simple text files containing a series of shell commands $ sh nu 7 $ chmod u+x nu $ nu 7 $ cat nu who | wc -l - Specify parameters to the command that we have created - $1 corresponds to the first parameter - $2 corresponds to the second parameter, and so on - If you wanted complete list of parameters, use $* - $0 is the name of the program itself - You can specify the output from a command in your echo by enclosing them in backward single quote ` $ echo Today is `date` Today is Wed Jan 24 19:43:56 EST 1996 $ - In additional to the positional parameters such as $1, $2, $3, etc., we can have other variables - Shell uses variables such as PATH, MAIL, etc. (covered earlier) - We can even define our own variables (USERNAME in assignment #1) - The variables set in a shell script are destroyed when the script finishes execution - The reason is that the shell script starts a subshell and supplies the commands from the shell scripts to this subshell $ cat useless hmmm=hello echo $hmmm $ sh useless hello $ echo $hmmm $ - . command allows you to retain the values of the variables defined in a subshell $ chmod u+x useless $ . useless hello $ echo $hmmm hello - The variables defined in a shell are not available to its subshells $ USERNAME='Pawan Lingras' $ echo $USERNAME Pawan Lingras $ echo 'echo $USERNAME' > testy $ sh testy - The export command makes the variable available to a subshell $ export USERNAME $ sh testy Pawan Lingras $ - Iteration General format is for in do done - Example for i in * do cat $i done # while true do for search in $* do if date | egrep $search then while who | egrep lingras do hi=`tty lingras` echo 'This is ' $search 'message: ' $* > $hi sleep 10 done exit fi done sleep 20 done - The above program has an outer while loop which runs forever - Inner for loop repeats the body for every parameter supplied to the script - Inside the for loop we check to see if the variable search appears in the output of the date command - The inner most while loop starts executing when a match is found - gets the terminal on which the user lingras is working and stores it in a variable called hi - echoes a message onto the terminal stored in $hi - When the innermost while is done, the program hits the exit statement and exits out of the script - Your first script is supposed to loop indefinitely - if someone is not working on tty11 or tty12 - echo a message "Can slip in now" - else echo a message "Cannot slip in now" - if who | grep tty11 then if who | grep tty12 then echo "Cannot slip in now" else echo "Can slip in now" fi else echo "Can slip in now" fi - The previous algorithm is probably not the best logic try to find a better algorithm using &&, ! or || - The second script is to be called del - It accepts parameters and for every file in the list of parameters show the top few lines bottom few lines ask the user if she wants to delete the file if the answer is yes delete the file for i in $* do head -5 $i echo 'DO you want to delete ' $i '(y/n)\c' read answer # if ..... look at page 141 to see how to setup the condition done - read command reads input from the user and stores in the variable that follows the word read read answer stores the input in the variable answer