You are on page 1of 71

Shell Scripting Part I

Prepared by : Simson D’silva


Date : 04th December 2008
Structure Of Unix

• General structure of Unix OS.


• Multi-user/ Multitasking /Multithreaded OS

2 >
Basics of VI Editor and Scripting

• VI a full screen text editor on Unix.

• Three modes – input – command – ex

• Set showmode – will show the mode while working in vi

3 >
Vi….(Command mode)

• Open mode Add below current line o


• Add above current line  shift o
• Append Mode next char  a | append at end of line  Shift a

• Copy lines / yank line  yy (repeating factor 5lines)  5yy


• Copy word / yank word  yw (repeating factor 4words)  4yw
• Paste line p above  Shift p

• Delete single Char x (repeating factor 3char)  3x


• Delete Line  dd (repeating factor 5lines)  5dd
• Delete Line from cursor to end  Shift d
• Paste line p above  Shift p

• To repeat your last action  . (dot)


• Undo last command u

• Replace char  r (followed by char to replace ,no repeating factor)


• Replace word  cw (followed by word to replace)
• Replace multiple chars  shift r

• To Join current line with next line  Shift J


• To check if file is modified & no of lines  ctrl g

4 >
Vi…(Ex/Line Mode)

• Search from start of Document  / (word) enter


• Search from bottom of Document  ? (word) enter
• Continues search n

• Search & Replace  %s/searchword/replaceword


(% represents entire document, s substitute first occurrence)
• Global Search & Replace search  %s/searchword/replaceword/g
• Search, Confirm n Replace  %s/searchword/replaceword/gc
(c confirm y or n)

• To go to start of Line  ^
• To go to end of Line  $
• To go to end of file  shift g
• To go to a line  :line number
• To see the line number at all times  :set number or :se nonumber

• Save file  :w also :wq


• Don’t save  :q! :q
• Restore to last saved version  :e!
• Saves file and Exit  Shift zz

5 >
Vi… (Addition Options)

--------Opening multiple files----------------


• $vi <firstfile> <secondfile>….
• Switching to next file  :n
• Interchange between last two files  :e#

• To give os commands from Vi  :!<os command>

---------Copy to Buffer ------------------


• Copying multiple lines
>  mk (mark start line)
>  :lineno (goto a particular line)
>  “ay’k (creates buffer in a, we can create upto 26 buffers)
• Pasting Buffer “ap

• To Cut Multiple lines replace “ay’k with “ad’k .. Pasting is the same “ap
---------Setting environment for Vi-----------
• Go to home directory : cd space
• vi .exrc
---------Setting environment after logging to Unix----------
• Go to home directory : cd space
• vi .profile

6 >
Introduction to Shell

What is Shell ?
• One of the most powerful program available in UNIX
• Provide consistent and easy to use environment for executing programs
• It reads the command you type and and ask UNIX kernel to perform
these commands
• Provides sophisticated programming constructs
• There are 30 odd shells eg. Borne, Bash, C, TC, Korn,
• Borne shell --> 1st available on unix machine also known as father of all
shells
• Korn shell & Bash shells are supersets of Borne shells & have lots of other
features
• The shell script written for specific shell can be ported to same shell on
other variant Unix. (The Unix specific commands can not be ported)

7 >
Types of Shells

Shell

Bourne Shell C Shell

Korn POSIX Bourne C


bash tcsh
Shell Shell Shell Shell

8 >
Types of Shells….continued

• Borne Shell
> Standard and most compact shell
> Default prompt : $
• Korn shell
> Superset of bourne shell
• C Shell
> Uses C-like syntax
> Default prompt : %
• Bash ( Bourne again shell )
> Developed as a part of GNU project and replaced the bourne shell on GNU systems –
Linux
> Includes features of C shell ( csh and tcsh) and korn shell (ksh)
> Retains syntax compatibility with the bourne shell

To list all the shells


• Cd /usr/bin
• Ls –ltr *sh

To Check your current Shell (3 ways)


1) ps (Most authentic way)
2) echo $SHELL
3) Cat /etc/passwd | grep <username>

9 >
Types of Shells….continued

Program Name Shell Desc


/bin/sh Bourne shell

/bin/rsh Restricted bourne shell

/bin/jsh Bourne shell including


job control
/bin/ksh Korn shell

/usr/dt/bin/dtksh Desktop korn shell

/bin/rksh Restricted korn shell

/bin/csh C shell

10 >
Special Files…

