C Shell Script Introduction - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

C Shell Script Introduction

Description:

var: number of words in var. Review: Command Arguments :0 command name :n argument number n ... end. foreach...end. foreach var [in list] statements. done ... – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 15
Provided by: a15159
Category:

less

Transcript and Presenter's Notes

Title: C Shell Script Introduction


1
Lecture 4
  • C Shell Script - Introduction
  • Array Variables

2
Shell script/program
  • Shell script a series of shell commands placed
    in an ASCII text file
  • Commands include
  • Anything you can type on the command line
  • Variables (including shell vars, env vars,
    command args) and Expressions
  • Control statements (if, while, for)

3
Script execution
  • Two ways to run a shell script
  • Launch a subshell with the script name as the
    argument. e.g. csh my_script.sh
  • Specify which shell to use within the script
  • First line of script is as !/bin/csh
  • Make the script executable using chmod
  • Make sure the PATH includes the current directory
  • Run directly from the command line
  • No compilation is necessary!

4
Shell Script Example
  • cat ltlt . gt ./hello.sh
  • ? !/bin/csh -f
  • ? echo Hello World
  • ? .
  • chmod x ./hello.sh
  • ./hello.sh

5
Variables
  • Shell/Environment Variables
  • Command Line Arguments
  • Array Variables

6
Review Predefined Shell Variables
  • argv list of argument passed to the current
    command
  • cwd full pathname of current dir
  • history number of commands saved in history
  • savehist number of history entries to save in
    /.history
  • home home dir ()
  • path lost of pathnames to search for commands
    to execute
  • shell name of shell in use (ex /bin/csh)
  • status exit status of last command

7
Review Shell Variables Expressions
  • Use some variables expression, we can also
    extract other info. about the shell variables
  • var or var value of variable var
  • ?var 1- if var is set 0 if var is not set
  • var1 first word in the value of var
  • var-10 words 1-10 in var
  • var2- words staring from word 2
  • var all words in the value of var
  • 0 name of the program being executed
  • lt read a line from stdin
  • var number of words in var

8
Review Command Arguments
  • 0 command name
  • n argument number n
  • - first argument
  • - last argument
  • n-m arguments n through m
  • n - arguments n through the last one
  • - All arguments

9
Array variables
  • Array is a list of values
  • To assign values to array variables, use ()
  • Reference a value by nameindex
  • a3
  • a (same as a0)
  • sports(ball frisbee puck)
  • Index is 1 based
  • Some of the predefined shell vars are array vars,
    such as argv (args), path (dirs)

10
Array Variable Examples
  • echo path
  • /bin /usr/bin /usr/local/bin /cs459_11/bin
  • echo path
  • 4
  • echo path0
  • echo path1
  • /bin
  • set numbers (one two three)

11
Expressions
  • C Shell expressions are used with _at_ or with
    (if/while) statements, where variable can be
  • Expression are formed by variables operators
  • _at_ operator assigns the value of arithmetic
    expressions to a variable
  • Example of _at_
  • set n2
  • _at_ an 2
  • _at_ a2
  • _at_ b (a gtgt 1) 1

12
Operators
  • Arithmetic operators
  • Assignment Operators ( , , -)
  • Comparison Operators (, !, lt)
  • Bitwise and logic operators (!, , ), (gtgt, ltlt)

13
File test operators (operator filename)
  • -d file the file is a dir?
  • -e file the file exists?
  • -f file the file is a plain file?
  • -o file the user owns the file
  • -r/w/x file the user has read/write/execute
    permission
  • -z file the file has 0 size
  • ! any above reverse the meaning

14
Control Statements
  • if thenelseendif
  • if condition
  • then
  • statements
  • else
  • statements
  • endif
  • Whileend
  • Syntax
  • while condition
  • statements
  • end
  • foreach...end
  • foreach var in list
  • statements
  • done
Write a Comment
User Comments (0)
About PowerShow.com