C Shell Scripts
Note: All the programs mentioned in this lecture are in:/home/cs352/SUMMER02/lecture24progs/
- Basic Syntax: - The first line must start with # - Comments alos begin with # - The rest of the syntax is the same as command line. - Conditional Statements - if (expr) then if (expr) then if (expr) then commands OR commands OR commands endif else else if (expr) then commands commands endif ... else commands endif Note: The expr is a comparison epxression constructed to comp. expressions in C, i.e. ==, !=, >, <, <=, etc. Similar to C the expr can contain logical operators &&, || - switch (str) case val1: commands breaksw case val2: commands breaksw ... default: commands breaksw endsw - Looping - foreach var_name ( wordlist ) commands end assigns each item in the wordlist to $var_name and executes the commands. - while (expr) commands end Note: We can also use break and continue in loops as in C. - How to pass arguments? - We can pass arguments to a script. - Inside the script: $#argv the number of arguments passed. $0 the name of the script. $argv[1] or $1 first argument. $argv[2-5] arguments from the 2nd to the 5th $argv all arguments see arg1_script see switch_script see foreach1_script - Assignments and Arithmetic Operations? - Assignment Operations only for integers: @ var operator expr OR @ var[index] operator expr where operator is one of = += -= *= /= ++ -- Ex: @ var = 5 + 2 @ var++ Note: Must put a space after @. Also before and after + But no space before ++ Note: It's illegal to use @ with string data, like @ var = mystring - Also Arithmetic, Bitwise and Logical Operators similar to C. Ex: @ var = $var + 10 see arith1_script see arg2_script see arith2_script see foreach2_script see while_script - How to Check File Properties? if (-operator file) where operator is one of r(read access), w(write), x(execute), e(existence), z(zero length), f(plain file-not a dir), d(directory) checks the appr. operator on file. see file1_script - How to Get Input Interactively? Using $< Ex: echo -n "Enter filename: " set file = $< reads in hatever is written as just one string. Ex: echo -n "Enter filenames: " set line = $< #line is only one string set files = ($line) #now files is a wordlist not a string. see file2_script