Linux 06
Linux 06
Shell Programming
Shell Programming
We often want to do a number of commands together
And bundle them up into one new command.
Just like a batch file in MS-DOS
Shell scripts
C Shell
Invoking scripts
C Shell
Shell Variables
Environment Variables
Used to provide information to programs
setenv
$ setenv NAME value # in C Shell
set
$ set name = value # in C Shell
Variables
To set variables:
set X [= value] # processed as a string
C Shell
Array
To create lists:
set Y = (abc 1 123)
C Shell
Built-in Variables
$user -- who am I?
$path -- my execution path (list of directories to be searched
for executables)
$term -- what kind of terminal I am using
$status -- a numeric variable, usually used to retun error
codes
$prompt -- what I am currently using for a prompt
$shell -- which shell am I using (usu. either /bin/csh or
/bin/sh)
% set
Will display the variable lists.
Arithmetic (@) command
C shell provides arithmetic operators
must be used with the arithmetic (@)
command
Arithmetic command works only with
integers.
set count = 5
@ count += 2
echo $count
7
Shell Arithmetic
expr op1 math-operator op2
Example
% expr 1 + 3
% expr 10 \* 3
% set A = `expr 3 + $B`
Command arguments
A shell script to swap files:
#! /bin/csh
set tmp = $argv[1]
cp $argv[1] tmp_file
cp $argv[2] $argv[1]
cp tmp_file $argv[2]
Arguments : $argv
The number of arguments to a script: $#argv
C Shell
if-then-else
if ( expr ) simple-command
if ( expr ) then
commandlist-1
[else
commandlist-2]
endif
C Shell
if-then-else exampe
#! /bin/csh
if ($#argv != 2) then
echo "we need two parameters!"
else
set tmp = $argv[1]
cp $argv[1] tmp_file
cp $argv[2] $argv[1]
cp tmp_file $argv[2]
endif
C Shell
Loops
while ( expr )
commandlist
end
C Shell
switch
switch ( str )
case string1:
commandlist1
breaksw
case string2:
commandlist2
breaksw
default
commandlist
endsw
C Shell
goto (Considered harmful!)
To jump unconditionally:
goto label
C Shell
shift command
Moves the values in the parameters
toward the beginning of the parameter
list
#!/bin/csh –f
echo “\n”
echo “There are now” $#argv “parameters”
echo “end of script”
C Shell
Input
Reading Line by Line
% set x = $<
This is a line.
% echo $x
This is a line.
File Operators
-e file : True if file exists
-r file : True if file is readable
-l file : True if file exists and is a symbolic link
-w file : True if file exists and is writable
-x file : True if file exists and is executable
-o file : True if the user owns it
-f file : True if the file exists and is a regular file
-d file : True if the file exists and is a directory
-s file : True if file exists and has a size greater than zero
-z file : True if file length is zero (empty)
Logical operator
! : NEGATE
&& : logical AND
|| : logical OR
Ex)
if (! -e somefile) then
# does not exist
Debugging
%csh –vx somescript args
-v : vervose
-x : echoes the commands after all substitutions are
made
-n : syntax check. No execution
example
#!/bin/csh
if (-e $argv[1]) then
echo $argv[1] exists
else
echo $argv[1] does not exist and cannot be opened
endif
# rest of script here
C Shell
example
#!/bin/csh
set sum = 0
C Shell
Guidelines
Shell script is better than C program if the problem
can be solved by using UNIX commands
Why script?
Easier to create and modify
Easy to debug
Good thing to do
Use redirection and pipe
Do validity check (argument number , type)
Check existence of files and directories
Display error messages
example
#!/bin/csh
set j = (1 2 3 4 5)
foreach i ($j)
echo $i Hello
end
C Shell
example
#!/bin/csh
C Shell
Numeric validation
example
#!/bin/csh
if ($status != 0) then
echo “Month argument is not numeric”
exit 1
endif
C Shell
example
#! /bin/csh -f
foreach name ($argv)
if ( -f $name ) then
echo -n "delete the file '${name}' (y/n/q)?"
else
echo -n "delete the entire dir '${name}' (y/n/q)? "
endif
set ans = $< # $< means “read a line”
switch ($ans)
case n:
continue
case q:
exit
case y:
rm -rf $name
continue
endsw
end
C Shell
Exercise 1
format
% ./fd_count.csh directory_name
Exercise 2
Write a shell script that removes
duplicate words from an input text file.
Format
% remove_dup.csh in.txt out.txt
Four
Two
One Four Two One Three
One out.txt
Four
Two
in.txt
Two
Three
Bash Shell
• Shell beginning
#!/bin/bash
# This is the beginning of a shell script
• Setting Variables
> MYVAR=“a new variable”
> echo $MYVAR
A new variable
> x=3
> echo $x
3
> export seven=7
> echo $seven
7
Command Output
In order to use the output of a command within
our script, we must set it apart using backticks
`command`
Or $(command)
#!/bin/bash
echo date
echo `date`
echo $(date)
Test
To test an expression, use one of
these :
test EXPRESSION
[EXPRESSION]
Returns 0 if true, 1 if false
File Tests
Control Structures
Example
#! /bin/bash
if [ $? -eq 0 ]
then echo "Last command exited cleanly!"
fi
#! /bin/bash
if [ $? -eq 0 ]
then echo "Last command exited cleanly!"
else echo "Uh-oh - non-zero exit status!"
fi
Arguments
$1, $2, $3, etc.
each argument passed to our script is
assigned a variable
$0
stores the name of the script
$#
gives us the number of arguments
#! /bin/bash
if [ ! $# -eq 3 ]
then echo "Correct usage: $0 arg1 arg2 arg3"
exit
fi
Arithmetics
Arithmetics
Let built-in
let VAR=$1+15
$[expression]
echo $[ 323*17 ]
Arrays
declare -a arrayname
arrayname[index_number]=value
arrayname=(value1 value2 ... valueN)
Example1
#! /bin/bash
for FILE in `ls *.txt`; do cp $FILE $FILE.bak; done
function
Shell functions are a way to group
commands together for later execution,
using a single name for the group.
Function syntax
function name { commands; }
name() { commands; }
Local/Global Variables
Example
#! /bin/bash
VAR=”global variable”
function func {
local VAR=”local variable”
echo $VAR; }
# Execute our new function!
func
echo $VAR
Function Parameters
Example
#! /bin/bash
function function_A {
echo $1; }
function_A “A function parameter!”
echo $1