Difference between revisions of "BASH Scripting"

From PHYSpedia
Jump to: navigation, search
(Pipes (the case for stdin))
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
Scripting refers to the practice of writing "scripts" to perform tasks on a machine. A shell script is a file that contains a set of command that well be executed as if they were typed in at the command line. Shell scripts make use of the wide range of command line tools that already exist for processing text and data. Scripts have been used by system administrators for years to automate repetitive task, but scripting has many applications in computational physics and high performance computing.
+
<code>bash</code> supports many features available in traditional programming languages. However, since <code>bash</code> is a shell first, and programming language second, there are a few quirks that you need to be aware of. This page covers several topics that are useful when writing bash scripts and provides examples.
  
==Pipes (the case for stdin)==
 
One of the most powerful features of Unix, and therefore Linux, is pipes. Pipes direct the standard output of one program to the standard input of another program. So, for exmaple, the <code>cat</code> command prints the contents of a file to standard output
 
$ cat file.txt
 
  1 2
 
  2 3
 
  3 4
 
  
In this example, the file named file.txt contains three lines. The <code>cat</code> command prints these lines to standard output. Now, the <code>grep</code> command reads lines from standard input, and prints lines that match a pattern to standard output. So, if we wanted to see only the lines in file.txt that contain the number 3, we can ''pipe'' the standard output of the <code>cat</code> command to <code>grep</code>
+
====variables====
$ cat file.txt | grep "3"
+
Bash supports several "programming" concepts, including variables. However, the syntax for using variables in bash can seem a little strange. When setting variables, use only the variable name
  2 3
+
and make sure there is not a space between the variable name and the equal sign. When deferencing a variable, put the variable name in <code>${}</code>. Variable substitution will be performed inside of double
  3 4
+
quotes, but not inside of single quotes. Here is an example:
 +
<code>
 +
#! /bin/bash
 +
 +
msg="hello world"
 +
 +
echo "i am supposed to give you this message: ${msg}"      # will print: i am supposed to give you this message: hello world
 +
echo 'i am supposed to give you this message: ${msg}'      # will print: i am supposed to give you this message: ${msg}
 +
echo "i am supposed to give you this message: '${msg}'"    # will print: i am supposed to give you this message: 'hello world'
 +
echo "i am supposed to give you this message: \"${msg}\""  # will print" i am supposed to give you this message: "hello world"
 +
 
 +
The shell automatically sets several special variables. These special variables <code>${1}, ${2}, ...<code> contain the arguments that were given to your script.
 +
<code>
 +
#! /bin/bash
 +
 +
echo "arg 1: $1"
 +
echo "arg 2: $2"
 +
echo "arg 3: $3"
 +
echo "not sure about the rest"
 +
</code>
 +
 
 +
====conditionals (<code>if</code> statements)====
 +
Bash also supports running code "conditionally". This is useful for checking that the correct number of arguments where given to your script, or
 +
 
 +
====Other====

Latest revision as of 10:31, 19 February 2013

bash supports many features available in traditional programming languages. However, since bash is a shell first, and programming language second, there are a few quirks that you need to be aware of. This page covers several topics that are useful when writing bash scripts and provides examples.


variables

Bash supports several "programming" concepts, including variables. However, the syntax for using variables in bash can seem a little strange. When setting variables, use only the variable name and make sure there is not a space between the variable name and the equal sign. When deferencing a variable, put the variable name in ${}. Variable substitution will be performed inside of double quotes, but not inside of single quotes. Here is an example:


#! /bin/bash

msg="hello world"

echo "i am supposed to give you this message: ${msg}"      # will print: i am supposed to give you this message: hello world
echo 'i am supposed to give you this message: ${msg}'      # will print: i am supposed to give you this message: ${msg}
echo "i am supposed to give you this message: '${msg}'"    # will print: i am supposed to give you this message: 'hello world'
echo "i am supposed to give you this message: \"${msg}\""  # will print" i am supposed to give you this message: "hello world"

The shell automatically sets several special variables. These special variables <code>${1}, ${2}, ...<code> contain the arguments that were given to your script.

<code>
#! /bin/bash

echo "arg 1: $1"
echo "arg 2: $2"
echo "arg 3: $3"
echo "not sure about the rest"

conditionals (if statements)

Bash also supports running code "conditionally". This is useful for checking that the correct number of arguments where given to your script, or

Other