0% found this document useful (0 votes)
47 views32 pages

Content: - Shells and Shell Scripts - TCSH, Enhanced C-Shell - Bash, Bourne-Again Shell

The document discusses shells and shell scripts. It provides information on popular shells like tcsh (enhanced C-shell) and bash (Bourne-Again Shell). It also discusses what shell scripts are used for, including to automate tasks and as equivalents to batch files in Windows/DOS. Key features of shell scripts are that they are made up of standard commands combined with control flow structures and variables, and are interpreted by shells.

Uploaded by

imran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views32 pages

Content: - Shells and Shell Scripts - TCSH, Enhanced C-Shell - Bash, Bourne-Again Shell

The document discusses shells and shell scripts. It provides information on popular shells like tcsh (enhanced C-shell) and bash (Bourne-Again Shell). It also discusses what shell scripts are used for, including to automate tasks and as equivalents to batch files in Windows/DOS. Key features of shell scripts are that they are made up of standard commands combined with control flow structures and variables, and are interpreted by shells.

Uploaded by

imran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Content

• Shells and Shell Scripts


• tcsh, enhanced C-Shell
• bash, Bourne-Again Shell

July 17, 2003 Serguei A. Mokhov, 1


[email protected]
Shell Commands
• Shell commands are interpreted directly by the
shell you specify.
• The commands are similar to the statement in
some programming languages, such as C.
• Popular shells include:
– Enhanced C-shell tchs (csh+)
– Bourne-Again Shell, bash (sh+)
– Korn Shell (ksh)
• These notes will focus on the first two shells.

July 17, 2003 Serguei A. Mokhov, 2


[email protected]
Shells’ Features
• The bash an tcsh shells are similar in the
features the offer. In particular:
– Pass arguments to your script
– Set and reference variables
– Use of control flow
– Interact with the user (read user input)
– Comments…
• Info on commands a given shell offers can be
found in the man pages for that shell.
• There are many Linux/UNIX references that give
detailed information and tips.
July 17, 2003 Serguei A. Mokhov, 3
[email protected]
Shell Scripts
• What are they for?
– To automate certain common activities an user
performs routinely.
– They serve the same purpose as batch files in
DOS/Windows.
– Example:
• rename 1000 files from upper case to lowercase

July 17, 2003 Serguei A. Mokhov, 4


[email protected]
What are Shell Scripts
• Just text/ASCII files with:
– a set of standard UNIX/Linux commands (ls, mv, cp,
less, cat, etc.) along with
• flow of control
– some conditional logic and branching (if-then),
– loop structures (foreach, for, while), and
• I/O facilities (echo, print, set, ...).
– They allow use of variables.
– They are interpreted by a shell directly.
– Some of them (csh, tcsh) share some of C syntax.
– DOS/Win equivalent - batch files (.bat)
July 17, 2003 Serguei A. Mokhov, 5
[email protected]
Why not use C/C++ for that?
• C/C++ programming requires compilation
and linkage, maybe libraries, which may not
be available (production servers).
• For the typical tasks much faster in
development, debugging, and maintenance
(because they are interpreted and do not
require compilation).

July 17, 2003 Serguei A. Mokhov, 6


