What is the use of shell scripting
Shell Scripts sh (Bourne Shell)
- Preliminary remark
- General information about shell scripts
- Simple example
- Testing a shell script
- Command line parameters
- Process control
- Conditional execution: if
- Multiple decision: case
- Loop: for
- Loop: while and until
Preliminary remarkUnder 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 scriptsA 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.
callBourne Shell Scripts can generally be called as follows:
In general, however, it is more practical to register the file as executable (execute permission) using
The shell script can then be called like a "normal" (binary) command:
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
- Syntax test (the commands are read and checked, but not executed)
- Output of the shell commands in the read form
- 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
- 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
Conditional execution: ifif[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
- is equivalent to
- Alternatively, it is possible to check the success of a command:
- (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.)
- "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)
- 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
- logical "and"
- 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: casecasevarinpattern1)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: forforiinpar1 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 untilwhile[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
- Reinhard Detering: "UNIX Handbook". Sybex, Düsseldorf, 1989.
Burkhard Kirste, 1994/01/19
- Where are the Tongans from?
- Can I do meth at home
- Why did bin Laden attack the US?
- What is the business of the government
- How does leverage work in intraday trading
- How old will you be
- How do cataracts affect eyesight?
- Which database does Elasticsearch use
- Can a certification body become an IPS officer
- Is the US under spiritual attack?
- How does aspirin work against pain
- How many Tamils live in Kashmir
- What time is my love is gone
- How did Karl Marx see democracy
- What's wrong with JUUL
- What are some reputable payment gateway providers
- A laser beam can occur naturally
- How should we value cubism?
- How does extreme poverty affect the economy?
- What do suicide bombers gain
- Should the international community intervene in Venezuela
- Will Africa ever unite like Western Europe?
- Take Denny on a paid vacation
- How many Telugu speakers are there in Pune