Shell script

from Wikipedia, the free encyclopedia
Shell script
Gnome-mime-text-x-sh.svg
Unix-ed-shellscript.png


Shell script .bash_profilein the text editor ed

File extension : .sh
MIME type : application / x-shellscript
Initial release: with UNIX
Type: Scripting language
Contained in: Unix shell
Extended by: various Unix shells, etc. a. Bash , Z shell and much more
Standard (s) : POSIX


Shell script .bash_profilein the text editor ed

A shell script or shell script is a computer program that is interpreted and executed by a shell . It is ultimately an executable text file in which all those instructions can be used to a user in the command line can use.

According to computer scientist Brian Kernighan , the shell is itself a programming language.

"The shell is actually a programming language: it has variables, loops, decision-making, and so on."

"The shell is actually a programming language: there are variables, loops, branches and so on."

- Brian Kernighan

Different scripting languages ​​are used in the various shells.

Although every shell has the ability to execute shell scripts, the term shell script is mostly used for Unix shell scripts. This article therefore refers to scripts in Unix-like systems .

In command line interpreters of DOS such. For example, COMMAND.COM or 4DOS , which are technically also shells, scripts are called " batch files ". In the PowerShell from Microsoft they are called "Power Shell scripts".

Compatibility of different Unix shells with one another

Since different Unix shells, such as For example, if the bash or the C shell do not use the same syntax , a shell script can usually only be used for a specific shell.

The following Unix shells use a common syntax basis:

Bourne -compatible shells
C shell -compatible shells
  • csh
  • tcsh
Thompson Shell Compatible Shells
  • osh
Z shell -compatible shells
  • zsh

File format

The first line of the shell script specifies which shell should be used to execute the script. This is done by specifying the file path to the respective shell program. Programs can be called and variables used within a shell script. Branches and loops are also possible.

A shell script starts with the shebang , #!followed by the path to the shell. If the first line (with the shebang) is omitted, the script is usually still executed as a shell script. Which shell is used by default for this, however, depends on the respective Unix derivative . Omitting the shebang line means that it cannot be saved with which shell the script is being executed and is therefore not recommended.

The most common Unix shell is the bash shell. Therefore, in the following example code, a file path is used in the first line that points to the bash shell:

#!/bin/bash
# Shellskript für die Bash
for i in 0 1 2
do
    echo $i
done

particularities

Operators, functions and spaces

A special feature, in contrast to other programming languages, is the mandatory use of spaces when calling operators and functions.

Examples:

# Korrekte Schreibweise für einen Vergleich der Zahlen 1 und 5:
if (( 1 == 5 ));
    then echo "1 und 5 sind gleich"
else
    echo "false"
fi

# Inkorrekte Schreibweise:
# Die Test-Funktion "((" wird vom Interpreter nicht erkannt, da das Leerzeichen vor der "1" fehlt.
if ((1 == 5 ));

# Inkorrekte Schreibweise:
# Der Vergleichsoperator "==" wird vom Interpreter nicht erkannt, da die Leerzeichen vor und nach "==" fehlen.
if (( 1==5 ));

Boolean variable

Another special feature of shell script is that the number 0 true (English true equivalent), while numbers greater than 0 wrong (English false match). In other programming languages ​​such as C , Java or Python and also in logic , the truth value is defined exactly the other way around. The reason is that the shell often has to evaluate return values ​​from called programs. A return of 0 corresponds to an error-free call of the program.

Run a shell script

Call in the Unix shell

By default, newly created files cannot be executed under Unix. In order to be able to execute a shell script, it must be set to "executable" via the access rights . This is done using the chmod command .

chmod +x script.sh

If the shell script is to be executed from any path , the complete path to the shell script must be specified.

Example: The shell script /usr/local/bin/script.sh is to be executed. The following command must be entered:

/usr/local/bin/script.sh

If the shell script in the current working directory is stored (English "working directory") and is to be performed there, this is done by prepending of the actual path ( "./"):

./script.sh

Place of execution

The working directory of the shell script is the path from which it is called, not the place where it is itself.

file extension

The file extension of a shell script is usually .sh. However, no file extension has to be used, as this has no technical significance in Unixoid systems and only serves as a distinguishing feature for the user.

MIME type

Besides application/x-shellscriptthere is still the MIME type application/x-sh . The Unix program file --mime-typeoutputs the type text/x-shellscript.

literature

  • Brian W. Kernighan: The UNIX Programming Environment . Prentice Hall, 1984, ISBN 0-13-937699-2

Web links

Individual evidence

  1. ^ Brian W. Kernighan: The UNIX Programming Environment . Prentice Hall, 1984, ISBN 0-13-937699-2 , Chapter 3: Using the Shell , p. 94.
  2. itworld.com
  3. bash gnu.org
  4. ubuntuusers.de/Dash
  5. ^ Bash Shell Scripting . Wikibooks (English)
  6. opensource.com
  7. linuxacademy.com