Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 SILICON VALLEY UNIVERSITY CONFIDENTIAL 2 Summer 2015 Introduction UNIX/Linux Course Section 13 Discuss numeric data processing Describe array processing Discuss how standard input of a command in a shell script can be redirected to data within the script Explain signal/interrupt processing capability of the C shell

3 SILICON VALLEY UNIVERSITY CONFIDENTIAL 3 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Numeric Data Processing Declare numeric variables containing Integer Data. Perform arithmetic and logic operations on numeric integer data without converting string to numeric data and vice versa. Only Local Variables allowed.

4 SILICON VALLEY UNIVERSITY CONFIDENTIAL 4 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Numeric Data Processing

5 SILICON VALLEY UNIVERSITY CONFIDENTIAL 5 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Numeric Data Processing % @ value1 = 10 % @ value2 = 15 % echo “$value1 $value2” 10 15 % % @ difference = ($value1 - $value2) sum = ($value1 + $value2) % echo $difference $sum -5 25 % % @ results++ % @ results += 1 % @ results = ( $results + 1 ) % % set side = 10 area volume % @ area = $side * $side % @ volume = $side * $side * $side % echo $side $area $volume 10 100 1000 % Initializes two variables in the same line. Three commands increment variable by one. Variable initially created by the “set” command can store numeric data.

6 SILICON VALLEY UNIVERSITY CONFIDENTIAL 6 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Array Processing An array is a named collection of items of the same type stored in contigous memory locations Array items are numbered with the first item being 1 % set students = (David James Jeremy Brian Art Charlie) % echo $students David James Jeremy Brian Art Charlie % echo $#students 6 % echo $?students 1 %

7 SILICON VALLEY UNIVERSITY CONFIDENTIAL 7 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Array Processing % echo $students[2] James % set students[2] = “Mansoor Sarwar” % echo $students David Mansoor Sarwar Jeremy Brian Art Charlie % echo $students[2] Mansoor Sarwar %

8 SILICON VALLEY UNIVERSITY CONFIDENTIAL 8 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Numeric Data Processing % unset students % echo $students students: Undefined variable. % % set files = `ls` numfiles = `ls | wc -w` % echo $files cmdargs_demo foreach_demo1 if_demo1 if_demo2 keyin_demo % echo $numfiles 5 % echo $files[3] if_demo1 % The unset command can be used to deallocate the array variable. Array variable can be set with the command output.

9 SILICON VALLEY UNIVERSITY CONFIDENTIAL 9 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Numeric Data Processing % cat num_array_demo #!/bin/csh # Initialize Fibonacci array to any number of Fibonacci numbers - first ten in this case set Fibonacci = ( 0 1 1 2 3 5 8 13 21 34 ) @ size = $#Fibonacci # Size of the Fibonacci array @ index = 1 # Array index initialized to point to the first element @ sum = 0 # Running sum initialized to 0 while ( $index <= $size ) @ sum = $sum + $Fibonacci[$index] # Update the running sum @ index++ # Increment array index by 1 end echo “The sum of the given $#Fibonacci numbers is $sum.” exit 0 % num_array_demo The sum of the given 10 numbers is 88. % Array variable can be set with numeric data.

10 SILICON VALLEY UNIVERSITY CONFIDENTIAL 10 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Numeric Data Processing [sau@localhost chap17_18_dir]$ cat lab18p1 #!/bin/csh -xv # fs script file takes a directory as an optional argument and returns # the size (in bytes) of all ordinary files in it. If no directory name # is given at the command line, the script uses the current directory. if ( $#argv == 0 ) then set directory = "." else if ( $#argv > 1 ) then echo "Usage: $0 [directory name]" exit 0 else if (! -d $argv[1] ) then echo "Usage: $0 [directory name]" exit 0 else set directory = $argv[ 1 ] endif # set files = `ls $directory` @ nfiles = $#files #Number of files in directory. @ index = 1 #Initialize array index of array of file names. @ sum = 0 #Running sum. #!/bin/csh -xv used to run c shell with “echo” and “verbose” option.

11 SILICON VALLEY UNIVERSITY CONFIDENTIAL 11 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Numeric Data Processing while ( $index <= $nfiles ) set thisfile = "$directory"/"$files[$index]" if ( -f $thisfile ) then set argv = `ls -l $thisfile` @ sum = $sum + $argv[5] @ index++ else @ index++ endif end # if ( "$directory" == "." ) then set directory = "your current directory" endif echo "The size of all non-directory files in $directory is $sum bytes." exit 0 [sau@localhost chap17_18_dir]$./lab18p1 The size of all non-directory files in your current directory is 2060 bytes. [sau@localhost chap17_18_dir]$./lab18p1 lab18p1 Usage:./lab18p1 [directory name] [sau@localhost chap17_18_dir]$ ls -l total 8 -rwxrw-r--. 1 sau sau 1030 Dec 10 20:03 lab18p1 -rw-rw-r--. 1 sau sau 1030 Dec 10 19:56 lab18p1~

