CSC 161 Grinnell College Fall, 2011
 
Imperative Problem Solving and Data Structures
 

Linux Scripts

Introduction

The Bash shell allows users to interact with the computer through a terminal window, organizing commands that tie various programs together. First developed by Brian Fox in 1987, the Bash shell seeks to incorporate numerous features from earlier shells for the Unix operating system. In particular, the Bash shell draws upon the Bourne shell (developed by Ken Thompson and then Stephen Bourne around 1977), the Korn shell (developed by David Korn in the early 1980s), and the C shell (developed by Bill Joy, based on Thompson's earlier shells).

Within a terminal window, you already have used several commands within the Bash shell. For example, Linux lab 1 and Linux lab 2 discussed Bash commands, such as:

In this lab, we consider several additional commands, and we explore how various capabilities can be packaged to accomplish various tasks. The approach is not unlike programming in C (or Scheme), although the programming elements often are entire programs.

Some Additional Commands

Bash includes a huge range of commands. Here are a few more useful capabilities:

Command Description Example
cal display a calendar for a given year cal 2010
date print the current time and date in various formats
many options documented with man date
date +'%I:%M %p on %A, %B %d, %Y'
diff display differences in lines between two files diff file1 file2
echo print specified text echo hello world!
env print the environmental variables currently set env
grep scan a file or other input for a specified text env | grep home
hostname return name of current workstation hostname
sort sort lines of a file sort -n -k 12 (sort by numbers in column 12)
users simple list of users users
who print list of users currently logged in who and who -a
whoami print the username of the person logged in whoami
ypcat password print user information from the password file ypcat password | grep walker

More information on each of these commands may be found with the man facility (e.g., man date). Beyond this basic documentation included here an in previous labs, many resources are available on line. Here are a few places to begin:

Additional Notes

In addition to basic commands, the Bash shell includes capabilities for conditionals (if) and loops (for, while). The long history of Bash is particularly evident in the syntax allowed for if expressions. Bash allows interprets each syntax properly — but do not try to mix and match the different versions. The following examples illustrate acceptable Bash syntax, following two different ancestors.

Task Bourne shell style Korn shell style
numeric test
a > 0
if [ $a -gt \0 ]; then
    echo positive
fi
if (( $a > 0 )); then
    echo positive
fi
numeric test
a ≤ 0
if [ $a -le 0 ]; then
    echo non-positive
else
    echo positive
fi
if (( $a <= 0 )); then
    echo non-positive
else
    echo positive
fi
numeric test
a < 0 and a == 0
if [ $a -lt 0 ]; then
    echo negative
elif [ $a -eq 0 ]; then
    echo zero
else
    echo positive
fi
if (( $a <= 0 )); then
    echo non-positive
elif (( $a == 0 )); then
    echo zero
else
    echo positive
fi
numeric test
0 ≤ a &le 10
if [ 0 -le $a -a $a -le 10 ]; then
    echo between 0 and 10
else
    echo not between 0 and 10
fi
if (( (0 <= $a) && ($a <= 10) )); then
    echo between 0 and 10
else
    echo not between 0 and 10
fi

For reference, all of these code segments are available in the Bash shell bash-example-1


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~walker/courses/161.fa11/modules/module-pointers-stacks-queues/reading-linux-scripts.shtml

created 21 April 2008
last revised 9 May 2009
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.