• Special Files
> /etc/profile
– Executed automatically at login, first.
> $HOME/.profile
– Executed automatically at login, second.
> $ENV
– Specify the name of the file to read when a new korn shell is
created.
• Shell Initialization
Y- Execute Y- Execute
is is
Command
Login /etc/profile $HOME/.profile
Prompt
exists exists
N-skip N-skip

11 >
Special Files… continued

• Continued
> ~/.login
– Executed by login shell after .cshrc login
> ~/.logout
– Executed by login shell at logout
> ~/.history
– History list saved from previous login
> ~/.cshrc
– Executed at each instance of C shell invocation

12 >
Unix Commands - Intro

Commands

Simple Complex Compound

13 >
Unix Commands - Intro….continued

Simple Commands
• Commands that are executed by just giving its name at the prompt
• Examples :
– who
– date
– ls
Complex Commands
• A command that consists of command name and list of arguments. Arguments
are command modifiers that changes the behavior of a command
• Examples :
– who am i
– date +%m/%d
– ls –l
Compound Commands
• Combination of simple and complex commands
• Consist of list of simple and complex commands separated by command
separator ( ; ).
• Examples :
– Date ; who am i

14 >
Unix Commands….

man (MANual)
This command prints out the description of a command, its uses, and syntax

passwd : Allows the user to change their password. No flags or arguments are used by the normal user.

pwd : We have already come across this command, which prints the name of the working directory on the
screen. This command takes no flags or arguments at all.

mkdir : Creates new directory eg :


> mkdir newdir
> mkdir –p these/directories/have/not/yet/been/created

rm -r [-i] dirname . . . [file . . .]


Removes files. Use this with caution. Once a file has been removed, you CANNOT retrieve it.
–i flag requiring confirmation
–r flag allows you to remove a directory recursively
-f forcefully delete file

rmdir (ReMove DIRectory)


rmdir dirname . . . The directory has to be empty to be deleted unlike the rm command above

cd [directory] : Changes the directory which you are currently in. e.g.,
> cd ..
> cd ~user/bin (takes us to the bin directory in user’s home directory)

15 >
Unix Commands….Continued

ls [-altr] [names] : Lists the directories and files in a directory.


> ls –l ~
> ls –lt ~
> ls –ltr ~

cat file . . .
Reads each file in sequence and writes it to the standard output. Eg.
> cat firstfile

more [file ...]


Writes the file specified as an argument to standard output, one screen at a time. eg
> more ls

head [ -n count ] [ file ...]


Prints a specified number of lines from the specified file to standard output.
> head -15 logfile

tail [ -f ] [ -c number | -n number ] [ file ]


Prints the specified file to the standard output beginning at a designated place. With no flags, tail writes the
last 10 lines of a file to standard output. Eg:
> tail -10 testdata.dat
> tail -f local.login (will show u the new updates from log files)
> tail -5c sample.f - display last 5 characters

16 >
Unix Commands….Continued

wc (Word Count) wc [ -c|-m ] [ -lw ] [ names ]


Counts the number of words, lines or characters in a file,
> wc –l firstfile (count number of lines in a file)
> wc –w firstfile (count number of words in a file)

