Presentation is loading. Please wait.

Presentation is loading. Please wait.

Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 12 Dr. Jerry Shiao, Silicon Valley University.

Similar presentations


Presentation on theme: "Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 12 Dr. Jerry Shiao, Silicon Valley University."— Presentation transcript:

1 Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 12 Dr. Jerry Shiao, Silicon Valley University

2 SILICON VALLEY UNIVERSITY CONFIDENTIAL 2 Summer 2015 Introduction UNIX/Linux Course Section 12 C Shell Programming Execution of shell programs Concept and use of shell variables Command line arguments are passed to shell programs Concept of command substitution Basic coding principles writing and discussing some shell scripts Commands and primitives

3 SILICON VALLEY UNIVERSITY CONFIDENTIAL 3 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Running a C Shell Script Run C shell in one of the three ways:  Make the script file executable by adding the execute permission to the existing access permissions for the file % chmod u+x script_file %  Run /bin/csh command with the script file as its parameter % /bin/csh script_file %  Force execution of a script in the C shell regardless of your current shell #!/bin/csh

4 SILICON VALLEY UNIVERSITY CONFIDENTIAL 4 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 12 Shell Variables and Related Commands A variable is a main memory location with a name The name of a shell variable is comprised of digits, letters, and underscore, with the first character being a letter or underscore Shell Variables:  Shell environment variables.  User-defined variables.  Must declare C shell variables before using them. Reference to uninitialized variable is an error.

5 SILICON VALLEY UNIVERSITY CONFIDENTIAL 5 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 12

6 SILICON VALLEY UNIVERSITY CONFIDENTIAL 6 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 12

7 SILICON VALLEY UNIVERSITY CONFIDENTIAL 7 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Reading and Writing Shell Variables set Command:  Declares and initializes a local variable.  Multi-variable word values in parenthesis. set name = (John Doe)  No argument displays all shell variables. setenv Command:  Declares and initializes a global variable.  Multivariable word values in double quotes. setenv name “John Doe”  No argument displays all environment variables.

8 SILICON VALLEY UNIVERSITY CONFIDENTIAL 8 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 12 bash Shell [sau@localhost c_dir]$ set cwd /home/sau/cs206/c_dir dirstack /home/sau/cs206/c_dir echo_style both edit gid 500 group sau histdup erase history 100 home /home/sau killring 30 owd path (/usr/kerberos/sbin /usr/kerberos/bin /usr/lib/ccache /usr/local/bin /bin /usr/bin /usr/local/sbin /usr/sbin /sbin /home/sau/rpi/tools/arm-bcm2708/gcc- linaro-arm-linux-gnueabihf-raspbian/bin /home/sau/bin) prompt %{\033]0;%n@%m:%c\007%}[%n@%m %c]%# prompt2 %R? prompt3 CORRECT>%R (y|n|e|a)? promptchars $# savehist (1024 merge) shell /bin/tcsh shlvl 2 sourced 0 status 0 tcsh 6.17.00 term xterm tty pts/0 uid 500 user sau csh Shell [sau@localhost c_dir]$ set BASH=/bin/bash BASH_VERSION='4.1.7(1)-release‘ COLUMNS=80 EUID=500 GROUPS=() HOME=/home/sau LINES=16 LOGNAME=sau MACHTYPE=i386-redhat-linux-gnu OLDPWD=/home/sau/cs206 OSTYPE=linux-gnu PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/cca che:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/ sbin:/home/sau/rpi/tools/arm-bcm2708/gcc-linaro-arm- linux-gnueabihf-raspbian/bin:/home/sau/bin PPID=2267 PS1='[\u@\h \W]\$ ' PS2='> ' PS4='+ ' PWD=/home/sau/cs206/c_dir TERM=xterm UID=500 USER=sau

9 % echo \$name $name % echo ‘$name’ $name % % echo $name name: Undefined variable. % set name % echo $name % set name = John % echo $name John % set name = John Doe II % echo $name John % echo $Doe $II % set name = (John Doe) % echo $name John Doe % set name = John* % echo $name John.Bates.letter John.Johnsen.memo John.email % echo “$name” John* % echo “The name $name sounds familiar\!” The name John* sounds familiar! SILICON VALLEY UNIVERSITY CONFIDENTIAL 9 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Reading and Writing Shell Variables

10 SILICON VALLEY UNIVERSITY CONFIDENTIAL 10 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Section 12

11 SILICON VALLEY UNIVERSITY CONFIDENTIAL 11 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Exporting Environment % setenv name “John Doe” % % cat display_name #!/bin/csh echo $name exit 0 % % set name=(John Doe) % display_name name: Undefined variable. % Unlike “set” command, there is no “ = “ between variable and value. Unlike “set” command, multiword values are in double quotes, not parenthesis.

