A shell script is a file with linux
commands. You enter the shell script file name at the Linux prompt to
execute the commands in the file. For example, if you create a file named
dateandwho (in vi or Pico) with these two
commands
date
who
then you have to make this shell script file (this script) file executable by
giRoomNog the chmod command
> chmod a+x dateandwho
and execute this file using its name as a command
> dateandwho
This file containing the linux commands is an example of a very simple shell
script that new command to show the date and then who was logged into the Linux
machine.
The Unix/Linux convention for naming a shell script is to use a file name
with no extension. This follows the convention of naming a program
executable file with no extension, for example, if the
hello.cpp program execuatble is compiled and linked by the commands
> c++ -c hello.cpp
> c++ -o hello hello.o
then the name of hte executable is just hello
without an extension. Linux determines the type of executable file from
the files directroy information. You can see this information ussing the
file command:
Shell scripts that do more complicated operations require a more complicated syntax. There are multiple shell languages. Each language has its own syntax for if statements, how assignments works, using variables, etc. We will use the bash shell.
All bash shell scripts must have the following on the first line of the file:
#!/bin/bash
Note that in bash shells, after the first line, all lines that begin with #
are comments. Hence, the following shell does nothing.
#!/bin/bash
# Steve Dannelly
# Lab 8
You will need to put your name on all your lab assignments.
Echo is a very common and useful command. Here is an example of a script with
a few echo commands
#!/bin/bash
# echo examples
echo "This is an example of using echo."
echo -n "The current directory is "
echo $PWD
The first echo command just prints the contents of the string inside the quotes.
The second echo also prints a string but does not add a return character. Note the second string ends in a space just so the output looks pretty.
The third echo prints the contents of a built-in variable that the system uses to keep track of your current directory (PWD is short for Print Working Directory).
Running the above script files would create the following output:
This is an example of using echo.
The current directory is /home/ACC.dannellys2/csci208lab
A few variables are already built into the system. To see the names and values of system variables, type "set" on the command line.
To create a new variable, just start using it. No need to declare it.
Your variable names must begin with a letter or the underscore character ( _ ).
The bash format for variable assignment is
var=contents
DO NOT put spaces around the = symbol.
This shell script
#!/bin/bash
# echo examples
name1=bob
name2=name1
name3=$name1
echo $name1
echo $name2
echo $name3
would create the following output
bob
name1
bob
The first assignment puts the string bob inside a new variable named name1.
The second assignment creates a new variable named name2 and puts inside it the string name1.
The third assignment creates a new variable named name3 and puts inside it the contents of the variable named name1.
Each of the echo commands prints the contents of a different variable.
Arguments to your shell script are placed into numbered variables. $1 is the first command line argument. $2 is the second. etc... $# is the number of arguments.
For example, shell3 is simple shell that I wrote to demonstrate how to use command line arguments.
> cat shell3 #!/bin/bash # arguments echo "There are " $# " arguments." echo "The first argument is " $1 echo "The name of this script is " $0 > shell3 hi mom There are 2 arguments. The first argument is hi The name of this script is ./shell3
The format for if statement is
if [ condition ]
then
true stuff
else
false stuff
fi
The "fi" is "if" backwards, and ends an if block.
The following example uses an if to make sure the user input an argument. If the user input no arguments, the script prints a message then exits. If the user input some arguments, then the script prints how many.
So long as the script did not exit due to too few arguments, the scripts runs a command to count the number of lines in the filename provided. Note, the single quotes are not the normal single quotes. These weird quotes cause the output of the command, not the contents of the string, to go into the variable named lines.
#!/bin/bash # an if statement # check for enough arguments if [ $# = 0 ] then echo "No Arguments" exit 1 else echo $# " Arguments" fi # count the number of lines lines=`cat $1 | wc -l` # output the results echo -n "The file named " echo -n $1 echo -n " contains " echo -n $lines echo " lines."That script would produce this output:
> shell4 No Arguments > shell4 shell4 1 Arguments The file named shell4 contains 21 lines.
Also, the user should provide the name of the file to process. If the user does not provide a file name, then print a usage error and quit.
For example
> majors Usage: majors filename > majors majorslist.txt ACCT 23 CSCI 2 CIFS 1 ENTR 5 MGMT 20 MKTG 22Be sure to test your script with input other than the file lab7.txt!!!!
Now add another feature to the previous script. Allow the user the option to view names. The option should be "-names". If the user includes that option then print the names as well as the graduation counts.
For example
> majors2 Usage: majors2 [-names] filename > majors2 lmajorslist.txt CSCI 2 CIFS 1 ACCT 23 ENTR 5 MGMT 20 MKTG 22 > majors2 -names majorslist.txt CSCI 2 Dickson, Anthony James BS CSCI MATH Downs, Christopher Paul BS CSCI BADM ************************************************************ CIFS 1 Anderson, Ian BS BADM CIFS ************************************************************ ACCT 23 Alexander, Amy Elizabeth BS BADM ACCT ACCT Ayers, Brittany Nicole BS BADM ACCT Brown, Lyeshea Semondre Shayron BS BADM ACCT Calloway, Logan Mackenzie BS BADM ACCT Childers, Jamie Leigh BS BADM ACCT Flick, Heather Wrenee BS BADM ACCT Foster, Rodney Tyrone BS BADM ACCT Gallman, Antonio James BS BADM ACCT blah blah blah blah