Custom Search

Bash Shell Script

Creating a simple shell script

A shell script is a file that contains commands that the shell can execute.

Bash shell supports

  • Variables that store values
  • The capability to evaluate expressions
  • The capability to define functions
  • Control structure

How to create a script file:

Start a script with #! (called a shebang line)

Specify the shell you are using after #!

#!/bin/bash

After indicating the shell, commands are entered onto each line of the file

Comments

Comments make shell scripts easier to read and maintain

A hash sign (#) starts a comment line, and the comment ends when you 'return carriage' or press 'enter' at the end of that line

 

#!/bin/bash

#This script displays the date and who’s logged on

date
who

Displaying Messages

echo command can display simple text string

$ echo This is a test
This is a test

By default, there is no need to use quotes to delineate the string, but can get tricky when using quotes within the string

$echo Let’s see if this’ll work
Lets see if thisll work

There is a problem. It ignores the apostrophes and prints.If we want to define exactly what to print we use either double (") or single (') quotes to enclose text strings.

Use one type within the text and the other type to delineate the string

$echo “Let’s see if this’ll work”
Let’s see if this’ll work

 

Double quotes allow variable substitution, single quotes do not.

How to run a shell script – Method 1

Make the file executable

After you create a shell script using a editor, make it executable by using chmod command

$ ./greeting.sh

./greeting.sh: Permission denied.
$ ls -l greeting.sh


-rw------- 1 hem1 seas 22 Feb 18 09:33 greeting.sh

$ chmod u+x greeting.sh
$ ./greeting.sh

How to run a shell script – Method 2

Pass the shell script as a parameter to a shell program

Run a shell script which does not have execution permission

$bash filename

Run the script with different shell other than your interactive shell

$sh filename

Environment Variables

Shell maintains environment variables that track specific system information

$ set

HOME The user’s login directory

SHELL The current shell being used

PATH The executable path

PS1 The value of the command prompt

Reference environment variables by using the variable’s name preceded by a dollar sign

echo $HOME

Environment variable - PATH

Another important environment variable is PATH. PATH is a list of directories that the shell uses to locate executable files for commands. So if the PATH is set to:

/bin:/usr/bin:/usr/local/bin:.

When you typed ls, the shell would look for /bin/ls, /usr/bin/ls etc. Note that the PATH contains '.', i.e. the current working directory.

This allows you to create a shell script or program and run it as a command from your current directory without having to explicitly say
"./filename".

Note that PATH has nothing to do with filenames that are specified as arguments to commands

User variables

Names

  • A shell variable can be any text string of up to 20 letters.
  • It must start with a letter or underscore character, followed by 0 or more letters, numbers or underscores
  • User variables are case sensitive

Assigning variables

Values are assigned to the user variable using an equal sign. (NB: No spaces can appear between the variable, the equal sign and the value).

User variables can be referenced using the $ sign:

Var=10

Var2=-57

Var3=“testing”

Var4=“still more testing”

An Example

 

#!/bin/bash

#testing variables

days=10

guest=“Katie”

echo “$guest checked in $days days ago.”

value1=10

value2=$value1

echo The resulting value is $value2

When referencing a variable value, use the $ sign; when referencing the variable to assign a value to it, not to use the $ sign

Variables created within a shell are local to that shell, so only that shell can access them.

By default, variables are local

To turn a local variable to an environment variables

$export variable_name

Environment variables are passed to subprocesses. Local variables are not.

The backtick

Backtick (`) is the lowly back quote character. The backtick allows you to assign the output of a shell command to a variable.

#!/bin/bash

# using backtick character

testing=`date`

echo “The date and time are: ” $testing

Positional Parameters

The command name and arguments can by referenced by their position on the command line

$0 : Name of the calling program

$1 - $9 : Command-line Arguments

The first argument is represented by $1

The second argument is represented by $2

$./display_args 1 2 3 4 5

you are running script ./display_args with
parameter 1 2 3 4 5

Access the parameter after $9

use ${10}, ${11}, etc.

shift

Built-in command shift promotes each of the command-line arguments.

The first argument (which was $1) is discarded

The second argument (which was $2) becomes

$1

The third becomes the second and so on.

Repeatedly using shift is another way to loop over all the command-line arguments

Positional Parameters
#demo_shift.sh
echo $1 $2 $3
shift
echo $1 $2
shift
echo $1

• $./demo_shift 1 2 3
1 2 3
2 3
3

Special Parameters
• Special parameters give you some useful values
— Number of the Command-line arguments
— Return status of the execution of shell commands
• Special parameters’ value can not be changed
directly, like positional parameters
• Value of Command-line arguments: $* and $@
— $* and $@ represent all the command line arguments
(not just the first nine)
— “$*” : treat the entire list of arguments as a single
argument
— “$@” : produce a list of separate arguments.
—$#: the number of arguments
—$$: the current shell’s PID number

Special Parameters $?
• Exit status: $?
—When a process stops executing for any reason, it returns
an exit status to its parent process.
• By convention
—Nonzero represents a false value that the command failed.
— A zero value is true and means that the command was
successful
• You can specify the exit status that a shell script
returns by using the exit built-in followed by a
number
• Otherwise, the exit status of the script is the exit
status of the last command the script ran.