Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Introduction to Shell Programming - Lecture Slides | CS 333, Study notes of Computer Science

Material Type: Notes; Class: UNIX Operating Sys Fundamental; Subject: Computer Science; University: University of Alabama - Birmingham; Term: Fall 2006;

Typology: Study notes

2009/2010

Uploaded on 04/12/2010

koofers-user-np6
koofers-user-np6 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS
CS-
-344
344 -
-Unix Operating
Unix Operating
System Fundamentals
System Fundamentals
Lecture 10
Introduction to Shell Programming
Based on slides created by
Dr. Bangalore for the
Spring 2005 offering of
the course
11/3/2006 3
Department of Com
puter and Informati
on Sciences
UAB
Shell: Turn Interpretation Off
Shell: Turn Interpretation Off
๎˜
Shell interprets characters entered at the
command prompt
๎˜
To turn off this interpretation we must use
๎˜‚
Backslash โ€“ \
๎˜‚
Double quotes โ€“ โ€ โ€
๎˜‚
Single quotes โ€“ โ€™ โ€™
๎˜
What happens when the following commands are
used
๎˜‚
echo โ€™$HOMEโ€™ โ€$HOMEโ€ \$HOME
๎˜‚
echo My files are: โ€*โ€
๎˜‚
echo โ€My files are: *โ€
๎˜‚
echo โ€™My files are: *โ€™
11/3/2006 4
Department of Com
puter and Informati
on Sciences
UAB
Effect of Quoting on Special Characters
Effect of Quoting on Special Characters
NONONOnewline
NONOYES!
NONONOsp aces
NONOYES` (back quote)
NONO-โ€ (double quote)
NO-NOโ€™ (single quote)
NONOYES$
NONONO;
NONONO>
NONONO|
NONONO<
NONONO{ }
NONONO[ ]
NONONO*
After \Inside โ€™ โ€™Inside โ€ โ€Character
11/3/2006 5
Department of Com
puter and Informati
on Sciences
UAB
Summary
Summary
๎˜
For the bash shell
๎˜‚
The backslash character and single quotes turn off
interpretation for all special characters
๎˜‚
The double quotes turn off interpretation of all special
characters except $, `, and \ (\ - only if the next
character is interpreted)
๎˜
For the C shell
๎˜‚
The backslash character turns off interpretation for all
special characters
๎˜‚
The single quotes turn off interpretation of all special
characters except the !
๎˜‚
The double quotes turn off interpretation of all special
characters except $, `, and !
11/3/2006 6
Department of Com
puter and Informati
on Sciences
UAB
Miscellaneous
Miscellaneous
๎˜
Passing Special Characters to Utilities
๎˜‚
grep treats $ character as end of line (e.g.,
grep ';$' MyClass.java
grep ';$' MyClass.java
โ€“ look for lines ending with ;)
๎˜
Single and double quotes can be mixed in
commands (only outer quotes have any effect)
๎˜‚
echo โ€™ โ€$USERโ€ โ€™
๎˜‚
echo โ€ โ€™$USERโ€™ โ€
๎˜‚
echo โ€™$USERโ€*?โ€™ $USER โ€™ โ€$USERโ€โ€™
๎˜‚
echo โ€™*$USER \โ€™ โ€Date is \โ€ `date` \โ€ $USER \*\โ€
๎˜
Interpreting special characters in variable names
$ aa=t*
$ echo $aa '$aa' "$aa"
t.c t1.c temp.java tmp typescript $aa t*
$ aa=โ€™t*โ€™
$ echo $aa '$aa' "$aa"
t.c t1.c temp.java tmp typescript $aa t*
pf3

Partial preview of the text

Download Introduction to Shell Programming - Lecture Slides | CS 333 and more Study notes Computer Science in PDF only on Docsity!

CS CS-- 344344 - - Unix OperatingUnix Operating

System Fundamentals System Fundamentals

Lecture 10

Introduction to Shell Programming

Based on slides created by

Dr. Bangalore for the

Spring 2005 offering of

the course

11/3/2006 3 Department of Com

puter and Informati

on Sciences

UAB

Shell: Turn Interpretation OffShell: Turn Interpretation Off

 Shell interprets characters entered at the

command prompt

 To turn off this interpretation we must use

 Backslash โ€“
 Double quotes โ€“ โ€ โ€  Single quotes โ€“ โ€™ โ€™

 What happens when the following commands are

used

 echo โ€™$HOMEโ€™ โ€$HOMEโ€ $HOME  echo My files are: โ€*โ€  echo โ€My files are: *โ€  echo โ€™My files are: *โ€™

11/3/2006 4 Department of Com

puter and Informati

on Sciences

UABEffect of Quoting on Special Characters Effect of Quoting on Special Characters

newline NO NO NO ! YES NO NO

spaces NO NO NO

` (back quote) YES NO NO

โ€ (double quote) - NO NO

โ€™ (single quote) NO - NO

$ YES NO NO

; NO NO NO

NO NO NO