12 SILICON VALLEY UNIVERSITY CONFIDENTIAL 12 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course The Here Document The here document feature of the C shell allows you to redirect standard input of a command in a script and attach it to data within the script Works only with commands that use stdin Advantage:  Eliminates extra file operations when data on separate file.  Easier to maintain, one file with script and data.

13 SILICON VALLEY UNIVERSITY CONFIDENTIAL 13 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course The Here Document % cat heredoc_demo #!/bin/csh # First example cat << DataTag This is a simple use of the here document. These data are the input to the cat command. DataTag # Second example mail $argv[1] << WRAPPER Hello, This is a reminder for the weekly meeting tomorrow. Mansoor WRAPPER echo “Sending mail to $argv[1]... done” exit 0 % heredoc_demo eecsfaculty This is a simple use of the here document. These data are the input to the cat command. Sending mail to eecsfaculty... done %

14 SILICON VALLEY UNIVERSITY CONFIDENTIAL 14 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course The Here Document % more dext #!/bin/csh if ( $#argv == 0 ) theecho “Usage: $0 name” exit 1 else set user_input = “$argv[1]” grep -i “$user_input” \ \ << Directory_Data John Doe 555.232.0000 johnd@somedomain.edu Jenny Great 444.656.1111 jg@new.somecollege.edu David Nice 999.111.3333 david_nice@xyz.org Jim Davis 777.000.9999 davis@great.adviser.edu Art Pohm 333.000.8888 art.pohm@great.professor.edu Directory_Data endif exit 0 %./dext Pohm Art Pohm 333.000.8888 art.pohm@great.professor.edu %

15 SILICON VALLEY UNIVERSITY CONFIDENTIAL 15 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Interrupt (Signal) Processing Three possible actions that the process receiving the signal can take:  Accept the default action as determined by the UNIX kernel  Ignore the signal  Take a programmer defined action Hardware interrupt e.g keyboard interrupt( ) Signal received due to termination of a child process Accessing a main memory location that is not part of the process’s address space Software termination  kill command with/without signal number.

16 SILICON VALLEY UNIVERSITY CONFIDENTIAL 16 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Interrupt (Signal) Processing

17 SILICON VALLEY UNIVERSITY CONFIDENTIAL 17 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Interrupt (Signal) Processing Intercept and ignore  Transfer control to command(s) at “label:”.

18 SILICON VALLEY UNIVERSITY CONFIDENTIAL 18 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Interrupt (Signal) Processing % cat onintr_demo #!/bin/csh backagain: # Intercept and transfer control to the command at onintr interrupt set secretcode = agent007 # Set secret code echo “Guess the code\!” # Get user input 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 interrupt: # Code executed when you press echo “Nice try -- you cannot terminate me by \!” goto backagain % Label used for “goto” command. Read from stdin. Intercept terminal interrupt and goto the label. % onintr_demo Guess the code! Enter your guess: codecracker Good guess but wrong. Try again! Enter your guess: Nice try -- you cannot terminate me by ! Guess the code! Enter your guess: agent007 Wow! You are a genius! %

19 SILICON VALLEY UNIVERSITY CONFIDENTIAL 19 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Debugging Shell Programs The C shell programs can be debugged by using the –x and –v options of the csh command  x: Echo option. Displays each line of the script after variable substitution, but before execution.  v: Verbose. Displays each line of the script (as it appears in the script file) before execution. This allows viewing the commands in the user’s script after the variable substitution but before execution

20 SILICON VALLEY UNIVERSITY CONFIDENTIAL 20 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Interrupt (Signal) Processing % cat debug_demo #!/bin/csh echo -n “Enter a digit: “ set var1 = `head -1` if ( ( “$var1” >= 1 ) && ( “$var1” <= 9 ) ) then echo “Good input!!” endif exit 0 % debug_demo Enter a digit: 4 “: Event not found % Error occurs in the script.

21 SILICON VALLEY UNIVERSITY CONFIDENTIAL 21 Copyright @2005 Pearson Addison-Wesley Introduction UNIX/Linux Course Interrupt (Signal) Processing $ csh -xv debug_demo echo -n “Enter a digit: “ echo -n Enter a digit: Enter a digit: set var1 = `head -1` set var1 = `head -1` head -1 4 if ( ( “$var1” > = 1 ) && ( “$var1” < = 9 ) ) then if ( ( 4 > = 1 ) && ( 4 < = 9 ) ) then echo “Good input!!” “: Event not found $ Executing echo command has error. Looking at the syntax, there should be one “ ! “.


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

Similar presentations


Ads by Google