Sie sind auf Seite 1von 37

IBM Global Business Services

1 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

Introductions

 Your name

 Location – home office

 Job role

 How long with


[industry, practice area, community, etc.]

 Something unique about yourself

2 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

Basic Shell Scripting

Date: <Day> <Month> <Year>

Duration of course: 2 Days

© Copyright IBM Corporation 2009


IBM Global Business Services

Session objectives

By the end of this session, you should be able to:


 Discuss the history of Unix
 Discuss the features and architecture of Unix
 Discuss the Unix file system and the directory structure
 Work with file handling commands
 Work with vi editor
 Use Unix filters, piping mechanism and command
substitution
 Use the find command
 Use the xargs command

4 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

Course content (Day 1)

 Understand basic shell behavior.

 Know shell actions on commands.

 Use Pipes and Quoting.

 Handle shell programming techniques.

 Understand and modifying the environment of work.

 Practice shell programming

5 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

Housekeeping

 Breaks

 Washrooms

 Transportation/parking

 No pagers or cell phones

 Participation

 Parking lot issues

 Questions

6 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

Objectives of this Section

 Understand basic shell behavior.

 Know shell actions on commands.

 Use Pipes and Quoting.

 Handle shell programming techniques.

 Understand and modifying the environment of work.

 Practice shell programming

7 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

Basic Shell Behaviour

 Shell allows us to edit our command line


 Shell stores history of commands that were executed by user in current login
session and even previous sessions.
 When user types a command and presses enter key, shell does following
tasks:
- Shell parses command line arguments, looks for metacharacters, and if any
metacharacters are there, shell expands special meaning of these metacharacters.
- Then, the shell searches for the command in the following places:
1. List of Aliases
2. List of Shell keywords
3. List of user-defined functions
4. List of shell built-ins
5. List of directories specified in PATH variable

8 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

Create Shell Scripts

 Starting any shell script as a new file:


$ vi test.sh <Enter>

 Going to Insert mode and typing the shell Program in the vi Editor:

echo “Today’s date is:”


date
echo ”Have a Nice Day”

 Going to Command Mode and Saving the Script. Then exiting.

9 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

Execute a Shell Script

If Shell Script is in Current Directory

$ sh test.sh <Enter> Full Pathname is not mandatory

If Shell Script is not in Current Directory

$ sh /home/ss/prg/test.sh <Enter> Full Pathname is mandatory

If Shell Script is not given execution permission

File Owner and System


$ chmod +x /home/ss/prg/test.sh <Enter>
Administrator can do it.
$ sh /home/ss/prg/test.sh <Enter>

10 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

One Simple Shell Script


proccal.sh

echo “Welcome to Shell.”


echo “The Currently Running Processes Here Are:”
ps
echo “The Calendar of this Month is:”
cal apr
echo “Have a Fine Luck.”

11 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

Quoting
 Quote can be “ “ or ‘ ‘ or ` `
 Example of Usage:
Without Quotes:
$ echo * <Enter>
abc.txt s.sh test.c dir1

With Quotes:
$ echo “*” <Enter>
*

12 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

Pipe : Transferring the Output of One Command As Input to Another

$ ls -l | wc -l <Enter>

$ cal 1990 | more <Enter>

13 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

read : Preparing Shell Programs Interactive

Useread.sh

echo “String Matching and Displaying using read.”


echo “Enter the string to be found out: \c”
read strng
echo “Enter the File from which the string is to be found out: \c”
read flnm
echo “Now Searching for $strng from the file $flnm”
grep “$strng” $flnm
echo “The Required Data is as above.”
echo “Thank You.”

14 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

read : Preparing Shell Programs Interactive

Output of Useread.sh
$sh useread.sh <Enter>
String Matching and Displaying using read.
Enter the string to be found out:
IBM
Enter the File from which the string is to be found out:
profile.txt
Now Searching for IBM from the file profile.txt
My company name is IBM.
The Required Data is as above.
Thank You.

15 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

Some Environmental Variables

 PATH.

 HOME.

 SHELL.

 PS1.

 LOGNAME.

 TERM.

16 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

Displaying Individual Environmental Variable Values

$ echo $PATH <Enter>


/bin:/usr/bin:/home/sachin:.

$ echo $SHELL <Enter>


/bin/sh.

$ echo $LOGNAME <Enter>


sachin

$ echo $HOME <Enter>


/home/sachin

17 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

.profile : The File That Runs at User Login

 It is located in the logged in user’s home directory (e.g. /home/sachin).

 It’s like AUTOEXEC.BAT file of MS-DOS.

 User can edit it also


- To alter the environmental variable values, or
- To run some command at login.

18 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

.profile : Sample Simplified Content After Change

$ cat .profile <Enter>


