How to run a shell program - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

How to run a shell program

Description:

... single quote, it ignores any otherwise special characters that follow until it ... msg='I love New York' $ echo $msg. I love New York. The single Quote (continue. ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 39
Provided by: skander4
Category:
Tags: program | run | shell

less

Transcript and Presenter's Notes

Title: How to run a shell program


1
Variables in UNIX
2
How to run a shell program
  • Shell program can be run from
  • terminal prompt
  • executable files which has the shell program
  • Any file can be executable by changing the
    permission chmod file(s)
  • The executable file may have many lines, where
    every line has a correct shell program

3
How to run a shell program (continue.)
  • If any of the lines (shell program) has error
    (i.e. syntax), this will not prevent other lines
    from being executed
  • Whenever the shell executes the special chracter
    at the start of a word, it takes whatever
    characters follow the to the end of the line as
    comment

4
How to run a shell program (continue.)
who wc -l 11 cat gt nu who wc -l
chmod x nu ls -l nu -rwxr-xr-x 1 abuzneid
534 12 Oct 11 0041 nu nu 11
nu gtnufile cat nufile 11
5
How to run a shell program (continue.)
cat gt userstate This is userstate script echo
The current date and time is date To print the
date echo echo The number of users on the system
is who wc -l echo echo Your current working
directory is pwd chmod x userstate
userstate The current date and time is Thu Oct
11 005246 EDT 2001 The number of users on the
system is 11 Your current working
directory is /home/abuzneid/UNIX
6
Variables
  • A shell variable begins with alphabetic or
    underscore "_" character, and is followed by zero
    or more alphabetic or underscore characters
  • variablevalue
  • Must not be space before or after ""
  • Example my_bin/usr/local/bin
  • Shell does not have the concept of data types

7
Displaying the value of variables
  • If a valid variable name followed the , then the
    shell takes this as an indication that the value
    stored inside that variable is to be substituted
    of that point
  • Variables can be assigned to
  • numbers
  • alphanumeric chracters/strings
  • other variables
  • utilities or programs
  • null values
  • file names

8
Displaying the value of variables (continue.)
num10 dir/usr/local/bin echo num 10
echo num num echo dir /usr/local/bin echo
dir dir echo dir num /usr/local/bin 10
9
Displaying the value of variables (continue.)
arguments
echo
10
arguments
echo
/usr/local/bin 10
10
Displaying the value of variables (continue.)
my_UNIX_dir/home/abuzneid/UNIX
my_UNIX_dir /home/abuzneid/UNIX cannot
execute commandwho command am i abuzneid
pts/7 Oct 11 0125 (216.87.102.220)
nu'who wc -l' nu bgeorge pts/16
Oct 5 1501 (216.87.102.204) msiles pts/3
Sep 27 1744 (216.87.102.201) abakshi
pts/7 Oct 11 0125 (216.87.102.220) tph
ilip pts/11 Oct 2 1410
(AC8C6085.ipt.aol.com) abuzneid pts/9
Oct 10 1929 (avicenna.102.87.216.in-addr.arpa)

11
Displaying the value of variables (continue.)
value110 value2value1 echo
value2 value1 value2value1 echo
value2 10
12
The null value
  • To set a variable to null
  • variable
  • variable"" (double quote)
  • variable' (single quote)
  • variable" " with a space between the quotes is
    not a null variable (space is a character)

13
The null value (continue.)
echo null_variable echo null_variable
nosuch echo nosuch
14
File name substitution as variables
ls Documents foo mail personal
TEST test x echo x Documents TEST
foo mail personal test
15
File name substitution as variables (continue.)
x

args ison memos phonebook . . . students
arguments
echo
16
The (variable) construct
  • To delimit the end of the variable name, enclose
    the entire name (but not the leading dollar sign)
    in a pair of curly braces
  • Example filename
  • Example to rename a file from "myfile" to
    "myfileX"

17
Quote Characters in UNIX
18
Quotation
19
The single Quote
  • When the shell sees the first single quote, it
    ignores any otherwise special characters that
    follow until it sees the closing quote

cat phonebook Edward John 336-1454 Alice
Stuart 334-1219 Sony Bell
332-3364 Robert Micheal 326-0569 grep Alice
phonebook Alice Stuart 334-1219 grep Sony
phonebook Sony Bell 332-3364 grep Sony
Bell phonebook grep can't open
Bell phonebookSony Bell 332-3364 grep
'Sony Bell' phonebook Sony Bell 332-3364
20
The single Quote (continue.)
  • grep Chen Tao phonebook
  • grep 'Chen Tao' phonebook

arguments
Chen Tao phonebook
echo
Chen Tao phonebook
arguments
echo
21
The single Quote (continue.)
  • Single quotes preserves all white spaces

echo one Two Three one Two Three echo 'one
Two three' one Two three echo 'How are
you doing, gt today' How are you doing, today
22
The single Quote (continue.)
  • Quotes are needed when assigning values
    containing white space or special characters to
    shell variables

msg'I love New York' echo msg I love New
York
23
The single Quote (continue.)
  • Shell does file substitution after variable name
    substitution even in between single quotes

msg' are my files' echo msg Documents TEST
foo list mail personal test are my files
24
The double quote
  • Three characters are not ignored inside double
    quotes
  • Dollar sign
  • Back quotes
  • Backslashes
  • File names substitution (,?) is not done inside
    double quotes

25
The double quote (continue.)
X echo X Documents TEST foo list mail
personal test echo 'X' X echo "X"
26
The double quote (continue.)
address"169 University Ave gt Bridgeport, CT
06601" echo address 169 University Ave
Bridgeport, CT 06601 echo "address" 169
University Ave Bridgeport, CT 06601
27
The double quote (continue.)
  • Double quotes can be used to hide single quotes
    from the shell, and vice versa

X"Hello, 'he said'" echo X Hello, 'he
said' Y'"he was here",Tom said' echo Y "he
was here",Tom said
28
The Backslash
  • Equivalent to placing single quotes around a
    single character

echo gt syntax error newline or '
unexpected echo \gt gt X echo \X X echo
\\ \
29
The (variable) construct
myfilefoo mv myfile myfileX mv
Insufficient arguments (1) Usage mv -f -i f1
f2 mv -f -i f1 ... fn d1 mv
-f -i d1 d2 mv myfile myfileX
30
Passing Arguments
31
Passing arguments
  • Whenever you execute a shell program, the shell
    automatically stores the first argument in the
    special shell variable 1, the second argument in
    the variable 2, and so on. 1,2..9
  • These special variables are known as positional
    parameters

32
Passing arguments (continue.)
cat gt ison who grep 1 who bgeorge
pts/16 Oct 5 1501 (216.87.102.204) abuz
neid pts/17 Oct 11 0117
(216.87.102.224) abuzneid pts/7 Oct 11
0125 (216.87.102.220) Steve pts/9
Oct 10 1929 (avicenna.102.87.216.in-addr.arpa)
chmod x ison ison abuzneid abuzneid
pts/17 Oct 11 0117 (216.87.102.224) abuz
neid pts/7 Oct 11 0125
(216.87.102.220) ison bgeorge bgeorge pts/16
Oct 5 1501 (216.87.102.204)
33
The variable
  • is set to the number of arguments that were
    typed on the command line

cat gtargs echo arguments passed echo
arg11 arg22 arg33 chmod x args
args a b c 3 arguments passed arg1a arg2b
arg3c args a b 2 arguments passed arg1a
arg2b arg3 args 'ls' 1 arguments
passed arg1ls arg2 arg3
34
The variable
  • references all of the arguments passed to the
    program

cat gtargs2 echo arguments passed echo they
are chmod x args2 args2 a b c 3
arguments passed they are a b c args2 10
arguments passed they are Documents TEST args
args2 fooX ison list mail personal test
35
The shift command
  • Maximum number of variables can be referenced by
    the shell is 9. Shell accepts single digit
    following the dollar sign
  • The shift command allows you to effectively left
    shift your positional parameters
  • I. e. after shift is executed, whatever was
    previously stored inside 2 will be assigned to
    1, and whatever was stored in 3 will be
    assigned to 2, and so on.

36
The shift command (continue.)
  • The old value of 1 will be irretrievably lost
  • Whenever shift is executed, is also
    automatically decremented by one

37
The shift command (continue.)
  • Naturally, you should first save the value of 1
    before shift, if the value is needed later in the
    program
  • arg11
  • shift
  • arg109
  • The shift command is very useful when processing
    a variable number of arguments

38
References
  • UNIX SHELLS BY EXAMPLE BY ELLIE QUIGLEY
  • UNIX FOR PROGRAMMERS AND USERS BY G. GLASS AND K
    ABLES
  • UNIX SHELL PROGRAMMING BY S. KOCHAN AND P. WOOD
Write a Comment
User Comments (0)
About PowerShow.com