What is the use of shell scripting

Shell Scripts sh (Bourne Shell)

  1. Preliminary remark
  2. General information about shell scripts
  3. call
  4. Simple example
  5. Testing a shell script
  6. Command line parameters
  7. Process control
    1. Conditional execution: if
    2. Multiple decision: case
    3. Loop: for
    4. Loop: while and until
  8. literature

Preliminary remark

Under Unix, shell scripts (command procedures) are the analogue of batch files (batch files) from MS-DOS, but they are much more powerful. However, their syntax depends on the "shell" used. This article looks at Bourne Shell (sh) scripts. They are generally considered to be more reliable than csh scripts.

General information about shell scripts

A shell script is a text file in which commands are stored. It is a command itself and, like a system command, can also be called with parameters. Shell scripts are used to make work easier and (after sufficient tests) to increase reliability, as they allow frequently used sequences of commands to be combined.

call

Bourne Shell Scripts can generally be called as follows:

myscript

In general, however, it is more practical to register the file as executable (execute permission) using

myscript

The shell script can then be called like a "normal" (binary) command:

myscript

A prerequisite is that the operating system processes the script with the correct shell. Modern Unix systems check the first line for this purpose. For the sh it should have the following content:

Although the pound sign usually introduces a comment that is ignored by the system, it recognizes here that the "sh" (with an absolute path: / bin / sh) should be used.

Simple example

#! / bin / sh # Simple example echo Hello, world! echo date, time and working directory: date pwd echo transfer parameters: $ *

The above simple script essentially contains normal Unix commands. Apart from the first line, the only specialty is the placeholder "$ *", which stands for all command line parameters.

Testing a shell script

myscript
Syntax test (the commands are read and checked, but not executed)
myscript
Output of the shell commands in the read form
myscript
Output of the shell commands after all replacements have been carried out, i.e. in the form in which they are carried out

Command line parameters

$0
Name of the command procedure that is currently being executed
$#
Number of parameters
$1 $2 $3 ...
first, second, third ... parameters
$*
stands for all command line parameters ($ 1 $ 2 $ 3 ...)
[email protected]
like $ * ($ 1 $ 2 $ 3 ...)
"[email protected]"
expands (in contrast to "$ *") to:
"$1" "$2" "$3" ...
$$
Process number of the shell (useful for assigning unique names to temporary files)
$-
represents the current shell options
$?
indicates the return code of the last command executed (0 if execution was successful)
$!
Process number of the last executed background process

Example:

#! / bin / sh # variables echo transfer parameters: $ * echo user is: $ USER echo shell is: $ SHELL echo parameter 1 is: $ 1 echo procedure name is: $ 0 echo process number is: $$ echo number of parameters is: $ # a = 17.89 # without gaps am = characters echo a is $ a

Process control

Conditional execution: if

if[condition]thencommands1elsecommands2fi

Remarks: "fi" is an "if" written backwards, it means "end if" (this notation is a special feature of the Bourne Shell). The "condition" corresponds to the syntax of test, see also below. The "else" branch can be omitted in the if-construct, on the other hand an extension by one or more "else if" branches is possible, which are called "elif" here:

if[condition1]thencommands1elif[condition2]thencommands2elsecommands3fi
The formulation
if[condition]
is equivalent to
iftestcondition
Alternatively, it is possible to check the success of a command:
ifcommand
(for example the command "true" always returns "true", the command "false", however, returns "untrue")

Important comparison operations (test)

Note: It is absolutely necessary that all operators are surrounded by spaces, otherwise they will not be recognized by the shell! (This also applies to the brackets.)
Strings
"s1" = "s2"
true if the strings are the same
"s1"! = "s2"
true if the strings are not equal
-z "s1"
true if the string is empty (length equal to zero)
-n "s1"
true if the string is not empty (length greater than zero)
(Whole numbers
n1 -eq n2
true if the numbers are the same
n1 -ne n2
true if the numbers are unequal
n1 -gt n2
true if the number n1 is greater than n2
n1 -ge n2
true if the number n1 is greater than or equal to n2
n1 -lt n2
true if the number n1 is less than n2
n1 -le n2
true if the number n1 is less than or equal to n2
miscellaneous
!
negation
-a
logical "and"
-O
logical "or" (non-exclusive; -a has a higher priority)
\( ... \)
Round brackets are used for grouping. Note that they must be protected by a preceding backslash, \.
-f filename
true if the file exists. (More options can be found in the test man page)

Example:

#! / bin / sh # Interactive input, if query echo Hello, user, are you all right? echo your answer, n / y: read answer echo your answer was: $ answer # if ["$ answer" = "y"] if ["$ answer"! = "n"] then echo yes else echo no fi

Multiple decision: case

casevarinpattern1)commands1;;pattern2)commands2;;*)default commands;;esacRemarks: "esac" is a "case" written backwards, it means "end case". The individual cases are specified by specifying a pattern, "muster)", and terminated by a double semicolon. (A single semicolon serves as a separator for commands that are on the same line.) The pattern "*)" acts as "default", it covers all remaining cases; the use of the default branch is optional.

Example:

#! / bin / sh # Interactive input, multiple decision (case) echo Everything okay? echo Your answer: read answer echo Your answer was: $ answer case $ answer in j * | J * | y * | Y *) echo yes ;; n * | N *) echo no, not at all! ;; *) echo that was probably nothing ;; esac

Loop: for

foriinpar1 par2 par3 ...docommandsdoneRemarks: The for loop in the Bourne Shell differs from the for loop in common programming languages ​​in that a sequence number is not automatically generated. The parameters following "in" are successively assigned to the loop variables. (The specification "for i in $ *" can be abbreviated to "for i".)

Example:

#! / bin / sh # Loops: for echo Transfer parameters: $ * # for i for i in $ * do echo This is where: $ i done

Loop: while and until

while[condition]docommandsdoneuntil[condition]docommandsdoneAnnotation: With "while" the condition is checked before the loop is processed, with "until" only afterwards. (Instead of "[ condition ] "or" test condition"a command can generally be used, the return code of which is checked; cf. the comments on" if ".)

Example:

#! / bin / sh # loops: while # with generation of a sequence number i = 1 while [$ i -le 5] do echo $ i i = `expr $ i + 1` done

Used literature

  • Reinhard Detering: "UNIX Handbook". Sybex, Düsseldorf, 1989.

Burkhard Kirste, 1994/01/19