Cis 208 lecture notes



Yüklə 37,76 Kb.
tarix08.10.2017
ölçüsü37,76 Kb.
#3712

CIS 208 LECTURE NOTES

Introduction to Shell Programming





  • A shell provides many mechanisms to customize your work environment.

  • A shell is more than a command interpreter--it is also a powerful programming language.

  • Different shells use different syntaxes when executing shell scripts. For example, Tcsh uses a C-like syntax, while Bourne shells use another type of syntax. This document is written for the bash shell.

Shell scripts.


  • A file that contains commands to be executed by the shell.

  • The commands can be any command that may be entered at a shell prompt or may be any of the control flow commands, designed specifically for use in shell scripts.

  • Control flow commands enable you to alter the order of execution of commands in a script.

Let's say that you use a series of commands often and would like to save time by grouping all of them together into a single ``command''. For example, the commands

date

who


Rather than type these commands, you can group them into a shell script. The shell script used to run all these commands might look like this:

#!/bin/sh

# A shell script to log current users

date


echo Users Currently Logged In

who


Shell scripts are just plain text files; you can create them with an editor such as emacs, pico or vi.

The first line, ``#!/bin/sh'', identifies the file as a shell script and tells the shell how to execute the script. It instructs the shell to pass the script to /bin/sh for execution, where /bin/sh is the shell program itself. Why is this important? On most Linux systems, /bin/sh is a Bourne-type shell, like bash. By forcing the shell script to run using /bin/sh, you ensure that the script will run under a Bourne-syntax shell (rather than a C shell). This will cause your script to run using the Bourne syntax even if you use tcsh (or another C shell) as your login shell.

  The second line is a comment. Comments begin with the character ``#'' and continue to the end of the line. Comments are ignored by the shell--they are commonly used to identify the shell script to the programmer and make the script easier to understand.

The rest of the lines in the script are just commands, as you would type them to the shell directly. In effect, the shell reads each line of the script and runs that line as if you had typed it at the shell prompt.

 


  • Permissions are important for shell scripts.

  • If you create a shell script, make sure that you have execute permission on the script in order to run it. When you create text files, the default permissions usually don't include execute permission, and you must set them explicitly.

  • you could use the command

/home/larry# chmod u+x whoson
to give yourself execute permission for the shell script whoson.

  • you run the script by simply typing its filename on the command line.

/home/larry# whoson

Shell variables and the environment.


  • A shell lets you define variables, as do most programming languages.

  • A variable is just a piece of data that is given a name.

  • When you assign a value to a variable (using the ``='' operator), you can access the variable by prepending a ``$'' to the variable name, as demonstrated below.

/home/larry# foo="hello there"
The variable foo is given the value hello there. You can then refer to this value by the variable name prefixed with a ``$'' character. For example, the command

/home/larry# echo $foo

hello there
produces the same results as

/home/larry# echo "hello there"

hello there


  • These variables are internal to the shell, which means that only the shell can access them. This can be useful in shell scripts; if you need to keep track of a filename, for example, you can store it in a variable, as above.

  • Using the set command displays a list of all defined shell variables.

  • However, the shell lets you export variables to the environment. The environment is the set of variables that are accessible by all commands that you execute. Once you define a variable inside the shell, exporting it makes the variable part of the environment as well. Use the export command to export a variable to the environment.

  • The environment is very important to the UNIX system. It lets you configure certain commands just by setting variables which the commands know about.

  • Here's a quick example. The environment variable PAGER is used by the man command and it specifies the command to use to display manual pages one screenful at a time. If you set PAGER to the name of a command, it uses that command to display the man pages, instead of more (which is the default).

Set PAGER to ``cat''. This causes output from man to be displayed to the screen all at once, without pausing between pages.

/home/larry# PAGER=cat

Now, export PAGER to the environment.

/home/larry# export PAGER

Try the command man ls. The man page should fly past your screen without pausing for you.

Now, if we set PAGER to ``more'', the more command is used to display the man page.

/home/larry# PAGER=more

Note that we don't have to use the export command after we change the value of PAGER. We only need to export a variable once; any changes made to it thereafter will automatically be propagated to the environment.



  • It is often necessary to quote strings in order to prevent the shell from treating various characters as special. For example, you need to quote a string in order to prevent the shell from interpreting the special meaning of characters such as ``*'', ``?'' or a space. There are many other characters that may need to be protected from interpretation.

  • The manual pages for a particular command tell you if the command uses any environment variables. For example, the man man page explains that PAGER is used to specify the pager command.

  • Some commands share environment variables. For example, many commands use the EDITOR environment variable to specify the default editor to use when one is needed.

  • The environment is also used to keep track of important information about your login session. An example is the HOME environment variable, which contains the name of your home directory.

  • Another interesting environment variable is PS1, which defines the main shell prompt. For example,

/home/larry# PS1="Your command, please: "

Your command, please:

To set the prompt back (which contains the user_name@\home:\current working directory followed by a ``$'' symbol),

/home/larry# PS1='\u@\h:\w\$ '

The bash manual page describes the syntax used for setting the prompt.

The PATH environment variable.


