Similar presentations:
Shell Scripting
1.
Shell ScriptingWhat is Shell
Shell is a consistent buffer between the user and the Unix
system kernel
Shell is a program that reads the commands we type and
converts them into a form readable by the Unix system.
Shell is powerful tool that includes some fundamental
programming constructs that let to make decisions, loops, and
store values in variables
2.
Shell ScriptingProgram Execution
$ <program-name> <arguments>
Variable and Filename
Substitution
* ? [...] $ABC
I/O Redirection
< > >>
Pipeline Hookup
$ <prog1> | <prog2>
What is Shell
Environment Control
home, path, prompt,
variables
Interpreted Programming
Language
built-in programming
interpreted language:
analyzing each statement
of one line at a time and
executes it.
3.
Shell ScriptingBasic Commands
Some commands:
date
who
whoami
echo
Some commands for working with files:
ls
cat
more (less)
wc
cp
mv
rm
touch
rename
find
4.
Shell ScriptingBasic Commands
Some commands for working with directories:
pwd
cd
ls
mkdir
cp
ln
rmdir
Current directory
.
Parent directory
..
5.
Shell ScriptingBasic Commands
Substitutions:
*
- any number of any symbol
?
- any symbol
[abc]
- any of “a”, “b” or “c”
[p-u]
- a symbol from range “p, q, r, s, t, u”
[!0-9]
- a symbol not from the range of “0...9”
6.
Shell ScriptingStandard Input and
Standard Output
standard input
standard output
command
7.
Shell ScriptingStandard Input and
Standard Output
Standard input:
$ wc -w
Text to input
<Ctrl-d>
3
$ sort
karen
zaven
aram
<Ctrl-d>
aram
karen
zaven
8.
Shell ScriptingStandard Input and
Standard Output
Standard output:
$ users
armen
karen
gevorg
sergey
$ echo Hello World!
Hello World!
$ pwd
/workspace/shahen/
9.
Shell ScriptingStandard Input and
Standard Output
Redirecting output:
$ echo Hello World! > hello.txt
tee command equal
$ pwd > my_path
$ ls > file_list
$ users >> hello.txt
$ pwd | tee file_name
$ /workspace/shahen
$ cat file_name
$ /workspace/shahen
to 1>&file_name
10.
Shell ScriptingRedirecting input:
$ wc -w < users
4
$ sort < users
armen
gevorg
karen
sergey
Standard Input and
Standard Output
11.
Shell ScriptingUsing pipes:
$ ls | wc -w
9
$ users | sort
armen
gevorg
karen
sergey
Pipes and Filters
12.
Shell ScriptingSimple filter:
$ ls | grep user | sort -r
users
all_users
Pipes and Filters
13.
Shell ScriptingStandard Error
$ ls n*
ls: n*: No such file or directory
Redirecting standard error:
$ ls n* > error
ls: n*: No such file or directory
$ ls n* 2> error
0 – standard input
1 – standard output
2 – standard error
14.
Shell ScriptingMultiple commands:
More Than One
Command on a Line
$ date; pwd
Mon May 24 15:43:52 AMST 2005
/workspace/shahen/
$ sort all_users &
[1] 7354
$ command1 && command2
$ command1 || command2
Control operator &&:
command2 is executed only if command1 returns an exit status of zero
Control operator ||:
command2 is executed only if command1 returns a non-zero exit status
15.
Shell ScriptingProcesses
List of processes:
$ ps -e
6840
6844
6882
6894
6899
6947
7086
30112
30456
?
?
?
?
?
?
pts/4
pts/6
pts/6
00:00:02
00:00:05
00:00:00
00:00:00
00:00:57
00:01:26
00:00:00
00:00:00
00:00:00
gaim
skype
firefox
run-mozilla.sh
firefox-bin
soffice.bin
bash
bash
ps
16.
VariablesShell Scripting
Shell variable begins with an alphabetic or underscore
character and is followed by zero or more alphanumeric or
underscore characters
$ ABC=1291
$ _2=”test data”
$ echo $ABC
1291
$ echo $ABC_bugs
$ echo ${ABC}_bugs
1291_bugs
<WRONG!>
_
17.
Shell ScriptingFilename Substitution and Variables
$ X=*
$ echo $X
afile all_users badfiles err file1
file2 filea fileb garbage users
$ echo ”$X”
*
Shell does not perform filename substitution
when assigning values to variables
Variables
18.
Shell ScriptingIndirect expansion
$ X=123
$ Y=X
$ echo $Y
X
$ echo ${!Y}
123
Variables
19.
VariablesShell Scripting
Built-in Integer Arithmetic
$((expression))
let variable=expression
$ echo $((2+3))
5
$ let A=4+5
$ echo $A
9
$ echo $((A*3))
27
$ result=$(( A >= 0
$ echo $result
1
&&
A <= 100 ))
20.
Shell ScriptingVariables
Command substitution
$ U=users
$ echo `$U` # similar to echo `users`
karen sergey gevorg
$ `which python` --version
Python 2.7.6
$ NOW=$(date)
$ echo $NOW
Mon May 24 16:04:12 AMST 2005
$ echo "we have $(ls | wc -l) files"
we have 10 files
`command`
$(command)
21.
Shell ScriptingVariables
Single quotes and double quotes
$ X=*
$ echo $X
$ echo '$X'
$X
$ echo "$X"
*
$ Y="'Hello', he said"
$ echo $Y
'Hello' he said
$ Z='”Hi”, she answered'
$ echo $Z
”Hi”, she answered
Single quotes:
shell ignores all
characters enclosed
Double quotes:
shell ignores most
characters but:
• Dollar signs
• Back quotes
• Backslashes
22.
Shell ScriptingBackslash
$ echo
syntax
$ echo
>
$ X=*
$ echo
$X
Variables
Removes special meaning
>
error near unexpected token `newline'
\>
\$X
$ LINES=one\
> two
$ echo "$LINES"
onetwo
Used for continuing lines
23.
Shell ScriptingScripts
Script file ”usr_search”
#!/bin/bash
# let's search users by name
cd data
cat users.list | grep armen
The first line
#!/bin/bash
is not
a comment!
cd ..
$ chmod +x usr_search
Changing mode
(attribute) of the file
to executable
24.
DecisionsShell Scripting
Exit status
$? variable
$ cp users users.old
$ echo $?
0
If a program completes
execution, it returns
an exit status back to
the system.
$ cp list backup
No such file or directory
$ echo $?
1
This status is a number
that indicates whether
the program successfully
ran.
zero
nonzero
- succeeded
- failed
25.
Shell ScriptingDecisions
”if” statement
if <test_command>
then
<command1>
<command2>
...
else
<command3>
<command4>
...
fi
The command
<test_command>
is executed and its
exit status is tested
If the status is zero
the commands:
<command1>
<command2>
...
executed
26.
Shell Scripting”if” statement
$ if who | grep sergey > /dev/null
> then
> echo "Hi, Sergey"
> fi
Hi, Sergey
Decisions
27.
Shell ScriptingDecisions
”test” command
test <expression>
or
[ <expression> ]
$
$
$
0
$
$
0
USER=armen
test $USER = armen
echo $?
[ $USER != karen ]
echo $?
( expression )
expression is true
! expression
expression is false
expression1 -a expression2
both
expression1 and expression2
are true
expression1 -o expression2
expression1 or expression2
is true
28.
Shell ScriptingDecisions
”test” command
string1 = string2
strings are equal
integer1 -eq integer2
integer1 is equal to integer2
integer1 -ge integer2
integer1 is
greater than or equal
to integer2
integer1 -gt integer2
integer1 is greater than integer2
string1 != string2
strings are not equal
integer1 -le integer2
integer1 is less than or equal
to integer2
integer1 -lt integer2
integer1 is less than integer2
integer1 -ne integer2
integer1 is not equal to integer2
29.
Shell ScriptingDecisions
”for” statement
for <var> in <word1> <word2> ... <wordN>
do
<command1>
<command2>
...
done
$ for i in 1 2 3
> do
> echo $i
> done
1
2
3
30.
Shell Scripting$* - all arguments
#!/bin/bash
# my_cmd script
for FILE in $*
do
cat $FILE | grep karen
done
$ my_cmd users all_users
1:karen
22:karena
93:karensh
Decisions
31.
ArgumentsShell Scripting
$# - number of arguments
$1 - first argument, $2 - second argument . . .
#!/bin/bash
# args script
echo total: $#
for A in $*
do
echo $A
done
echo arg1: $1
echo arg2: $2
$ ./args a b c
total: 3
a
b
c
arg1: a
arg2: b
$
32.
DecisionsShell Scripting
”while” and ”until” statements
while <test_command>
do
<command1>
<command2>
...
done
while command continues
execution as long as
<test_command>
returns a zero exit status
until <test_command>
do
<command1>
<command2>
...
done
until command continues
execution as long as
<test_command>
returns a non-zero exit status
33.
DecisionsShell Scripting
”case” statement
case <value> in
<pat1>)
<command1>
...
<command2> ;;
<pat2>)
<command3>
...
<command4> ;;
...
esac
case
in
0)
1)
2)
3)
4)
5)
6)
7)
8)
9)
esac
"$1"
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
zero;;
one;;
two;;
three;;
four;;
five;;
six;;
seven;;
eight;;
nine;;
34.
Shell Scripting”read” command
read variables
read X Y Z
read TEXT
Input
Shell reads a line from
standard input and assigns
the first word read to the
first variable, the second
word read to the second
variable,
and so on.
If there are more words on
the line than there are
variables listed,
the excess words get
assigned to the last variable
35.
Shell ScriptingOutput
”printf” command
printf "<format>" <arg1> <arg2> ... <variables>
$ printf "This is a number: %d\n" 10
This is a number: 10
d Integers
u Unsigned integers
o Octal integers
x Hexadecimal integers, using a-f
c Single characters
s Literal strings
36.
Shell ScriptingAny Character
.
Beginning of Line
^
End of Line
$
Choice of Characters
[...]
Zero or More Characters
*
[A-Za-z][A-Za-z]* [-0-9] [a-z]
Regular Expressions
37.
Shell ScriptingDecisions
”$$” pid parent shell
“$!” pid last process running in background mode
$
$
$
$
$
$
myscript &
17865
echo $!
17865
echo $$
17860
38.
Shell ScriptingDecisions
function declaration
function myFunc ()
{
local a=9
return $(($a + $1))
}
Sum ()
{
b=3
return $(($1 + $2))
}
$ myFunc 10
$ 19
$ Sum 12 56
$ 68
“a” declared as a local
variable in “myFunc”
function
“b” declared as a global
variable in “Sum” function
39.
Shell Scriptingsed
$
$
$
$
and
Decisions
awk commands
sed “s/EXPORT/export/g” file_name > new_file_name
a=“ararat 1973”
echo $a | sed ‘s/\([a-z]*\).*/\1/g’
ararat
$ echo $a | awk ‘{print $2}’
$ 1973
$ echo $a | awk ‘{if ($1 == “ararat”) print $2}’