HOME=/home/sachin/shscr
LOGNAME=sachin
PATH=/bin:/usr/bin:/home/sachin:/usr/lib:.
……..
PS1=‘SACHIN>’
SHELL=/bin/sh
……..
echo “Todays date is `date’”

19 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

Accepting Inputs as Command Line Arguments Using Positional Parameters

 Some Positional Parameters:


- $0 : Shell script name.
- $* : Command line arguments.
- $# : Number of command line arguments.
- $1 : First command line argument.
- $2 : Second command line argument.
- $3 : Third command line argument, and so on.
- $@: Command Line Arguments.

20 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

Command Substitution

$ echo “The number of files present in current directory are: `ls|wc –l`”
The number of files present in current directory are: 3

$ echo “Current month name is: `date +%h`”


Current month name is: Apr

21 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

exit : Exiting from Program


Several Variation of Usage:

exit

exit 1

exit 2

One Sample Usage:

grep “IBM” profile.txt || exit


echo “The Required Data is Displayed Above.”

22 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

$?: Exit Status of Previous Command

 Return value of any command is called the exit status of that command.

 UNIX Commands return:


- 0, on success.
- 1, on failure.

 Examples:

$ grep “IBM” profile.txt; echo $? <Enter>


0

$ grep “IDP” profile.txt; echo $? <Enter>


1

23 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

expr: Arithmetic Computation Using Integers

$ expr 4+2 <Enter>


6

$ p=6 q=9 <Enter>


$ expr $p + $q <Enter>
15

$ expr 9 – 6 <Enter>
3

$ p=6 q=9 <Enter>


$ expr $p \* $q <Enter>
54

24 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

expr: Arithmetic Computation Using Integers

$ r=3 s=9 <Enter>


$ expr $s / $r <Enter>
3

$ p=6 q=9 <Enter>


$ expr $p % $q <Enter>
6

$ i = 3 <Enter>
$ i = `expr $i + 2` <Enter>
$ echo “The New value of i is: $i <Enter>
5

25 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

test: Comparison of Two Numbers


Several Numeric Comparison Operators:

 -ne : Not Equal to

 -eq : Equal to

 -lt : Less Than

 -le : Less Than Equal To

 -gt : Greater Than

 -ge : Greater Than Equal To

26 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

test: Example of Comparison of Numbers

$ p=9; q=9.7; r=3


$ test $p –le $r ; echo $? (Or [ $p –le $r]; echo $? )
1
$ test $r –ne $p ; echo $?
0
$ test $p –eq $q ; echo $?
0
$ test $r –gt $p ; echo $?
1

27 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

test: Use in String Handling

Several String Handling Possibilities:

 string1 = string2 : Tests for matching of two strings.


 string1 != string2 : Tests for non-matching of two strings.
 string1 > string2 : Tests for the case that string1 is alphabetically after string2

 string1 < string2 : Tests for the case that string2 is alphabetically after string1

28 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

test: Example of Use in String Handling

$ str1 = ‘abc’ ; str2 = ‘def’ ; [ str1 = str2 ] ; echo $?


1

$ str1 = ‘ab’ ; str2 = ‘df’ ; [ str1 < str2 ] ; echo $?


0

29 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

if Statements : Example

ifexmpl.sh:
echo “Illustration of if Statements.”
if grep “IDP” profile.txt
then
echo “The Required Data is Obtained.”
else
echo “The Required Data is Not Obtained.”
echo “Better Luck Next Time.”
fi

30 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

if Statements : Execution of the Example


Execution of ifexmpl.sh:

$ sh ifexmpl.sh <Enter>
Illustration of if Statements.
The Required Data is Not Obtained.
Better Luck Next Time.

31 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

Case structure
Syntax

case “$var” in
opt1) <statement(s)> ;;
opt2) <statement(s)> ;;
opt3) <statement(s)> ;;

*) <statement(s)> ;;
esac

The variable’s value is evaluated and compared with every


option. The block following the matching option is
executed.

32 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

while loop
Syntax
while test <expression>
do
<set of statement(s)>
done

while is a looping structure. test command evaluates the


expression, and as per evaluation (true or false) stores a
value (zero or one) in the $? variable. While takes a
decision about taking control into loop block depending on
the value stored in $? variable. While repeats the block as
long as the expression is evaluated to true.

33 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

until loop
Syntax

until test <expression>


do
<set of statement(s)>
done

until is a looping structure. test command evaluates the


expression, and as per evaluation (true or false) stores a
value (zero or one) in the $? variable. until takes a
decision about taking control into loop block depending on
the value stored in $? variable. until repeats the block as
long as expression is evaluated to false.

34 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

for loop
Syntax

for variable <list of items>


do
<set of statement(s)>
done

for iterates over a list of items, and assigns the list of items
one by one to the variable, and executes the block for
each assignment. for loop terminates when it has iterated
over all the items in the list.

35 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

for : Another Way of Looping


forex.sh :

echo “Illustration of for Loop.”


echo “It will Show You Names of School Toppers for Previous 5 Years.”
yr=2002
for studname in Ramesh Madhavan Ruchi Iti Pankaj
do
echo “ The Name of Topper in Year $yr was $studname
yr = `expr $yr + 1`
done
echo “You Will Be Welcome Again Also.”

36 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009


IBM Global Business Services

for : Running the Example


Running forex.sh :

Illustration of for Loop.


It will Show You Names of School Toppers for Previous 5 Years.
Ramesh Madhavan Ruchi Iti Pankaj
The Name of Topper in Year 2002 was Ramesh
The Name of Topper in Year 2003 was Madhavan
The Name of Topper in Year 2004 was Ruchi
The Name of Topper in Year 2005 was Iti
The Name of Topper in Year 2006 was Pankaj
You Will Be Welcome Again Also.

37 IBM Internal Use | December 9, 2021 © Copyright IBM Corporation 2009

Das könnte Ihnen auch gefallen