You are on page 1of 19

Lab 7: Introduction to Bash

Scripts -2

Operating System Concepts 9th Edition


Ask the user for Input
If we would like to ask the user for input then we use a command called read.
This command takes the input and will save it into a variable.

Operating System Concepts 9th Edition


More with Read
We can alter the behaviour of read with a variety of command line options. Two commonly
used options however are -p which allows you to specify a prompt and -s which makes the
input silent. This can make it easy to ask for a username and password combination like the
example below:

with Read
You are able to alter the behaviour of read with a variety of command line
options. (See the man page for read to see all of them.) Two
commonly used options however are -p which allows you to specify a
prompt and -s which makes the input silent. This can make it easy to
ask for a username and password combination like the example below:

Operating System Concepts 9th Edition


More Variables with Read
The general mechanism is that you can supply several variable names to read. Read
will then take your input and split it on whitespace. The first item will then be assigned
to the first variable name, the second item to the second variable name and so on.

If there are more items than variable names then the remaining items will all be added
to the last variable name. If there are less items than variable names then the
remaining variable names will be set to blank or null.

Operating System Concepts 9th Edition


More Variables with Read

Operating System Concepts 9th Edition


Reading from STDIN
It's common in Linux to pipe a series of simple, single purpose commands together to create
a larger solution tailored to our exact needs. The ability to do this is one of the real
strengths of Linux. It turns out that we can easily accommodate this mechanism with our
scripts also.

Bash accommodates piping and redirection by way of special files. Each process gets it's
own set of files (one for STDIN, STDOUT and STDERR respectively) and they are linked when
piping or redirection is invoked. Each process gets the following files:

• STDIN - /dev/stdin or /proc/self/fd/0


• STDOUT - /dev/stdout or /proc/self/fd/1
• STDERR - /dev/stderr or /proc/self/fd/2

So if we would like to make our script able to process data that is piped to it all we need to
do is read the relevant file. All of the files mentioned above behave like normal files.

Operating System Concepts 9th Edition


Reading from STDIN

Operating System Concepts 9th Edition


Cut command in Linux
The cut command in UNIX is a command for cutting out the sections from each line of
files and writing the result to standard output. It can be used to cut parts of a line
by byte position, character and field. Basically the cut command slices a line and
extracts the text. It is necessary to specify option with command otherwise it gives
error.

Let us consider a file having name state.txt that contains 5 names of the Indian states.

1. -b(byte): To extract the specific bytes, you need to follow -b option with the list of
byte numbers separated by comma. Range of bytes can also be specified using the
hyphen(-). It is necessary to specify list of byte numbers otherwise it gives error.

Operating System Concepts 9th Edition


Cut command in Linux

2. -c (column): To cut by character use the -c option. This selects the characters given
to the -c option. This can be a list of numbers separated comma or a range of numbers
separated by hyphen(-).

Operating System Concepts 9th Edition


Cut command in Linux

Operating System Concepts 9th Edition


Cut command in Linux
3. -f (field): -c option is useful for fixed-length lines. Most Unix files doesn’t have fixed-
length lines. To extract the useful information you need to cut by fields rather than
columns. List of the fields number specified must be separated by comma. Ranges are
not described with -f option. cut uses tab as a default field delimiter but can also work
with other delimiter by using -d option.

Operating System Concepts 9th Edition


Cut command in Linux

Operating System Concepts 9th Edition


Write a Script
• Create a simple script which will ask the user for a few pieces of information then
combine this into a message which is echo'd to the screen.

• Add to the previous script to add in some data coming from command line
arguments.
 
• Create a script which will take data from STDIN and print the 3rd line only.

• Now play about with creating a script which will behave as a filter. Create a script
which will rearrange the output of the command ls -l in a useful way (eg maybe
you only print the filename, size and owner) 

Operating System Concepts 9th Edition


Lab Problem 1 Solution

Operating System Concepts 9th Edition


Lab Problem 2 Solution

Operating System Concepts 9th Edition


Lab Problem 3 Solution

Operating System Concepts 9th Edition


Lab Problem 4 Solution

Operating System Concepts 9th Edition


Awk Command in Unix
AWK Operations:
(a) Scans a file line by line
(b) Splits each input line into fields
(c) Compares input line/fields to pattern
(d) Performs action(s) on matched lines

Example:
Consider the following text file as the input file for all cases below.

Default behavior of Awk : By


default Awk prints every line of
data from the specified file. In
the example below, no pattern
is given. So the ‘print’ action is
applicable to all the lines.

Operating System Concepts 9th Edition


Awk Command in Unix
 Print the lines which matches with the given pattern.

Spliting a Line Into Fields : For each record i.e line, the awk command splits the record
delimited by whitespace character by default and stores it in the $n variables. If the line
has 4 words, it will be stored in $1, $2, $3 and $4 respectively. Also, $0 represents the
whole line. NR = Display the line number

Operating System Concepts 9th Edition

You might also like