Crash+course+in+shell+scripts

toc =Introduction= This article discusses shell scripts very, very briefly. The Internet abunds with information on the subject, so this is only to get you started.

=What is a shell script?= A shell script, AKA "bash script", "shell program" is a sequence of shell commands collected in a file, the shell script. That means that the commands you can use are basically the same as those you use in the shell.

=Shell scripts 101=

Structure
All shell scripts share this common structure: code < > code The first line simply means that you want to execute this script using /bin/sh (the shell). This //must// be the first line of any shell script
 * 1) ! /bin/sh

Making the shell script executable
To execute your shell script, you must first make it executable. Use the command for this. Afterwards, you can write use the command to execute the script.

If you copy your shell script to (or some other location that is in your, you can start the script simply by using the command >>.

Input variables
Most often, a script will take input from the command line, typically file names. Input can be used in the script by the $ macros, where is the script's name,  is the first command-line argument,  is the second and so forth.

=An example= We wish to create a small script that creates a backup of all files in the source directory in the specified archive. First, we create the script: code $ gedit back_it_up code

This opens the new file in the editor gedit and we write the following: code tar -cvf $2 $1 code
 * 1) ! /bin/sh

Back in the shell, we make the script executable and move it to code $ chmod a+x back_it_up $ sudo cp back_it_up /usr/bin code

Now, if we wish to create a backup of the directory  we would use the shell script as follows: code $ back_it_up /home/stud/important_stuff/ backup.tar code

..plain & simple! =More information= Check out quick reference for more information on commands and control structures that you can use.