
SHELL PROGRAMMING GODFREY C. MUGANDA 1. Introduction A Unix shell is a program that interprets user's commands typed at the keyboard. Unix systems come with thousands of commands in the form of programs. These programs implement all kinds of functionality and can be invoked at the shell prompt. A Unix shell functions as a command intepreter, executing these programs at the behest of the user. In addition to executing commands typed by users, Unix shells support a program- ming language with decision making and looping control structures. Instead of typing commands at the keyboard, you can edit a sequence of commands into a file called a shell script. Your shell script can use sequencing, decision making, and looping control structures to tie together the commands available and perform useful tasks. There are several types of shell programs available for Unix systems. Here we cover bash, the Bourne-Again Shell. This is the most popular shell on Unix systems. 2. Some Unix commands Let's cover a few of the many Unix commands available. You can simply type these at the shell prompt in a terminal to see what they do, or you can use the man pages to learn more about them. (1) who (2) cal (3) whoami (4) date (5) cat (6) grep (7) mv (8) printf (9) echo (10) ls (11) uname (12) wc Notice that information for these commands can be found in section 1 of the man pages. Unix manual pages are divided into 8 sections: the first few are 1 Commands available to users 2 Unix and C system calls 3 C library routines for C programs 1 2 GODFREY C. MUGANDA So for example, printf is both a user command and a C library function. The command man 1 printf returns information about the user command, while man 3 printf returns information about the printf library function. 3. Trying Out the echo command Here is the information on echo returned by the man pages. NAME echo - display a line of text SYNOPSIS echo [SHORT-OPTION]... [STRING]... echo LONG-OPTION DESCRIPTION Echo the STRING(s) to standard output. -n do not output the trailing newline -e enable interpretation of backslash escapes -E disable interpretation of backslash escapes (default) If -e is in effect, the following sequences are recognized: \\ backslash \a alert (BEL) \b backspace \c produce no further output \e escape \f form feed \n new line Basically, the echo command outputs a line of text. The command takes a list of 0 or more arguments, where the arguments are separated by spaces. Each argument is printed to standard output, with spaces being used to separate the arguments. If an argument is a string containing a space, you can put quotes around it to have echo treat it as a single argument. This practice of putting quotes around a single argument to echo is widespread. It is routinely used even when it is not needed. Try the following commands echo echo hello world echo hello world echo "hello world" echo -e "my \bmother" echo "get\nout" echo -e "get\nout" SHELL PROGRAMMING 3 4. Hello World Let us do the obligatory \Hello World!" in a Shell program. Start your favourite text editor, say gedit, and use is to create a text file named hello.sh. The .sh extension is not necessary, but it is conventional to use it for a shell script. gedit hello.sh Edit a single line into the file echo "Hello World!" save the file, and exit the editor. Before we can run the program, we must make its file executable. Type the com- mand chmod a+x hello.sh This command changes the file mode of hello.sh to grant eXecute permissin to All. Now you can execute the file with the command. ./hello.sh That is it: we have now written our first shell script. 5. Variables and Assignment The shell does not require variables to be declared. The variable is created the first time it is used. To assign a value to a variable, use the assignment operator = with no spaces around it. To access the value in a variable, prefix the variable with a $. Try these commands at the shell prompt: greeting="Hello out there!" echo greeting echo $greeting number=12 echo number echo $number number1=$number echo $number1 echo "$number1" echo "$number days of Christmas" Notice how the value in a variable is evaluated even when it is part of a string. It is not uncommon for shell variables to be enclosed in f g when being evaluated. For example, greeting="Hello out there!" echo greeting echo ${greeting} Enclosing with f g is optional, and supposedly, aids readability. 4 GODFREY C. MUGANDA 6. Reading Input You typically use echo or printf to do output in a shell script. To input values and store them in a variable, Use read. Use your text editor to create a file input.sh with the following contents echo -n "Enter a number: " read x echo "You entered $x" echo -n "Enter a string: " read str echo "You entered $str" Make it executable and run it to see how it works. 7. Comments Comments start with # and end at the end of line. You can find many examples of comments in shell scripts online, or in any book on Shell programming. 8. Command Substitution The string that a program outputs to its standard output can be captured and used in a Shell program. To capture the ouput of a command, enclose the invocation of the command in backticks. The backtick is the backward-leaning single quote that occupies the same key as the tilde at the top left of your keyboard. For example, try typing d=`date` echo "The day and time is $d" at the command line. You will see that the output of the date command is captured, assigned to the variable d, and then output as part of the argument to echo. This mechanism is called command substitution, or sometimes, command expansion. There are two alternative forms (syntax) for command substitution. `command` and $(command) The shell performs the expansion by executing COMMAND and replacing the com- mand substitution with the standard output of the command, with any trailing newlines deleted. Thus echo `date` and echo $(date) SHELL PROGRAMMING 5 do the same thing. Exercise: Use the uname command to print the name of the operating system, name of the network host, and the the name of the CPU processor that the shell script is running on. 9. Arithmetic Expansion There are several ways to evaluate arithmetic expressions. Let us try something that we would expect to work. echo 2+3 d=17+2 echo d echo $d When you try this, you quickly realize that the Shell does not evaluate arithmetic expressions written like this. This is because evaluation of arithmetic expressions requires a mechanism called arithmetic expansion. The syntax of arithmetic is $((expression)) Try echo $((2+3)) d=17+2 echo $((d)) echo $((d+3)) You should get the expected results. Nearly all the operators you have come to know and love from C and C-derived languages are supported by the shell. For example echo $((1 ? 23 : 24)) is an example of the conditional operator ? :. The above command will ouput 23. If you use bash, you can also use the square brackets syntax $[expression] in place of $((expression)) 10. The expr command The expression command evaluates its arguments and writes the result to standard output. This is different from expression expansion, which yields the value of an expression but does not write it to standard output. The arguments of the expr command must be separated by whitespace. To learn more about expr, use the command man expr Here are examples of using the command expression 6 GODFREY C. MUGANDA expr 2 + 3 expr 2 \* 3 These two commands yield 5 and 6, the sum and product of 2 and 3. The * operator normally stands for wildcard; this usage has to be escaped. Notice the spaces around the arguments to expr. Here are more examples expr 2 + 2 = 4 + 1 expr 2 + 3 = 3 + 1 expr 2 != 4 These commands process boolean expression and output 1 for true and 0 for false. Note from the man pages that expr can operate on strings and regular expressions expr length "I love linux" expr index hello l The first of the above prints the number of characters in the string I love linux whereas the second prints the position that the substring l is found in the string hello. Note that positions of characters in strings are 1-based rather than 0-based. A return value of 0 for the index means the searched-for substring was not found. 11. Using pipes You can form pipelines of processes in the shell in a way similar to how you can use pipes of processes on the command line. You use the j symbol to form a pipeline between two processes. For example, the command wc -w will count the number of words in the file used as its standard input. The command cat file will send the contents of the file passed on the command line to standard input. For example, to count the number of words in the file, just write cat file | wc -w The standard output of the cat command is piped into the standard input of the wc command. You can type the statement at the shell prompt, or put it in a shell script. The arithmetic facilities built into the shell are very weak. You can process integers, but not floats.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages15 Page
-
File Size-