sort [-nr] [-k keydef] [files] : Sorts the file based on some sort criteria.
examples
> sort -k1 data.txt (Sorts on first field)
> sort -t"," -k3 data.txt (K3 ref the third col & -t"," gives the separator"
> sort -t"," -k2 -r data.txt (-r for desc order)
> sort -t"," -k2,3 data.txt (ordering multiple cols)

cut : Cuts out selected fields of each line of a file. Examples:


> cut -c 3 data.txt (character)
> cut -c 6-10 data.txt
> cut -d "," -f2,4 data.txt
> cut -d '|' -f 1,3 data1.txt
> cut -d '|' -f 1-3 data1.txt

17 >
find (FIND) : The find command is very powerful. It can search
the entire filesystem for one or more files that you specify to look
for.
> find / -name Chapter1 -print
> find / -name data* -print 2>/dev/null (to ignore errors we can sends
errors to trashcan)
> find . user ls (searches for files by user in current directory & prints an extended
file information )
> find . -name "my*" -type f
(limits the results of the above search to only regular files, excluding directories,
special files, symbolic links, etc. )
> find . \( -name "*jsp" -o -name "*java" \) -type f -ls
(Find any one of differently named files)
> find /var/ftp/mp3 -name "*.mp3" -type f -exec chmod 644 {} \;
(Execute an action with –exec. The {} replaces the name of the file once found)
> find /tmp -exec grep "search string" '{}' /dev/null \; -print
(will search for a string in all files from the /tmp directory and below)

18 >
Unix Commands….Continued

mv file1 [file2 ...] target


Moves file1 to the location at target. If target already exists, it will be overwritten. This
command is often used to rename files.
> mv ~/firstfile testfolder/firstfile

cp file1 [file2 ...] target


Copies file1 to the location at target, so that there are now two copies of the same file (at
file1 and target). If target already exists, it is overwritten.

echo [ -n ] [ arg ] ...


Writes its arguments separated by blanks and terminated by a newline (except when -n is
specified) to the standard output.
> echo hello my name is Simson prints “hello my name is Simson” to standard
output
> echo “HELLO WORLD”
> echo “$SHELL”
> echo ‘$SHELL’

19 >
Unix Commands….Continued

• whatis – Display one line answer Eg: whatis ls

• read – Creates & accepts input for a variable from the user in an interactive mode

• date – displays or set the date


> date +%Y/%m/%d (Displays the date in YYYY/MM/DD format)
> date +%T (Displays the Time)
> date +%a/%b (Displays the Day of week, Month of year in English abbrrevation)

• who [am i] – displays the current user login name

• finger –display information about local and remote users

• which – locate the directory of command

• touch – create a empty file

• id – it returns user identity

• su – become superuser or another user

20 >
File information from ls –l command

•The output of the "ls -al" command is very important.


•The first ten characters of output can be broken down as follows:

File Type: The character in the first column indicates the “type” of file that is listed on the current line. This
first character is usually a "-" (indicating an ordinary file) or a "d" (indicating a directory).

User: This field contains information about permissions for the owner of the file or directory.

Group: This field contains information about permissions for users in the same group as the owner of the file
or directory.

Other: This field contains information about permissions for users not in the same group as the owner of the
file or directory.

File User Group Other


Type
d r w x R W X R w x
d r w X r W - R - -
- r w X r - - r - -
- r - x - - - - - -

21 >
Possible UNIX File Types

File Type Description


Regular file (-) •No imposed structure on the internal contents - just a string of bytes
•ASCII text or binary data

Directory File (d) •A "table of contents" file


•Has a defined internal structure
•Provides file system organization

Block Special File (b) •Identifies a device


•Block I/O
•Floppy disks, hard disks, tape devices

Character Special File (c ) •Identifies a device


•Character I/O
•Serial ports, parallel ports (terminals, printers)

Symbolic Link (l) •A file that point to another file


•Used to provide an alias for another file

Shared Memory (m) •Allows multiple programs to access a common memory space
•Used most often by application programs

Named Pipe (p) •Used to pass data between processes

Semaphore (s) •Used to synchronize concurrently running processes


•Used by application programs

22 >
Change File Permissions

chmod (CHange permissions MODe)


chmod [-R] mode file ...
chmod [-R] [ugoa]{+|-|=}[rwxXstl] file ...

Changes the permissions mode of a file or directory. Changes can be made using either the symbolic or
absolute representations.
– Absolute 4 = read
– Absolute 2 = write &
– Absolute 1 = execute
Examples Absolute:
•For example:
» user group others

•chmod 640 file1 rw- r-- ---


•chmod 754 file1 rwx r-x r–
•chmod 664 file1 rw- rw- r–

Changes the permissions of filename to read, write and execute for the user, execute only for the group,
and read only for all others.

If symbolic representations are used, the group(s) whose permissions are being changed must be specified
(the groups are indicated using letters: u: user; g: group; o; others; a: all, i.e., user, group and others) as
well as whether the permissions are being added or removed.
Example Symbolic
chmod u-rwx insertfile

23 >
Other Symbols

Symbol Meaning/Action Example


> Redirect Output ls > outputfile.dat
>> Append to file date >> outputfile.dat
< Redirect input bteq < insertfile
<< “Here” document bteq << EOF…..
| Pipe output cat logfile | grep failure
|& Start a coprocess (korn shell only)
& Run process in background ./example &
nohup To execute the job on the server nohup ./example
; Separate command on same line ( ls ; pwd )
* Match any character(s) in filename ls *dat
? Match single character in filename ls ???_2008.mld
`` Substitute output of enclosed command echo “I am in `pwd`”

24 >
What is a Shell Script

• A text file containing commands which could have been


typed directly into the shell.

• The shell itself has limited capabilities -- the power


comes from using it as a language to combine the
standard Unix utilities to produce a tool more useful than
the individual commands alone.

25 >
My First Shell Script

Example : With n without quotes

#!/bin/ksh
echo Hello World

#!/bin/ksh
echo “Hello World”

#!/bin/ksh
echo ‘Hello World’

Example : Reading input from user

#!/bin/ksh
echo “Enter your text for printing :\c”
read txt
echo $txt

26 >
Executing a shell script

To enforce that the script is executed in the shell of your


choice
>Add Banging character on first line of script
>#!/bin/sh or #!/bin/ksh or #!/bin/csh

To execute script
•Chmod 766 lab1
>./lab1 (always recommended to use ./ so that no errors are given
in some shell)

•Or ksh lab1 (even if the file is not given executable permissions
KSH is executed & read is used for file lab1)

27 >
Assignment….

• Write a program to get the output like this


DATE : YYYY/MM/DD
TIME : HH:MM:SS
USER : <your username>
Program Directory is : /export/…./tuser_15
The number of files in this directory are : <filecount>

Eg :
• DATE is : 2008/12/03
• TIME is : 11:06:37
• USER is : sd185043
• Program Directory is : /export/home0/users/sd185043
• The number of files in this directory are 32

28 >
Solution….

• #!/bin/ksh
• clear
• echo
• echo
• echo "DATE is :" `date +%Y/%m/%d%n`
• echo "TIME is :" `date +%T`
• echo “USER is : `who am i | cut -c1-8`"
• echo "Program Directory is :" `pwd`
• echo "The number of files in this directory are \c"
• ls | wc –l
• exit 0

29 >
Echo…escape sequences

• echo
> Print arguments, separated by spaces, and terminated by a
newline, to stdout.
> Use quotes to preserve spacing.
> Echo also understands C-like escape conventions.

> \b backspace \c print line without new-line


> \f form-feed \n new-line
> \r carriage return \t tab
> \v vertical tab \\ backslash

30 >
Shell Variables/Environment Variables

• Local Variable
TDP_ID=gccindia (Define variable TDP_ID and assign value gccindia)
X=10

• Environment/Global Settings
export variable
export x=10
export PS1=‘$PWD>’

• There should be no spaces between <Variable>=<value>


• set Command = Displays all the variables set in shell
(in C shell set command is used like the Export command above)
• unset Command = removes a variable from shell

31 >
Command line parameters

Symbol Meaning/Action
$# Number of arguments in command line

$0 Name of program

$@ All arguments enclosed in quotes.

Similar to above but does not identify quoted arguments. Each


$*
argument as simple word
$1 First argument

$9 Ninth argument

Shift If more than 9 arguments are to be read from command line

Echo “param8 $8”


Echo “param9 $9”
shift
example
Echo “param10 $9”
shift
Echo “param11 $9”

$$ Process ID

$? Return Code/Exit Status of the previous command


32 >
Command line parameters(Continue…)

• EG : If the argument list is:

> a1 a2 "a3 contains spaces" a4


> then: $1=a1 $2=a2
> $3=a3 contains spaces $4=a4
> $*=a1 a2 a3 contains spaces a4
> $@"="a1" "a2" "a3 contains spaces" "a4“

• EG :argument.ksh
----------------------------
> #!/bin/ksh
> echo 0 $0
> echo 1 $1
> echo 2 $2
> echo 3 $3
> echo 4 $4
> echo All $*
> echo All $@
> echo "Count :" $#

33 >
Shell standard input and output

• Most Unix commands do not care where their input is derived from, or where
their output is written. This feature allows commands to work with files,
devices, terminals and other commands.
• There are three standard devices associated with a command when it
executes.
Device Name File Descriptor Normally associated with

stdin standard input 0 keyboard

stdout standard output 1 console

stderr standard error 2 console

• ./insertfile 1> stdlog 2>errorlog


• ./insertfile 2>&1 commonlog
• echo “error” >& error.log (bash shell no need to maintain 1 & 2 )
• cmd > /dev/tty >& error (Redirect output to screen, and error to error )

34 >
Expression evaluation (expr)

expr : evaluates expressions which can be mathematical or logical. It also has some string
operators. Once it has evaluated the expression, it sends the result to standard output.

•Eg : expr 1 + 1

•Each component of the expr expression has to be separated by whitespace. All shell
metacharacters ((,),<,>,|,&,;,*) must be ‘escaped’ by a back slash (\). Expressions can
be grouped using \( expr \).

•Eg : expr \( 5 + 4 \) \* 10

•Eg : expr.sh
=================================
#!/bin/sh
echo Enter three numbers
read a b c
echo `expr \( $a + $b \) \* $c`

• Operators Supported by expr :


Mathematical ( +, -, /, *, %)
comparison (=>, >, =, <=, !=)
logical : and (&), or (|)

35 >
Test Statement

• test command evaluates various expressions or determines whether they are true or
false. When an expression is true, a value of 0 is returned, otherwise it returns a non-
zero integer.

Test Exit Status


-l <string> Returns the length of string
string1 = string2 Returns true (0) if string1 is equal to string2
string1 != string2 Returns true if string1 is not equal to string2
integer1 –eq integer2 Returns true if integer1 = integer2
integer1 –ne integer2 Returns true if integer1 != integer2
integer1 –gt integer2 Returns true if integer1 > integer2
integer1 –ge integer2 Returns true if integer1 >= integer2
integer1 –lt integer2 Returns true if integer1 < integer2
integer1 –le integer2 Returns true if integer1 <= integer2
! Expr Returns true if expr is false
expr1 –a expr2 Returns true if both expr1 and expr2 are true ANDING
expr1 –o expr2 Returns true if both expr1 or expr2 is true OR’ING

Eg:
X=5; y=7; z=7.2
test $x –eq $y; echo $?

36 >
File & String testing (test) contd..

Test Exit Status

-e file True if file exists

-f file True is file exists and is a regular file

-r file True is file exists and is readable

-w file True is file exists and is writeable

-x file True is file exists and is executable

-d file True if file exists and is a directory

-s file True is file exists and has a size greater than zero

-z String True if string string exists is length 0.

-n String True if string exists & is of length non-null

37 >
Basic Shell Scripting (Continue…)

• If condition

if condition
then
execute commands
else
execute commands
fi

38 >
Basic Shell Scripting (Continue…)

• Multi-way branching
if condition
then
execute commands
elif condition
then
execute commands
else
execute commands
fi

39 >
Assignment…

• Enter two numeric arguments at run time.

• If numbers are equal print “Good choices”

• If first number is greater than second number print


“First choice is greater”

• If second number is greater than first number print


“Second choice is greater”

40 >
If Example….

if.sh
=================================
#!/bin/ksh
echo Enter two numbers
read first second
============
if test $first -gt 10
then
This script outputs:
if test $second -gt 10
then -----------------------
else
echo "You like big numbers."
prompt> if.sh
echo "Up and down you think like the stockmarket."
fi Enter two numbers
elif test $first -eq 10
then 1 66
if test $second -eq 10
then You should be a
echo "Looks like you like 10s."
else
echo "Good choices."
random number
fi
elif test $second -lt 10
generator.
then
echo "You like small numbers."
-----------------------
else
echo "You should be a random number generator.”
fi

41 >
Case Statement

• Case statement

case expression in
pattern1)
execute commands ;;
pattern2)
execute commands ;;
pattern3)
execute commands ;;

