Difference between revisions of "Sh-file"

From HPC Wiki
Jump to navigation Jump to search
(Created page with "== General == A shell script or Sh-file is something between a single command and a (not necessarily) small programm. The basic idea is to couple a few shell commands...")
 
m
Line 1: Line 1:
 
== General ==
 
== General ==
  
A [[shell]] script or Sh-file is something between a single command and a (not necessarily) small programm. The basic idea is to couple 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. Generally there is one command per line.
+
A [[shell]] script or Sh-file is something between a single command and a (not necessarily) small programm. The basic idea is to couple 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 easily.
  
  
Line 9: Line 9:
 
starts a (new) instance of the bash shell, which runs the commands specified in 'yourfile.sh'.
 
starts a (new) instance of the bash shell, which runs the commands specified in 'yourfile.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)
+
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)
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
#!/bin/bash
 
#!/bin/bash
Line 18: Line 18:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
#!/bin/bash
 
#!/bin/bash
 +
 +
# pwd prints the current directory
 
echo 'The current directory is:'
 
echo 'The current directory is:'
# pwd prints the current directory
 
 
pwd
 
pwd
 +
 +
# ls -l gives a list of everything of files and subdirectories
 
echo 'and in this directory you can find:'
 
echo 'and in this directory you can find:'
# ls -l gives a list of everything of files and subdirectories
 
 
ls -l
 
ls -l
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 11:00, 28 February 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 couple 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 easily.


Usage

$ bash myscript.sh

starts a (new) instance of the bash shell, which runs the commands specified in 'yourfile.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 read- and reusability. 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

which prints the current directory and a list of the contained subdirectories and files.



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