[email protected]
Shell Script Invocation
• Specify the shell directly:
– % tcsh myshellscript
– % tcsh -v myshellscript
(-v = verbose, useful for debugging)
• Make the shell an executable first and then run is a
command (set up an execution permission):
– % chmod u+x myshellscript
• Then either this:
– % myshellscript
(if the path variable has ‘.’ in it; security issue!)
• Or:
– % ./myshellscript
(should always work)
July 17, 2003 Serguei A. Mokhov, 7
[email protected]
Shell Script Invocation (2)
• If you get an error:
“myshellscrip: command not found”
– The probably “.” is not in your path or there’s no
execution bit set.
• When writing scripts, choose unique names, that
preferably do not match system commands.
– Bad name would be test for example, since there are
many shells with this internal command.
• To disambiguate, always precede the shell with
“./” or absolute path in case you have to name
your thing not very creatively.
July 17, 2003 Serguei A. Mokhov, 8
[email protected]
Start Writing a Shell Script
• The very first line, often called 'shebang' (#!) should
precede any other line, to assure that the right shell is
invoked.
#!/bin/tcsh #!/bin/bash
# This is for tcsh # For Bourne-Again Shell

#!/bin/sh
# This is for Bourne Shell

• Comments start with '#', with the exception of #!, $#,


which are a special character sequences.
• Everything on a line after # is ignored if # is not a part
of a quoted string or a special character sequence.
July 17, 2003 Serguei A. Mokhov, 9
[email protected]
tchs
Quick Ref

July 17, 2003 Serguei A. Mokhov, 10


[email protected]
Variables
• Variables start with a $ sign when they are used.
– $x, $val
• There's no $ when a variable is declared.
– set x = 3
– @ y = 1
– set input = "$<"
• There are some system, predefined variables:
– $0, $1, $3 .... - argument references (arguments themselves)
– $* - all the arguments
– $< - user's input from STDIN
– $# - # of arguments passed to the script

July 17, 2003 Serguei A. Mokhov, 11


[email protected]
if
if ( <expression> ) then
<statements>
else if ( <another-expression> ) then
<statements>
else
<statements>
endif

July 17, 2003 Serguei A. Mokhov, 12


[email protected]
foreach
foreach var ( <list-of-values> )
<statements>
end

July 17, 2003 Serguei A. Mokhov, 13


[email protected]
switch
switch ( string )
case str1:
<statements>
breaksw
...
default:
<statements>
breaksw
endsw

July 17, 2003 Serguei A. Mokhov, 14


[email protected]
while
while ( <expression> )
<statements>
end

July 17, 2003 Serguei A. Mokhov, 15


[email protected]
File Inquiry Operators:
-op file
r Read access f Plain file
w Write access d Directory
x Execute access l Symbolic link
e Existence b Block special file
o Ownership c Character special file
z Zero size p Named pipe (FIFO)
s Non-zero size S Socket special file

July 17, 2003 Serguei A. Mokhov, 16


[email protected]
Example
• See creator and uptolow.
• NOTE: run them in a some temporary
directory to do not mess with your own
valuable files.
• The uptolow script:
– will convert any uppercase letters in an
ordinary file name to lowercase.
– will leave directories untouched.
July 17, 2003 Serguei A. Mokhov, 17
[email protected]
Bourne Shell
Quick Ref

July 17, 2003 Serguei A. Mokhov, 18


[email protected]
Quick Note
• In no way this going to be a duplication for
the zillions of resources on Bourne Shell,
but more a quick reference/syntax for most
often used constructs and pointers to
resources where else to find that kind of
stuff. Some of it is a lame reap off the man
page and so on.

July 17, 2003 Serguei A. Mokhov, 19


[email protected]
Quick Resource Summary
• Manual Pages:
man bash

• An Intro to UNIX Shell:


<http://steve-parker.org/sh/bourne.html>

• How To Write a Shell Script:


<http://www.tinker.ncsu.edu/LEGO/shell_help.html>

July 17, 2003 Serguei A. Mokhov, 20


[email protected]
Bourne Shell Script Constructs
Reference
• System/Internal Variables
• Control Flow (if, for, case)

July 17, 2003 Serguei A. Mokhov, 21


[email protected]
Internal Variables
$# Will tell you # of command line arguments supplied

$0 Ourselves (i.e. name of the shell script executed


with path)
$1 First argument to the script
$2 Second argument, and so on…
$? Exit status of the last command
$$ Our PID
$! PID of the last background process
$- Current shell status
July 17, 2003 Serguei A. Mokhov, 22
[email protected]
Internal Variables (2)
• Use shift command to shift the arguments one
left:
– Assume intput:
• ./shift.sh 1 2 foo bar
– $0 = <directory-of>/shift.sh
– $1 = 1
– $3 = 2
– $4 = foo
– $5 = bar
• shift:
– $1 = 2
– $2 = foo
– $3 = bar
July 17, 2003 Serguei A. Mokhov, 23
[email protected]
Environment
• These (and very many others) are available to your
shell:
– $PATH - set of directories to look for commands
– $HOME - home directory
– $MAIL
– $PWD – personal working directory
– $PS1 – primary prompt
– $PS2 – input prompt
– $IFS - what to treat as blanks
July 17, 2003 Serguei A. Mokhov, 24
[email protected]
Control Flow: if
• General Syntax: if [ <expression> ]; then
<statements>
elif
<statements>
else
<statements>
fi
• <expression> can either be a logical
expression or a command and usually a
combo of both.
July 17, 2003 Serguei A. Mokhov, 25
[email protected]
if (2)
• Some Logical “Operators”:
-eq --- Equal
-ne --- Not equal
-lt --- Less Than
-gt --- Greater Than
-o --- OR
-a --- AND
• File or directory?
-f --- file
-d --- directory
July 17, 2003 Serguei A. Mokhov, 26
[email protected]
case
• Syntax:
case <expression> in
<patter1>|<value1>)
command1
;;

<patter2>|<value2>)
command2
;;
esac
July 17, 2003 Serguei A. Mokhov, 27
[email protected]
case (2)
case $# in
1) cat >> $1
;;
2) cat >>$2 <$1
;;
3) case $3 in
-[abc]) echo "-a -b or -c"
;;
-foo|-bar) echo "-foo or -bar"
;;
esac
;;
*) echo "we accept up to 3 args only."; exit 127
;;
esac
July 17, 2003 Serguei A. Mokhov, 28
[email protected]
for
• Syntax:
for variable in <list of values/words>[;]
do
command1
command2

done

• List can also be a result of a command.

July 17, 2003 Serguei A. Mokhov, 29


[email protected]
for (3)
for file in *.txt
do
echo File $file:
echo "======"
cat $file
echo "======"
done
July 17, 2003 Serguei A. Mokhov, 30
[email protected]
while
• Syntax
while <expression>
do
command1
command2

done

July 17, 2003 Serguei A. Mokhov, 31


[email protected]
until
• Syntax
until <expression>
do
command1
command2

done

July 17, 2003 Serguei A. Mokhov, 32


[email protected]

You might also like