12 SILICON VALLEY UNIVERSITY CONFIDENTIAL 12 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Exporting Environment % setenv name “John Doe” % display_name John Doe % echo $? 0 % % cat export_demo #!/bin/csh setenv name “John Doe” display_change_name echo “$name” exit 0 % cat display_change_name #!/bin/csh echo “$name” set name = (Plain Jane) echo “$name” exit 0 % export_demo John Doe Plain Jane John Doe %

13 SILICON VALLEY UNIVERSITY CONFIDENTIAL 13 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Resetting Variables % set name=John place=Corvallis % echo “$name $place” John Corvallis % unset name % echo “$name” name: Undefined variable. % echo “$place” Corvallis % % unset name place %

14 SILICON VALLEY UNIVERSITY CONFIDENTIAL 14 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Reading Standard Input % cat keyin_demo #!/bin/csh echo -n “Enter input: “ set line = `head -1` echo “You entered: $line” exit 0 % keyin_demo Enter input: UNIX rules the network computing world! You entered: UNIX rules the network computing world! %

15 SILICON VALLEY UNIVERSITY CONFIDENTIAL 15 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Passing Arguments to Shell Scripts % cat cmdargs_demo #!/bin/csh echo “The command name is: $0.” echo “The number of command line arguments is $#argv.” echo -n “The values of the command line arguments are: “ echo “$argv[1] $argv[2] $argv[3] $argv[4] $argv[5] $argv[6] $argv[7] $argv[8] $argv[9].” echo “Another way to display the arguments: $argv[*].” exit 0 % cmdargs_demo a b c d e f g h i The command name is: cmdargs_demo. The number of command line arguments are 9. The value of the command line arguments are: a b c d e f g h i. Another way to display values of all of the arguments: a b c d e f g h i. % cmdargs_demo One Two 3 Four 5 6 The command name is: cmdargs_demo. The number of command line arguments are 6. The value of the command line arguments are: One Two 3 Four 5 6. Another way to display the arguments: One Two 3 Four 5 6. %

16 SILICON VALLEY UNIVERSITY CONFIDENTIAL 16 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Passing Arguments to Shell Scripts % cat shift_demo #!/bin/csh echo “The name of the program is $0.” echo “The arguments are: $argv[*].” echo “The first three arguments are: $argv[1] $argv[2] $argv[3].” shift echo “The name of the program is $0.” echo “The arguments are: $argv[*].” echo “The first three arguments are: $argv[1] $argv[2] $argv[3].” exit 0 % shift_demo 1 2 3 4 5 6 7 8 9 10 11 12 The program name is shift_demo. The arguments are: 1 2 3 4 5 6 7 8 9 10 11 12. The first three arguments are: 1 2 3. The program name is shift_demo. The arguments are: 2 3 4 5 6 7 8 9 10 11 12. The first three arguments are: 2 3 4. %

17 SILICON VALLEY UNIVERSITY CONFIDENTIAL 17 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Passing Arguments to Shell Scripts % date Sat Aug 14 11:33:38 PDT 2004 % set argv = `date` % echo $argv[*] Sat Aug 14 11:33:38 PDT 2004 % echo “$argv[2] $argv[3], $argv[6]” Aug 14 2004 %

18 SILICON VALLEY UNIVERSITY CONFIDENTIAL 18 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Passing Arguments to Shell Scripts % cat set_demo #!/bin/csh set filename = $argv[1] set argv = `ls -il $filename` echo “The command line arguments are: $argv[*]” set inode = $argv[1] set size = $argv[5] echo “Name Inode Size” echo echo “$filename $inode $size” exit 0 % set_demo lab3 The command line arguments are: 856162 -rwx------ 1 sarwar 668 Aug 13 20:24 lab3 NameInodeSize Lab3856162668 % Using shell script argument as an argument to the set command to set the positional arguments.

19 SILICON VALLEY UNIVERSITY CONFIDENTIAL 19 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Comments and Program Headers Put comments in your programs to describe the purpose of a particular set of commands Use program header for every shell script you write, including:  Name of the file containing the script  Name of the author  Date written  Date last modified  Purpose of the script  A brief description of the algorithm used to implement the solution to the problem at hand

20 SILICON VALLEY UNIVERSITY CONFIDENTIAL 20 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands Used to determine the sequence in which statements in a shell script execute The three basic types of statements for controlling flow of a script are:  Two-way branching The if statement.  Multiway branching The if and switch statements.  Repetitive execution of one or more commands The foreach and while statements.  Jump out of sequence The goto statement.

21 SILICON VALLEY UNIVERSITY CONFIDENTIAL 21 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands The if Command -Expression is an execution of commands returning a status of TRUE ( 0 or success ) or FALSE ( 1 or fail ). - Expression formed by using operators: -Operator precedence: parenthesis, unary, arithmetic, shift, relational, bitwise, and logical.