| NO NO NO

< NO NO NO

{ } NO NO NO

[ ] NO NO NO

  • NO NO NO

Character Inside โ€ โ€ Inside โ€™ โ€™ After \

11/3/2006 5 Department of Com

puter and Informati

on Sciences

UAB

SummarySummary

 For the bash shell

 The backslash character and single quotes turn off interpretation for all special characters  The double quotes turn off interpretation of all special characters except $, `, and \ (\ - only if the next character is interpreted)

 For the C shell

 The backslash character turns off interpretation for all special characters  The single quotes turn off interpretation of all special characters except the!  The double quotes turn off interpretation of all special characters except $, `, and!

11/3/2006 6 Department of Com

puter and Informati

on Sciences

UAB

Miscellaneous Miscellaneous

 Passing Special Characters to Utilities

 grep treats $ character as end of line (e.g., grep ';$' MyClass.java grep ';$' MyClass.java โ€“ look for lines ending with ;)

 Single and double quotes can be mixed in

commands (only outer quotes have any effect)

 echo โ€™ โ€$USERโ€ โ€™  echo โ€ โ€™$USERโ€™ โ€  echo โ€™$USERโ€?โ€™ $USER โ€™ โ€$USERโ€โ€™  echo โ€™$USER \โ€™ โ€Date is \โ€ date \โ€ $USER *\โ€

 Interpreting special characters in variable names

$ aa=t* $ echo $aa '$aa' "$aa" t.c t1.c temp.java tmp typescript $aa t*

$ aa=โ€™tโ€™ $ echo $aa '$aa' "$aa" t.c t1.c temp.java tmp typescript $aa t

11/3/2006 7 Department of Com

puter and Informati

on Sciences

UAB

โ€œโ€œechoechoโ€โ€^ commandcommand

 Using โ€ โ€ and โ€™ โ€™ with echoecho

$ echo ' $ echo 'HelloHello $USER$USER' Hello $USER $ echo "Hello $ echo "Hello $USER$USER"" Hello afgane

 When the shell interprets โ€™โ€™ it stops interpretation

until a matching โ€™โ€™ is found

 To eliminate new line with echoecho use

echo echo โ€“ โ€“n n echo echo (^) โ€“ โ€“nn โ€โ€Enter your choice:Enter your choice: โ€โ€; echo; echo (^) โ€œโ€œ1, 2, 31, 2, 3โ€โ€

Shell Programming Shell Programming

11/3/2006 9 Department of Com

puter and Informati

on Sciences

UAB

IntroductionIntroduction

 Used for storing commonly used sequences of commands

 So, a series of shell commands can be stored in a regular text file for later

execution

 Before running a script, need to give it

execute permissions with chmod utility

11/3/2006 10 Department of Com

puter and Informati

on Sciences

UAB

Determining shell for a Script Determining shell for a Script

 When script is run, the system determines which shell the script was written for and then executes the shell using the shell as stdin  The system knows which shell to use to execute the script by examining the first line of the script:  If the first line is just a #, the script is interpreted by the shell from which it is executed  If the first line is of the form #! pathName, then the executable program pathName is used to interpret the script  If neither if the two rules apply, then the script is interpreted by a Bourne shell.

$ cat myScript.sh

Print todayโ€™s date

echo โ€“n The date today is date

$ cat myBashScript.sh #! /bin/bash

Print todayโ€™s date

echo โ€“n The date today is date

$ cat myDefaultScript.sh

Print todayโ€™s date

echo โ€“n The date today is date

11/3/2006 11 Department of Com

puter and Informati

on Sciences

UAB

Passing Arguments to a ScriptPassing Arguments to a Script

 Similar to passing command-line arguments in

other programming languages, we can pass

arguments to a shell script

Example: ./myscript arg1 arg2./myscript arg1 arg

 Shell interprets the arguments are follows:

 $1 โ€“ argument 1  $2 โ€“ argument 2 and so on  $0 โ€“ the name of the current script  $* โ€“ all arguments  $# โ€“ no. of arguments  $$ โ€“ process id (PID) of process running the script

11/3/2006 12 Department of Com

puter and Informati

on Sciences

UAB

Example: script with complex arguments Example: script with complex arguments

$ cat > cmdscript.sh echo 'The name of the script is: ' $ echo 'No. of command-line arguments = ' $# echo 'First three arguments are: ' $1 $2 $ echo 'Complete argument list is:' $* echo 'PID of this process = ' $$ $ chmod + cmdscript.sh

$ ./ cmdscript.sh โ€Hello Worldโ€ โ€$USERโ€ โ€™$USERโ€™ _The name of the script is: ./cmdscript.sh No. of command-line arguments = 3 First three arguments are: Hello World afgane $USER Complete argument list is: Hello World afgane $USER* PID of this process = 5828_ $ ./ cmdscript.sh a b c The name of the script is: ./cmdscript.sh No. of command-line arguments = 3 First three arguments are: a b c Complete argument list is: a b c PID of this process = 5831