Advertisement
anonopsbelgium

Shells and Shell Scripts intro

Feb 10th, 2012
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.95 KB | None | 0 0
  1. Shells and Shell Scripts
  2.  
  3. A Unix shell is a command language interpreter, the primary purpose of which is to translate command lines typed at a terminal into system actions. The shell itself is a program through which other programs are invoked. Although there are several different Unix shells, among them the C shell (csh), the Bourne shell and the Korn shell, the one most frequently used on Blake within Berkeley UNIX is the C shell.
  4.  
  5. The shell has some built-in functions, which it performs directly, but most commands that you enter cause the shell to execute programs that are external to the shell. This sets the shell apart from other command interpreters, because it is just another user program at the same time that it functions almost exclusively as a mechanism for invoking other programs.
  6.  
  7. For more information about the C shell, refer to the article ``An Introduction to the C Shell,'' by William Joy, in the Unix User's Manual Supplementary Documents volume. Also see `man csh' online.
  8. What is a Script?
  9.  
  10. One handy thing you can do with the shell is to use standard UNIX commands as building blocks to create your own new commands. To do this, you write a `shell script', which can contain a number of commands, and then the file can be executed as one command.
  11.  
  12. Following is an example of a very simple shell script, which gives a brief indication of the types of things you might do. To use this feature to its maximum potential, however, you will need to refer to the documentation mentioned above, or other Unix manuals.
  13.  
  14. Suppose your name is joanx and occasionally you want the system to tell you your user id, what directory you are in, list the files in that directory, and display the date. You want each of these pieces of information to be preceded by an appropriate statement. The individual Unix commands you need are `echo,' `who am i,' `pwd,' `ls,' and `date.' So you would create a a regular text file (let's call it `me') with the following lines in it:
  15.  
  16. echo you are
  17. who am i
  18. echo The directory you are in is
  19. pwd
  20. echo The files in this directory are
  21. ls
  22. echo The date is
  23. date
  24.  
  25. You (joanx) can execute the file called me, when you are in the same directory it is in, simply by entering
  26.  
  27. sh me
  28.  
  29. When you do this, the following information might be displayed on your screen:
  30.  
  31. you are
  32. joanx ttyB7 Jun 9 14:51
  33. The directory you are in is
  34. /b2/joanx/mine
  35. The files in this directory are
  36. herfile hisfile myfile ourfile
  37. The date is
  38. Fri Jun 9 16:10:50 PDT 1989
  39.  
  40. Once you learn to create and use them, you should see that shell scripts are useful Unix tools that might save you a lot of time in your day-to-day computing.
  41. More on Scripts
  42.  
  43. Virtually any command which can be executed at the unix prompt can be executed within a shell script.
  44. Structure
  45.  
  46. Each line of the script file is a single command. There are two exceptions to this statement. The first is when a line ends with a backslash "\\", since the backslash removes the significance of the newline character as a command terminator. Therefore, a backslash at the end of a line is the indication of command continuation on the following line, just as it is at the prompt.
  47.  
  48. The second exception is when the "<<" I/O redirection symbol is used in a script file. This symbol will be discussed below.
  49. Comments
  50.  
  51. Comments in C shell scripts begin at a pound sign "#" and continue to the end of the line. Since most Unix system support several shells, it is recommended that each shell script start with a comment indicating which shell the script was written for.
  52. Output
  53.  
  54. Text can be sent to standard output from within a shell script by using the c-shell command 'echo'. This command will print the value of its arguments on standard output appending a newline. The -n option to echo will suppress the newline at the end of the text.
  55. Examples
  56.  
  57. The following script will print out the name and contents of the current working directory.
  58.  
  59. # print the name and contents of the current
  60. # working directory
  61. echo "This is from a shell script:"
  62. pwd # print the directory name
  63. ls # print the directory contents
  64.  
  65. The following script will edit a C source code file named "count-c", compile and link it producing an executable version named "count", and finally run count.
  66.  
  67. # edit compile link, and run
  68. vi count.c
  69. cc count.c -o count
  70. count
  71.  
  72. Running Scripts
  73.  
  74. There are three ways to execute a c-shell script.
  75. Direct Execution
  76.  
  77. The simplest way to execute a shell script is to used chmod to make the text file executable. The script can then be executed by typing its name if it is in the current working directory or is in the path for that shell.
  78.  
  79. Executing a script creates a new shell and executes each command in the script within the new shell. When the end of the script file is encountered, the new shell exits. Any changes in the new shell caused by the script are lost when the shell exits.
  80.  
  81. For example, if the file
  82. /usr/samples/script/change contains...
  83.  
  84. cd/usr/games
  85. pwd
  86. fortune
  87.  
  88. Then the command sequence would yield the following output:
  89.  
  90. unix> cd /usr/samples/script
  91. unix> pwd
  92. /usr/samples/script
  93. unix> chmod +x change
  94. unix> change
  95. /usr/games
  96. To err is human. To forgive, divine.
  97. unix> pwd
  98. /usr/sample/script
  99.  
  100. csh Execution
  101.  
  102. Another way to execute a script within the context of a new shell is to run csh and give it the name of the script file as its first argument. This is, in fact, how direct execution is implemented. By executing csh directly, the default csh options can be modified. Then the command sequence would yield the following output:(user input is in bold) (the -x option causes echo to be set, i.e each command will be echoed before it is executed)
  103.  
  104. unix> cd /usr/samples/script
  105. unix> pwd
  106. /usr/samples/script
  107. unix> csh -x change
  108. cd /usr/games
  109. pwd
  110. /usr/games
  111. fortune
  112. To err is human. To forgive, divine.
  113. unix> pwd
  114. /usr/samples/script
  115.  
  116. source Execution
  117.  
  118. The third way to execute a script will execute it within the context of the current c-shell, using the c-shell command source and giving it the name of the script file as its first argument. Since execution takes place within the context of the current shell, any changes in the shell are retained following the completion of the shell. Then the command sequence would yield the following output:
  119.  
  120. unix> cd /usr/samples/script
  121. unix> pwd
  122. /usr/samples/script
  123. unix> source change
  124. /usr/games
  125. To err is human. To forgive, divine
  126. unix> pwd
  127. /usr/games
  128.  
  129. Non-script Execution
  130.  
  131. There are several ways to execute commands within shells or at the command prompt.
  132. Sequential Commands
  133.  
  134. A series of independent commands can be grouped together as a single command by separating them by semicolons ";".
  135. Pipes
  136.  
  137. Pipes "|" also cause the execution of multiple processes from a single command line. With a pipe, the standard output of the process before the pipe is redirected to the standard input of the process after the pipe. This is also covered below, in I/O Redirection.
  138.  
  139. unix> /fortune | tee temp ; wc temp ; rm temp
  140. Save the whales. Collect the whole set.
  141. 1 7 41
  142.  
  143. Inline Execution
  144.  
  145. Grave accents "`" (on the same key as the tilde "~") will force inline execution of the enclosed command. Like the quotation marks "''" and the apostrophes "'", grave accents must be used in pairs. Grave accents are used where a command "is not expected".
  146.  
  147. unix> set code = ( `ls *.c`)
  148.  
  149. Subshells
  150.  
  151. Any commands enclosed within parentheses are run in a separate shell, similar to the "direct" or "csh" execution of a shell script. As you can see, technically, this is not a means of running a shell script at all, but it is similar. The following sequence of commands illustrates this. Recall the use of the semicolon to separate sequential commands.
  152.  
  153. unix> cd /usr/samples
  154. unix> pwd
  155. /usr/samples
  156. unix> (cd ; pwd)
  157. /users/unix0
  158. unix> pwd
  159. /usr/samples
  160. unix> cd ; pwd
  161. /users/unix0
  162. unix> pwd
  163. /users/unix0
  164.  
  165. Deferred Execution
  166.  
  167. There are times when variable substitution must occur before command execution. The c-shell command eval causes execution to be deferred until after variable substitution. Variable substitution is discussed in greater detail in the next section.
  168. Redirection
  169.  
  170. Input and output redirection is available within shell scripts.
  171. Input and Output Redirection
  172.  
  173. Input redirection will read from the named file instead of from the terminal. Input redirection uses the symbol "<". Output redirection uses the symbol ">" to create the named file to contain the output of the process. Output redirection can clobber (overwrite) an existing file. Output will be appended to the named file when the output redirection symbol ">>" is used.
  174. Pipes and Filters
  175.  
  176. Piping connects the output of one process to the input of a second. Piping uses the symbol "|". A filter is a process which is between two pipes. It simply changes the information coming down the pipe.
  177.  
  178. Standard input for a process can be drawn from the script file itself. This uses the special redirection symbol "<<". The name which follows the redirection symbol is a tag for the end of the input. That is, the contents of the script file will be read as standard input to the process until a input line matching the name following the redirection symbol.
  179.  
  180. If the file /usr/samples/script/redirect contains:
  181.  
  182. cd
  183. echo "Here are some commands:"
  184. sort << end > temp
  185. pwd
  186. ls
  187. clear
  188. passwd
  189. end
  190. cat temp
  191.  
  192. The following is an example of using the 'redirect' script:
  193.  
  194. unix> cd /usr/samples/script
  195. unix> redirect
  196. Here are some commands:
  197. clear
  198. ls
  199. passwd
  200. pwd
  201. unix> pwd
  202. /usr/samples/script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement