Parameters passed into a script from the command line:
$ cat t echo $1 $2 echo $3 echo $4Suppose we have a set of shell commands in a file. In this example $1 through $4 refer to the first through fourth parameters to be passed into the file from the command line. $ chmod +x t $ ls -l t -rwxr-xr-x 1 ddailey 27 Nov 18 16:37 tWe set the file as executable.... $ ./t one two happy sad one two happy sadwe can then run the commands in the file
Something a bit more advanced (and useful):
$ cat te w=/usr/dict/words echo Welcome, $1 to a simple program. a=`grep -c $2 $w` b=`grep $2 $w|head -1` c=`grep $2 $w|tail -1` echo The second word you\'ve used: $2 echo appears as a substring of $a words echo in $w, including in $b and $cWe first count the occurrences of the string $2 in a word list and then find two examples. The output has been captured into variables $a, $b, and $c for storage until it is time to format output. $ ./te geronimo hi there Hello, and welcome, geronimo to a simple program. The second word you've used: hi appears as a substring of 1104 words in /usr/dict/words, including in abashing and YorkshireA running of the program with three parameters. The third one is ignored. $ ./te fred tez Hello, and welcome, fred to a simple program. The second word you've used: tez appears as a substring of 1 words in /usr/dict/words, including in Cortez and CortezThis run of the program suggests that we may wish to examine the value of $c and differently structure output based on the result.
Handing cases based on the number of findings:
$ cat tef w=/usr/dict/words echo Hello, and welcome, $1 to a simple program. a=`grep -c $2 $w` b=`grep $2 $w|head -1` c=`grep $2 $w|tail -1` echo The second word you\'ve used: $2 if [ $a = 0 ] then echo appears as a substring of no words in $w fi if [ $a = 1 ] then echo appears as a substring of one word: $b fi if [ $a -gt 1 ] then echo appears as a substring of $a words echo in $w, including in $b and $c fiWe provide different output depending on the number of lines matching the regular expression. $ ./tef fred teq Hello, and welcome, fred to a simple program. The second word you've used: teq appears as a substring of no words in /usr/dict/wordsAn example with $a = 0. $ ./tef fred tez Hello, and welcome, fred to a simple program. The second word you've used: tez appears as a substring of one word: CortezAn example with $a = 1. $ ./tef fred teu Hello, and welcome, fred to a simple program. The second word you've used: teu appears as a substring of 9 words in /usr/dict/words, including in amateur and SteubenAn example with $a -gt 1.
And for a more advanced treatment of parameters... look here.