Bash Shell Scripting

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Contents

[edit] What

Bash is a Unix shell which extends upon the Bourne shell syntax to support new features such as integer calculation and in-process regular expressions.

[edit] Why

Shell scripting is particularly well suited to many system administration jobs, such as; performing disk backups, easy evaluation of system logs, and many other functions. Bash is included by default in many Linux distributions, and is also available to install on other Unix-based systems, such as FreeBSD.

[edit] Who

Bash was created in 1987 by Brian Fox. In 1990 Chet Ramey became the primary maintainer.

[edit] How

[edit] Hello World

#!/bin/bash
echo "Hello World"

[edit] Basic Syntax

[edit] Comments

# This is a comment

[edit] Variables

Variables store any piece of information, whether it is a string or a number.

In order to set a variable my_variable storing a value of "water"

my_variable=water

In order to recall this variable, add a dollar sign "$" before the start of the variable name, like so

$my_variable

This could be used in most command, including echo commands. exception : export variablename

[edit] Statements

[edit] Program Control

[edit] For loops

for arg; do echo $arg; done
for arg in $*; do echo $arg; done
for arg in "$@"; do echo $arg; done

[edit] Conditional statements

Conditional statements are implemented in Bash using the following syntax:

if [ expression ];
then
    echo "True";
else
    echo "False";
fi

The expression block, beginning with [ and ending with ], can contain unary or binary expressions.

Unary expressions in shell scripting are often used for checking the status of files, such as whether a file exists or not, or if the file is executable. The following example demonstrates a unary file operation to check if the user can access the file /var/log/auth.log:

#!/bin/bash
 
AUTHLOG='/var/log/auth.log'
 
if [ -r $AUTHLOG ];
then
    echo "--- Authorization log ---";
    tail $AUTHLOG | less -;
else
    echo "Unable to access log file $AUTHLOG";
    exit 1;
fi

Binary expressions are used for comparing strings and numbers.


[edit] Methods

[edit] Arguments

[edit] Objects

[edit] Input / Output

[edit] User

[edit] Using whiptail

Whiptail is a program that allows shell scripts to display dialog boxes to the user for informational purposes, or to get input from the user in a friendly way. Whiptail is included by default on Debian.

[edit] Info box

A simple type of dialog box in Whiptail is the info box. This shows a dialog box containing text to the user.

whiptail --title "Example Dialog" --infobox "This is an example of an info box." 8 78

In the example above, the value of --title is displayed at the top of the dialog box. The first argument to --infobox is the dialog box text which is shown beneath the title. The next two arguments specify the height and width of the dialog box. The width is set to 78 as most terminals will be at least 80 columns or more.

[edit] Message box

A message box is very similar to an info box, except that it waits for the user to hit the OK button. Usage is similar to the info box:

whiptail --title "Example Dialog" --msgbox "This is an example of a message box. You must hit OK to continue." 8 78
[edit] Yes/no box

The simplest way to get input from the user is via a Yes/no box. This displays a dialog with two buttons labelled Yes and No.

whiptail --title "Example Dialog" --yesno "This is an example of a yes/no box." 8 78
 
exitstatus=$?
if [ $exitstatus = 0 ]; then
    echo "User selected Yes."
else
    echo "User selected No."
fi
 
echo "(Exit status was $exitstatus)"
[edit] Input box
[edit] Text box
[edit] Menus

Whenever you want to present a list of options to the user, whiptail has several dialog types to choose from.

A menu should be used when you want the user to select one option from a list, such as for navigating a program.

whiptail --title "Menu example" --menu "Choose an option" 20 78 16 \
"<-- Back" "Return to the main menu." \
"Add User" "Add a user to the system." \
"Modify User" "Modify an existing user." \
"List Users" "List all users on the system." \
"Add Group" "Add a user group to the system." \
"Modify Group" "Modify a group and its list of members." \
"List Groups" "List all groups on the system."

The values given to --menu are:

  • The text describing the menu ("Choose an option")
  • The height of the dialog (20)
  • The width of the dialog (78)
  • The height of the menu list (16)

The rest of the values are a list of menu options in the format tag item, where tag is the name of the option which is printed to stderr when selected, and item is the description of the menu option.

If you are presenting a very long menu and want to make best use of the available screen, you can calculate the best box size by.

eval `resize`
whiptail ... $LINES $COLUMNS $(( $LINES - 8 )) ...
[edit] Check list

At some point, you will want to present options to the user which would not be appropriate to place in a menu.

A check list allows a user to select one or more options from a list.

whiptail --title "Check list example" --checklist \
"Choose user's permissions" 20 78 16 \
"NET_OUTBOUND" "Allow connections to other hosts" ON \
"NET_INBOUND" "Allow connections from other hosts" OFF \
"LOCAL_MOUNT" "Allow mounting of local devices" OFF \
"REMOTE_MOUNT" "Allow mounting of remote devices" OFF

When the user comfirms their selections, a list of the choices is printed to stderr.

[edit] Radio list

A radio list is a dialog where the user can select one option from a list. The difference between a radio list and a menu is that the user selects an option (using the space bar in whiptail) and then comfirms that choice by hitting OK.


[edit] Progress gauge

[edit] Files

[edit] Shell redirection

In shells, redirection is used for file I/O. The most common usage of is to redirect standard streams (stdin, stdout and stderr) to accept input from another program via piping, to save program output as a file, and to suppress program output by redirecting a stream to /dev/null.


[edit] Networks

[edit] Standard Examples

[edit] Specific Examples

[edit] For More Information

[edit] API

[edit] Companies

[edit] User Sites

[edit] Wikibooks