esac

42 >
Case Example

• case.ksh
• ===========================
• #!/usr/bin/ksh
• echo "\t 1. By User ID"
• echo "\t 2. [E]xit/[Q]uit"
• echo "\t Enter your choice: \c"
• read answer

• case "$answer" in
• 1)
• echo "\t ${b}Enter User ID : ${n}\c"
• read userid
• ;;
• 2|E|e|Q|q )
• clear
• exit
• ;;
• *)
• echo "\t${b} Wrong choice...\c"
• sleep 2
• ;;
• esac

43 >
Basic Shell Scripting (Continue…)

• Here Document

bteq<<EOF > /dev/null


.logon TDP_ID/USER_ID, PASSWD ;
.export report file=my_file
SELECT GRANTEE (Title '') FROM DBC.ROLEMEMBERS WHERE ROLENAME= '$1' ORDER BY 1;
.logoff
.quit
EOF

44 >
Assignment

• Write a program which does the following


-Accepts two parameters table name database name
-Write a bteq query using here document which will give the DDL for
that table in a flat file .
• Eg:
• ./assignment au accounts

• Output :
CREATE SET TABLE au.accounts ,FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
DATABLOCKSIZE = 64000 BYTES, CHECKSUM = DEFAULT
(
ACCOUNT_NUMBER INTEGER,
NUMBER INTEGER,
STREET CHAR(25) CHARACTER SET LATIN NOT CASESPECIFIC,
CITY CHAR(20) CHARACTER SET LATIN NOT CASESPECIFIC,
STATE CHAR(2) CHARACTER SET LATIN NOT CASESPECIFIC,
ZIP_CODE INTEGER,
BALANCE_FORWARD DECIMAL(10,2),
BALANCE_CURRENT DECIMAL(10,2))
UNIQUE PRIMARY INDEX ( ACCOUNT_NUMBER );