When you use the ls command, how does the shell find the ls executable itself? In fact, ls is in /bin on most systems.

  • The shell uses the environment variable PATH to locate executable files for commands you type.

For example, your PATH variable may be set to

/bin:/usr/bin:/usr/local/bin

This is a list of directories for the shell to search, each directory separated by a ``:''. When you use the command ls, the shell first looks for /bin/ls, then /usr/bin/ls, and so on.


  • Note that the PATH has nothing to do with finding regular files. For example, if you use the command

/home/larry# cp foo bar

the shell does not use PATH to locate the files foo and bar--those filenames are assumed to be complete. The shell only uses PATH to locate the cp executable.



  • This saves you time, and means that you don't have to remember where all the command executables are stored.

  • On many systems, executables are scattered about in many places, such as /usr/bin, /bin, or /usr/local/bin. Rather than give the command's full pathname (such as /usr/bin/cp), you can set PATH to the list of directories that you want the shell to automatically search.

  • Notice that PATH contains ``.'', which is the current working directory. This lets you create a shell script or program and run it as a command from your current directory without having to specify it directly (as in ./makebook). If a directory isn't in your PATH, then the shell will not search it for commands to run; this also includes the current directory.



Shell initialization scripts.


  • In addition to the shell scripts that you create, there are a number of scripts that the shell itself uses for certain purposes. The most important of these are initialization scripts, which are scripts executed by the shell when you log in.

  • The initialization scripts themselves are simply shell scripts. However, they initialize your environment by executing commands automatically when you log in. If you always use the mail command to check your mail when you log in, you place the command in the initialization script so it will execute automatically.

  • Both bash and tcsh distinguish between a login shell and other invocations of the shell. A login shell is a shell invoked when you log in. Usually, it's the only shell you'll use. However, if you ``shell out'' of another program like vi, you start another instance of the shell, which isn't your login shell. In addition, whenever you run a shell script, you automatically start another instance of the shell to execute the script.

  • The initialization files used by bash are: /etc/profile (set up by the system administrator and executed by all bash users at login time), $HOME/.bash_profile (executed by a login bash session), and $HOME/.bashrc (executed by all non-login instances of bash). If .bash_profile is not present, .profile is used instead.

  • See the manual pages for bash or tcsh to learn more about customizing the Linux environment.



Preventing Redirection from Overwriting Files


  • The redirection operator (>) overwrites an existing file.

  • If you write a shell script that uses the > operator to create a file, you may want to prevent if from overwriting important information. You can use the set command with the -o noclobber option to prevent a file from being overwritten, as in the following example:

$ set -o noclobber

  • The noclobber option may be ignored by placing an exclamation point after the redirection operator:

ckelly@linux:~$ set -o noclobber

ckelly@linux:~$ cat file1 > file2

bash: file2: cannot overwrite existing file

ckelly@linux:~$ cat file1 >! file2

ckelly@linux:~$

Decision Logic


  • Decision logic enables your script to execute a statement or series of statements only if a certain condition exists.

  • The if statement is the primary decision-making control structure.

echo -n "What is your favorite operating system? "

read OS_NAME

if [ "$OS_NAME" = "UNIX" ]

then


echo "You will like Linux."

else


if [ "$OS_NAME" = "WINDOWS" ]

then


echo "A great OS for applications."

else


echo "You should give Linux a try!"

fi

fi



  • The following are examples of some of the different comparisons that you may use.

if [ "$A" = "B" ]

if [ "$A" != "B" ]

if [ "$A" "<" "B" ]

if [ "$A" ">" "B" ]

if [ "$A" ">=" "B" ]

if [ "$A" "<=" "B" ]



Looping Logic


  • Use the for command for looping through a range of values. It causes a variable to take on each value in a specified set, one at a time.

  • The following script would display the files chap1, chap2, chap3.

for file in chap[123]; do

more $file

done


  • The while statement sets up a loop to test repeatedly for a matching condition.

echo -n "Try to guess my favorite color "

read guess

while [ "$guess" != "red" ]; do

echo "No, not that one. Try again. "; read guess

done

Passing Arguments to Scripts


  • Scripts may be written to use command line arguments.

  • If a script requires arguments it should always check to see whether the user has supplied them. The following script checks for the presence of an argument.

if test $# = 0

then


echo You must supply at least one argument.

exit


fi

  • The following example script is named out.

  • When you call out with arguments that are names of files, it displays the files on the terminal. If the first argument is a -v, out uses more to display the files. After determining that it was called with at least one argument, out tests its first argument to see if it is not -v. If the test is true (if the first argument is not -v), the script uses cat to display the files. If the test is false (if the first argument is -v), out shifts the arguments to get rid of the -v and displays the files using more.

if [ $# = 0 ]

then


echo "Usage: out [-v] filenames" 1>&2

exit 1


fi

if [ "$1" != "-v" ]

then

cat "$@"


else

shift


more "$@"

fi

Generating Random Numbers in Scripts





  • The RANDOM environment variable contains a random number between 0 and 32767.

  • To generate a random number between 0 and 100 do the following:

let "X = RANDOM/327"
Yüklə 37,76 Kb.

Dostları ilə paylaş:




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə