Standard IO and Pipes
& Regular Expressions
grep
• To searches file for line that match a pattern.
grep {word-to-lookup} {filename} $ grep foxmyfile
Grep ‘[Cc]at’ pets
• Linux provides three I/O channels to programs
– Standard input (STDIN) – Keyboard by default
– Standard Output (STDOUT) –terminal window def
– Standard error (STDERR) – terminal window def
• By default standard output is the screen or
terminal window but when output is sent to a
destination other than screen , such as a file then
it means you are redirecting the standard output.
• Consider the following command
• Ls –l myfile fakefile
(the first line of this output is directed to the
standard error stream and the second to the
standard output)
These streams are abbreviated as
– STDIN (file descriptor #0)
– STDOUT(file descriptor #2)
• Supported operators include:
► > Redirect STDOUT to file
► 2> Redirect STDERR to file
► &> Redirect all output to file
Note: ( The standard output command can be redirected to a
file or piped in to another command but standard error
redirects to only file)
Common Redirection Operators
• Command > file - Direct standard output to file
• Command >> file – Append standard output of command to file
• Command < file – Send file as input to command
• Command 2> file – Redirects error message from command to
file
• Command 2>> file – Append error messages from command to
file
Standard Output
• ls > file_list.txt
In this example, the ls command is executed and
the results are written in a file named file_list.txt.
• ls >> file_list.txt
If you want the new results to be appended to
the file instead, use ">>" like this:
Standard Input
• sort < file_list.txt
In the above example we used
the sort command to process the contents of
file_list.txt.
• sort < file_list.txt > sorted_file_list.txt
As you can see, a command can have both its
input and output redirected.
• spell < text.txt > err.txt
PIPES
• Pipes ( the | character) can connect
commands:
Syntax:
Command1 | Command2
Example:
cat people-I-like | sort
Filter
• If a Linux command accepts its input from the
standard input and produces its output on
standard output is know as a filter.
$ tail +20 < hotel.txt | head -n30 >hlist
Introduction to Shell Programming
Variables in Linux
• In Linux, there are two types of variable
1) System variables- Created and maintained by
Linux itself. This type of variable defined in
CAPITAL LETTERS.
2) User defined variables (UDV)- Created and
maintained by user. This type of variable defined
in lower LETTERS.
Some System variables
User defined variables (UDV)
• Syntax:
variablename=value
print or access value of UDV-
echo $variable name
Eg:
$ x=20
$ y=5
$ z=`expr x / y`
$ echo $z
Commands Related with Shell Programming
echo [options] [string, variables...]
-n Do not output the trailing new line.
-e Enable interpretation of the following backslash escaped characters in the
strings:
\a alert (bell)
\b backspace
\c suppress trailing new line
\n new line
\r carriage return
\t horizontal tab
\\ backslash
For eg.
$ echo -e "An apple a day keeps away \a\t\tdoctor\n"
More about Quotes
• There are three types of quotes
– " i.e. Double Quotes
– ' i.e. Single quotes
– ` i.e. Back quote
1."Double Quotes" - Anything enclose in double quotes
removed meaning of that characters (except\ and $).
2. 'Single quotes' - Enclosed in single quotes remains
unchanged.
3. `Back quote` - To execute command.
For eg.
$ echo "Today is date"
Can't print message with today's date.
$ echo "Today is `date`".
Now it will print today's date as, Today is Tue Jan ....,
Shell Arithmetic
• $ expr 1 + 3
• $ expr 2 - 1
• $ expr 10 / 2
• $ expr 20 % 3 # remainder read as 20 mod 3
and remainder is 2)
• $ expr 10 \* 3 # Multiplication use \* not *
since its wild card)
• $ echo `expr 6 + 3`
Command line arguments
• The arguments are stored in variables with a number in the order
of the argument starting at 1
• First Argument: $1
• Second Argument: $2
• Third Argument: $3
• Example
– command: ./script.bash alpha beta gamma
– Variables: $1=='alpha'; $2=='beta'; $3=='gamma'
The variable $0 is the script's name. The total number of arguments
is stored in $#. The variables $@ and $* return all the arguments.
• $ ls -a /*
$ cat > demo
#!/bin/sh
#
# Script that demos, command line args
#
echo "Total number of command line argument are $#"
echo "$0 is script name"
echo "$1 is first argument"
echo “$2 is second argument"
echo "All of them are :- $*“
Save the above script by pressing ctrl+d, now make it executable
$ chmod +x demo
$ ./demo Hello World
$ cp demo ~/bin
$ demo
• Script Example
#!/bin/bash
echo "the $1 eats a $2 every time there is a $3"
echo "bye:-)“
Command: ./script.sh dog bone moose
For Mathematics use following operator in
Shell Script
For string Comparisons use
Logical Operators
Exit Status
• By default in Linux if particular command is
executed, it return two type of values, (Values
are used to see whether command is
successful or not)
• if return value is zero (0), command is
successful,
• if return value is nonzero (>0), command is
not successful or some sort of error executing
command/shell script.
• Eg.:
$ ls
$ echo $?
Try the following commands and not down there
exit status
$ expr 1 + 3
$ echo $?
$ echo Welcome
$ echo $?
$ wildwest canwork?
$ echo $?
$ date
$ echo $?
if...else...fi
Syntax:
if condition
then
command1 if condition is true or if exit status
of condition is 0(zero)
...
...
else
command2 if condition is false or if exit status
of condition is >0 (nonzero)
...
...
fi
$ cat > showfile
#!/bin/sh
#Script to print file
if cat $1
then
echo -e "\n\nFile $1, found and successfully echoed"
fi
Now try to write answer for following
1) Create following script
if rm $1
then
echo "$1 file deleted"
fi
(Press Ctrl + d to save)
$ chmod +x trmif
Loops in Shell Scripts
for loop Syntax:
for { variable name } in { list }
do
execute one for each item in the list until the list is not
finished (And repeat all statement between do and done)
done
• Suppose,
$ cat > testfor
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
while loop
Syntax:
while [ condition ]
do
command1
command2
command3
..
....
done
we used 'exit 1' command which takes 1 as
$cat > nt1
argument (1Indicates error
#!/bin/sh
and therefore script is terminated)
#
#Script to test while statement
#
if [ $# -eq 0 ]
then
echo "Error - Number missing form command line argument"
echo "Syntax : $0 number"
exit 1
fi
n=$1
i=1
while [ $i -le 10 ]
do
echo "$n * $i = `expr $i \* $n`"
i=`expr $i + 1`
done
The read Statement
• Use to get input from keyboard and store them to
variable.
$ cat > sayH
#
#Script to read your name from key-board
#
echo "Your first name please:"
read fname
echo "Hello $fname, Lets be friend!"
Functions
• Function is series of instruction/commands. Function performs
particular activity in shell.
Syntax:
function-name ( )
{
command1
command2
.....
...
commandN
return
}