Difference between revisions of "Sh-file"

From HPC Wiki
Jump to navigation Jump to search
Line 2: Line 2:
  
 
A [[shell]] script or sh-file is something between a single command and a (not necessarily) small programm. The basic idea is to chain a few [[shell]] commands together in a file for ease of use. So whenever you tell the [[shell]] to execute that file, it will execute all the specified commands in order. This makes it very easy to run a couple of commands and gives one the opportunity to simplify frequent tasks.
 
A [[shell]] script or sh-file is something between a single command and a (not necessarily) small programm. The basic idea is to chain a few [[shell]] commands together in a file for ease of use. So whenever you tell the [[shell]] to execute that file, it will execute all the specified commands in order. This makes it very easy to run a couple of commands and gives one the opportunity to simplify frequent tasks.
 
  
 
== Usage ==
 
== Usage ==
 
  $ bash myscript.sh
 
  $ bash myscript.sh
  
starts a (new) instance of the bash shell, which runs the commands specified in 'myscript.sh'.
+
starts a (new) instance of the bash shell, which runs the commands specified in <code>myscript.sh</code>.
  
To create or write a new shell script file, one can utilize e.g. [[vim]] or a different text editor. Generally a script starts with a shebang (#!) specifying the type of shell the script expects (bash in this case)
+
To create or write a new shell script file, one can utilize e.g. [[vim]] or a different text editor. Generally a script starts with a shebang (<code>#!</code>) specifying the type of shell the script expects (bash in this case)
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
#!/bin/bash
 
#!/bin/bash
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Following that, one continues with [[shell]] commands, one per line. Comment lines start with '#' and are highly recommended for improving the readability of the code and later support its reusability for other tasks. Furthermore the 'echo' command is useful to display information on what is currently being done or return results. A minimal shell script might look like this
+
Following that, one continues with [[shell]] commands, one per line. Comment lines start with <code>#</code> and are highly recommended for improving the readability of the code and later support its reusability for other tasks. Furthermore the <code>echo</code> command is useful to display information on what is currently being done or return results. A minimal shell script might look like this:
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
Line 27: Line 26:
 
ls -l
 
ls -l
 
</syntaxhighlight>
 
</syntaxhighlight>
which prints the current directory and a list of the contained subdirectories and files.
 
 
  
 +
The script prints the current directory and a list of the contained subdirectories and files. An output when executing the script might look like this:
  
 +
<syntaxhighlight lang="bash">
 +
$ bash myscript.sh
 +
The current directory is:
 +
/home/user123/Desktop/CPP
 +
and in this directory you can find:
 +
total 20
 +
-rw-rw---- 1 user123 user123  131 Apr 16 13:05 HelloWorld.cpp
 +
-rwxrwx--- 1 user123 user123 8968 Apr 16 13:06 HelloWorld.exe
 +
-rw-rw---- 1 user123 user123 2496 Apr 16 13:06 HelloWorld.o
 +
</syntaxhighlight>
  
 
== Making a file executable ==
 
== Making a file executable ==
Line 40: Line 48:
  
 
However, don't forget the shebang, otherwise this might not work.
 
However, don't forget the shebang, otherwise this might not work.
 
  
 
== References ==
 
== References ==
 
[https://wiki.ubuntuusers.de/Shell/Bash-Skripting-Guide_f%C3%BCr_Anf%C3%A4nger/ Guide to shell scripting]
 
[https://wiki.ubuntuusers.de/Shell/Bash-Skripting-Guide_f%C3%BCr_Anf%C3%A4nger/ Guide to shell scripting]

Revision as of 12:21, 16 April 2018

General

A shell script or sh-file is something between a single command and a (not necessarily) small programm. The basic idea is to chain a few shell commands together in a file for ease of use. So whenever you tell the shell to execute that file, it will execute all the specified commands in order. This makes it very easy to run a couple of commands and gives one the opportunity to simplify frequent tasks.

Usage

$ bash myscript.sh

starts a (new) instance of the bash shell, which runs the commands specified in myscript.sh.

To create or write a new shell script file, one can utilize e.g. vim or a different text editor. Generally a script starts with a shebang (#!) specifying the type of shell the script expects (bash in this case)

#!/bin/bash

Following that, one continues with shell commands, one per line. Comment lines start with # and are highly recommended for improving the readability of the code and later support its reusability for other tasks. Furthermore the echo command is useful to display information on what is currently being done or return results. A minimal shell script might look like this:

#!/bin/bash

# pwd prints the current directory
echo 'The current directory is:'
pwd

# ls -l gives a list of everything of files and subdirectories
echo 'and in this directory you can find:'
ls -l

The script prints the current directory and a list of the contained subdirectories and files. An output when executing the script might look like this:

$ bash myscript.sh
The current directory is:
/home/user123/Desktop/CPP
and in this directory you can find:
total 20
-rw-rw---- 1 user123 user123  131 Apr 16 13:05 HelloWorld.cpp
-rwxrwx--- 1 user123 user123 8968 Apr 16 13:06 HelloWorld.exe
-rw-rw---- 1 user123 user123 2496 Apr 16 13:06 HelloWorld.o

Making a file executable

To execute the script without specifying the shell first, one has to make the file executable with

$ chmod +x myscript.sh

and now one can execute the file with

$ ./myscript.sh

However, don't forget the shebang, otherwise this might not work.

References

Guide to shell scripting