45 >
Solution…

• #!/bin/ksh
• . ./logon.dat
• bteq << EOF
• .logon $TDP_ID/$USER_ID,$PASSWD;
• .export report file = showoutput.dat
• show table au.accounts;
• .export reset;
• EOF
• clear
• cat showoutput.dat

46 >
Iterative loops – While

• while looping

while condition
do
execute commands
done

Eg:
• #!/bin/ksh
• count=0 # Initialise a counter
• while [ $count -lt 5 ] # Set up a loop control
• do # Begin the loop
• count=`expr $count + 1` # Increment the counter
• echo “Count is $count“ # Display the result
• done # End of loop

Eg:
• while read line
• do
• echo $line
• done < Filename

47 >
Iterative loops – For…do…done

• for looping
for variable in list
do
execute commands
done

(for...do...done)
#!/bin/sh
for file in `ls`
do
echo $file
done

48 >
Assignment

• Write a shell script to read datafile & create SQL


statements “insert” along with values in file bteqinput..

• Datafile
20032894|511|Granada|Hermosa Beach|CA|90365|699.72|978.60
20028828|500|La Salle Ave|Culver City|CA|90020|18183.10|10752.95
20028862|771|Sparta Dr|Culver City|CA|90020|7971.81|11149.05

• Output eg :
insert into accounts values
(20032894,511,'Granada','Hermosa Beach','CA',90365,699.72,978.60);
insert into accounts values
(20028862,771,'Sparta Dr','Culver City','CA',90020,7971.81,11149.05);
…..