22 SILICON VALLEY UNIVERSITY CONFIDENTIAL 22 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands -Expression is an execution of commands returning a status of TRUE ( 0 or success ) or FALSE ( 1 or fail ). - Expression formed by using operators: -Operator precedence: parenthesis, unary, arithmetic, shift, relational, bitwise, and logical.

23 SILICON VALLEY UNIVERSITY CONFIDENTIAL 23 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands

24 SILICON VALLEY UNIVERSITY CONFIDENTIAL 24 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands

25 SILICON VALLEY UNIVERSITY CONFIDENTIAL 25 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands % if_demo1 Usage: if_demo1 ordinary_file % if_demo1 lab3 lab4 Usage: if_demo1 ordinary_file % if_demo1 dir1 If_demo1: argument must be an ordinary file % if_demo1 lab3 NameInodeSize lab3856110591

26 SILICON VALLEY UNIVERSITY CONFIDENTIAL 26 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands

27 SILICON VALLEY UNIVERSITY CONFIDENTIAL 27 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands else

28 SILICON VALLEY UNIVERSITY CONFIDENTIAL 28 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands

29 SILICON VALLEY UNIVERSITY CONFIDENTIAL 29 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands % cat if_demo3 #!/bin/csh If ( ( $#argv == 0 ) || ( $#argv > 1 ) ) then echo “Usage: $0 file” exit 1 else if ( -f $argv[1] ) then set filename = $argv[1] set fileinfo = `ls -il $filename` set inode =$fileinfo[1] set size = $fileinfo[5] echo “NameInodeSize” echo echo “$filename $inode $size” else if ( -d $argv[1] ) then set nfiles = `ls $argv[1] | wc -w` echo “Num directory files are: $nfiles.” else echo “$0: argument file or directory” exit 1 endif exit 0 % if_demo3 Usage: if_demo3 file % if_demo3 foo If_demo3: argument file or directory % if_demo3 foo foobar Usage: if_demo3 file % if_demo3 if_demo3 NameInodeSize If_demo3 825745121 % if_demo3. Num directory files are: 3. % if_demo3 ~/unixbook Num directory files are: 81. %

30 SILICON VALLEY UNIVERSITY CONFIDENTIAL 30 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands The foreach Statement

31 SILICON VALLEY UNIVERSITY CONFIDENTIAL 31 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands Foreach loop variable values taken from the statement. Using $argv, foreach loop variable values taken from the command line.

32 SILICON VALLEY UNIVERSITY CONFIDENTIAL 32 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands The while Statement Variables and/or conditions in the expression must be manipulated in the ‘command-list’ to eventually terminate (expression evaluate to False).

33 SILICON VALLEY UNIVERSITY CONFIDENTIAL 33 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands % cat while_demo #!/bin/csh set secretcode = agent007 echo “Guess the code\!” echo -n “Enter your guess: “ set yourguess = `head -1` while (“$secretcode” != “$yourguess”) echo “Good guess but wrong. Try again\!” echo -n “Enter your guess: “ set yourguess = `head -1` end echo “Wow! You are a genius\!” exit 0 % while_demo Guess the code! Enter your guess: star wars Good guess but wrong. Try again! Enter your guess: columbo Good guess but wrong. Try again! Enter your guess: agent007 Wow! You are a genius! % Read from stdin ( keyboard ) one line. Another command to use is: “ set variable = $< “

34 SILICON VALLEY UNIVERSITY CONFIDENTIAL 34 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands The break, continue and goto Commands Transfers to “end” and reevaluates the while expression. Transfers out of the loop. Transfers to command at the label.

35 SILICON VALLEY UNIVERSITY CONFIDENTIAL 35 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands The switch Command Multiway branching, use when the nesting level of an if statement becomes deeper than three levels (i.e. using three else-ifs).

36 SILICON VALLEY UNIVERSITY CONFIDENTIAL 36 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands The switch Command

37 SILICON VALLEY UNIVERSITY CONFIDENTIAL 37 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands

38 SILICON VALLEY UNIVERSITY CONFIDENTIAL 38 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Program Control Flow Commands % switch_demo Use one of the following options: d or D: To display today’s date and present time l or L: To see the listing of files in your... w or W: To see who is logged in q or Q: To quit this program Enter your option and hit : d Sun Aug 15 00:59:07 PDT 2004 % switch_demo Use one of the following options: d or D: To display today’s date and present time l or L: To see the listing of files in your... w or W: To see who is logged in q or Q: To quit this program Enter your option and hit : w Dfrakes console Aug 14 22:18 Sarwar pts/0 Aug 14 23:41


Download ppt "Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 12 Dr. Jerry Shiao, Silicon Valley University."

Similar presentations


Ads by Google