7.2.5. Shell scripts
126.96.36.199. What are scripts?
A shell script is, as we saw in the shell configuration examples, a text file containing shell commands. When such a file is used as the first non-option argument when invoking Bash, and neither the '-c' nor '-s' option is supplied, Bash reads and executes commands from the file, then exits. This mode of operation creates a non-interactive shell. When Bash runs a shell script, it sets the special parameter '0' to the name of the file, rather than the name of the shell, and the positional parameters are set to the remaining arguments, if any are given. If no additional arguments are supplied, the positional parameters are unset.
A shell script may be made executable by using the chmod command to turn on the execute bit. When Bash finds such a file while searching the PATH for a command, it spawns a sub-shell to execute it. In other words, executing
is equivalent to executing
bash filename ARGUMENTS
if 'filename' is an executable shell script. This sub-shell reinitializes itself, so that the effect is as if a new shell had been invoked to interpret the script, with the exception that the locations of commands remembered by the parent (see hash in the Info pages) are retained by the child.
Most versions of UNIX make this a part of the operating system's command execution mechanism. If the first line of a script begins with the two characters '#!', the remainder of the line specifies an interpreter for the program. Thus, you can specify bash , awk , perl or some other interpreter or shell and write the rest of the script file in that language.
The arguments to the interpreter consist of a single optional argument following the interpreter name on the first line of the script file, followed by the name of the script file, followed by the rest of the arguments. Bash will perform this action on operating systems that do not handle it themselves.
Bash scripts often begin with '#! /bin/bash' (assuming that Bash has been installed in '/bin'), since this ensures that Bash will be used to interpret the script, even if it is executed under another shell.
188.8.131.52. Some simple examples
A very simple script consisting of only one command, that says hello to the user executing it:
[jerry@nowhere ~] cat hello.sh #!/bin/bash echo "Hello $USER"
The script actually consists of only one command, echo , which uses the value of ($) the USER environment variable to print a string customized to the user issuing the command.
Another one-liner, used for displaying connected users:
#!/bin/bash who | cut -d " " -f 1 | sort -u
Here is a script consisting of some more lines, that I use to make backup copies of all files in a directory. The script first makes a list of all the files in the current directory and puts it in the variable LIST . Then it sets the name of the copy for each file, and then it copies the file. For each file, a message is printed:
tille:~> cat bin/makebackupfiles.sh #!/bin/bash # make copies of all files in a directory LIST='ls' for i in $LIST; do ORIG=$i DEST=$i.old cp $ORIG $DEST echo "copied $i" done
Just entering a line like mv * *.old won't work, as you will notice when trying this on a set of test files. An echo command was added in order to display some activity. echo 's are generally useful when a script won't work: insert one after each doubted step and you will find the error in no time.
The /etc/rc.d/init.d directory contains loads of examples. Let's look at this script that controls the fictive ICanSeeYou server:
#!/bin/sh # description: ICanSeeYou allows you to see networked