49 >
Insert data in table - bteq

• #!/bin/ksh
• echo `date`
• rm bteqinput
• . ./logon.dat
• echo "delete from accounts all;" >bteqinput

• while read line


• do

• fl1=`echo $line|cut -d '|' -f1`


• fl2=`echo $line|cut -d '|' -f2`
• fl3=`echo $line|cut -d '|' -f3`
• fl4=`echo $line|cut -d '|' -f4`
• fl5=`echo $line|cut -d '|' -f5`
• fl6=`echo $line|cut -d '|' -f6`
• fl7=`echo $line|cut -d '|' -f7`
• fl8=`echo $line|cut -d '|' -f8`
• echo "insert into accounts values ($fl1,$fl2,'$fl3','$fl4','$fl5',$fl6,$fl7,$fl8);">>bteqinput
• done < datafile
• echo $
• bteq << EOF
• .logon $TDP_ID/$USER_ID,$PASSWD
• .run file=bteqinput
• #.quit
• EOF
• echo `date`
• bash-2.05$

50 >
Shell Basics….

• Differing features
> Following table display features that are different among
Bourne, Korn and C Shells

51 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

$ $ % Prompt

52 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

>file >file Combine stdout and


>& file
2>&1 2>&1 stderr

53 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

$HOME $HOME $home Home directory

54 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

~ ~ Home directory symbol

55 >
Shell Basics….continued

• Differing features

sh ksh Csh Meaning/Action

var= Var= set var=


Variable assignment
Value value value

56 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

export export setenv Set environment


var var=val var val variable

57 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

$# $# $#argv Number of arguments

58 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

$? $? $status Exit status

59 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

Source
.File .File Read command in file
file

60 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

alias
alias x y Names x stand for y
x=y

61 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

switch/c
case case Choose alternatives
ase

62 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

exit exit
exit [n] Exit with status
[n] [(expr)]

63 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

for/ for/
foreach Loop through variables
Do do

64 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

history history List previous commands

65 >
Shell Basics….continued

• Differing features

Sh ksh csh Meaning/Action


if
[ $I –eq if ((i==5)) if($I==5) Sample if statement
5]

66 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

fi fi endif End if statement

67 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

Read from standard


read read $<
input

68 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

unalias unalias Remove aliases

69 >
Shell Basics….continued

• Differing features

sh ksh csh Meaning/Action

while/ while/
while Begin while loop
do do

70 >
Assignments

71 >

You might also like