Guides:Shells/sh-bash/Basic Syntax

From CoderGuide

Jump to: navigation, search

Contents

Basic Syntax

A typical Bourne/bash shell script looks like this:

#!/bin/sh

echo "Hello world! What is your name?"
read name
echo "Hello $name!"


read reads in text from standard input (stdin) and stores it in a variable.

The first line tells Unix/POSIX systems (including Mac OS X) which interpreter to use. Since we're not doing anything fancy, "sh" is fine. However, if we're doing anything that relies on more advanced features, or the use of the -e option in the echo command, then bash should be used. Normally under Linux sh was symlinked to bash, however some Linux (and other Unix systems) use a simple Bourne-like shell for sh. bash, by the GNU Project, is installed on nearly all Unix systems today.

Variables

Assigning a value to a variable is easy:

variable=value

That's it! If you want that value visible to functions and other scripts, you need to export it first to copy it to the environment.

To get a value from the environment, prefix the variable with a dollar sign.

Say you have one variable prefix and you want it to appear right before the text "abc", and you type:

$prefixabc

You'll get nothing, because the shell thinks you want prefixabc and not prefix. To fix this, enclose the name in curly bracets:

${prefix}abc

Now you'll get what you want.

Command Line Arguments

Command line arguments are assigned to variable $1-9 and $* ($0 is assigned the name of script). $* is special in that it contains the entire command line. There is a special command shift that will shift all the arguments stored in $0 to $9 over by one and place the next argument in $9. This is useful if you need to process more than nine arguments.

#!/bin/sh

echo "$1"
shift 
echo "$1"

Special Variables

Here is a list of special variables.

$?Stores the exit code of the last command run (processes started in the background don't return an exit code to the shell)
$#The number of arguments/parameters given to the script
$* and $@References all arguments given on the command line
$$Stores the PID (process ID) of the current shell
$!Stores the process ID (PID) of the last command run (useful right after placing a command in the background with &

Doing Math

Well, it's always nice to be anle to do some math. First we'll show the older method, and then one method bash uses, and a third method for when you need to do more complex monitoring.


The expr command will process mathematical calculations given on the command line. Here is a list

+Addition
Subtraction
*Multiplication (Note: you will need to escape/prefix it with a backslash so that the shell will treat it as a regular character, and not a wildcard)
/Division

If you want the output of a command, like expr, to appear inline in your script, then you enclose it in single back quotes. Here are a few examples that can be run from the command line:

# expr 1 + 3
4
# expr 3 /* 2
6
# r=`expr 2 + 4`
# echo `expr $r / 2`
3

The bash method is a little simpler, and does not require a call to an external program. Also, if you use a *, then it will be treated as a *, not as a wild card. Here is how you use the bash operator for arithmetic:

 var=$(($var+1))
 var=$(($var*2))
 var=$((6/3))

Important: If you use this convention, be sure to put #!/bin/bash at the top of your script, instead of tt>#!/bin/sh</tt>, that way the Unix system will be sure to use the real bash interpreter, rather than some other Bourne-like shell.

These functions still only work with integers. If you want to deal with very large numbers, or floating point numbers, then you'll need to use bc, the binary calculator.

Functions

You can define a function simply by:

function() {
  commands
   ...
   ...
} 
function arg1 arg2 arg...

A function essentially works like a mini shell script within your script (in fact a separate command parser is started just for that function). They can be used in the exact same way as any other program or command.

Termination

You can terminate your script with the exit' or return commands. return allows you to specify a exit code from 0 to 255 to be passed to the process that called it. Normally, an exit code of zero means success.

echo all good
return 0
echo "Dang Gremlins!!!"
return 1

Note: the explanation point, '!', has special meaning, and should always be quoted. If you want a string to be taken literally, including all dollar signs, enclose the string in single quotes.

Personal tools