Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced C Shell Programming

Similar presentations


Presentation on theme: "Advanced C Shell Programming"— Presentation transcript:

1 Advanced C Shell Programming
Chapter 18 Advanced C Shell Programming

2 Objectives To discuss numeric data processing
To describe array processing To discuss how standard input of a command in a shell script can be redirected to data within the script To explain signal/interrupt processing capability of the C shell Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

3 Numeric Data Processing
Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

4 Numeric Data Processing
Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

5 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 Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

6 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 % Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

7 Array Processing % echo $students[2] James
% set students[2] = “Mansoor Sarwar” % echo $students David Mansoor Sarwar Jeremy Brian Art Charlie Mansoor Sarwar % Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

8 Array 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 Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

9 Array Processing % cat num_array_demo #!/bin/csh
# Initialize Fibonacci array to any number of Fibonacci numbers - first ten in this case set Fibonacci = ( ) @ 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. % Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

10 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 Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

11 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 Sending mail to eecsfaculty ... done % Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

12 The Here Document % more dext #!/bin/csh if ( $#argv == 0 ) then
echo “Usage: $0 name” exit 1 else set user_input = “$argv[1]” grep -i “$user_input” \ \ << Directory_Data John Doe Jenny Great David Nice Jim Davis Art Pohm Directory_Data endif exit 0 % Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

13 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(<Ctrl-C>) 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 Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

14 Interrupt (Signal) Processing
Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

15 Interrupt (Signal) Processing
Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

16 Interrupt (Signal) Processing
% cat onintr_demo #!/bin/csh backagain: # Intercept <Ctrl-C> 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\!” end echo “Wow! You are a genius\!” exit 0 interrupt: # Code executed when you press <Ctrl-C> echo “Nice try -- you cannot terminate me by <Ctrl-C>\!” goto backagain % Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

17 Interrupt (Signal) Processing
% onintr_demo Guess the code! Enter your guess: codecracker Good guess but wrong. Try again! Enter your guess: <Ctrl-C> Nice try -- you cannot terminate me by <Ctrl-C>! Enter your guess: agent007 Wow! You are a genius! % Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

18 Debugging Shell Programs
The C shell programs can be debugged by using the –x and –v options of the csh command This allows viewing the commands in the user’s script after the variable substitution but before execution Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

19 Debugging Shell Programs
% 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 % Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

20 Debugging Shell Programs
$ 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 $ Copyright © 2005 Pearson Addison-Wesley. All rights reserved.


Download ppt "Advanced C Shell Programming"

Similar presentations


Ads by Google