Thursday, October 9, 2008

cshell script

basic

Shell variables can be created using the set name=value construct; they are henceforth referenced by prepending the shell variable name with a dollar: say, echo $name

variable

Variables can be used in C shell by typing a dollar sign ($) before the variable name. If the variable is an array, the subscript can be specified using brackets, and the number of elements can be obtained using the form $#var2.

The existence of variables can be checked using the form $?variable
     if (! $?var) set var=abc

Simple integer calculations can be performed by C shell, using C language-type operators. To assign a calculated value, the @ command is used as follows: 
@ var = $a + $x * $z

quotions

Ticks don't recognise certain characters in the shell. Ticks don't honour special characters such as the dollar sign. (')

we can use ticks and quotes interchangeably unless we need to honour special characters in the shell.(")

You can always use the backslash to quote a character. However, within the single quote mechanism, "\'" does not "quote the quote." The proper way to do this is as follows: 
% echo 'Don' \' 't do that'
Don ' t do that

The purpose of a backtick is to be able to run a command, and capture the output of
that command.  (`) Say:DATE=`date`

job control

Processes may be started in the background by following the command with an ampersand (&).

When a job is placed in the background, information for the job is shown similar to the example given below: 
[1] 15934

This specifies that the process has been placed in the background, and is job 1. In order to recall jobs placed in the background, the fg command is used, while the bg command places a recently stopped process into the background. The jobs command gives a list of all processes under control of the current shell. Also, typing a percent sign (%) with the job number brings that particular job to the foreground.

file judgement

=~     If the right hand side matches a pattern, (i.e., similar to filename matching, with asterisks and question marks.) the condition is true.

 !~     If the right hand side doesn't match a pattern, the condition is true.

-d $var     True if the file is a directory.

-e $var     True if the file exists.

-f $var     True if the file is a file. (I.e., not a directory)

-o $var     True if the file is owned by the user.

-r $var     True if the user has read access.

-w $var     True if the user has write access.

-x $var     True if the user has execute access.

-z $var     True if the file is zero-length.

parameters

Command line arguments are in special shell variables 0, 1, 2 etc. $0 is the name of the script, $1 the first argument (if present), etc. All command line arguments $1, $2,.. are also pre-defined in an shell variable argv, which is actually a list (like an array). It can be referenced as $argv[1], $argv[2], ... etc. This form has the advantage that the construct $#argv (the same as $*) can be used to find the number of elements in the list

$$ is a way of referring to the process number of the current shell. The
characters $$ are often used as part of a filename in order to generate a
unique name for a temporary file.

Looping in a shell script

The C-shell includes the following
constructs: if, switch, foreach, while and goto.

regular expression is supported, e.g. foreach varname (List*)

repeat 100 DoSth

foreach VariableName (SomeList)
    command1
    command2

    if (something1) break

    if (something2) continue
        ...
    commandn

end

done:

switch ($year)
case 92:
    set lyf = 1
    set soyf = 1
    breaksw
case 93*:
    set lyf = 0
    set soyf = 3
    breaksw
endsw

while ( expr )
    statement_list
end

a list is words separated with comma,i.e. ","

Labels are defined by a name, followed by a colon, label: jumping to a label is be done by using goto label.

Decision making: using the if command

simple format: if (expression) command

if ( ! -d mydir &&  $a== `USER`) then
    commands
else if ( expression ) then
    commands
else
    commands
endif

else if can be replace with elif

return value

In terms of any programming language one has the boolean operators, true and false.

In the shell, being "true" is represented by a 0 (zero) and anything else is
false. use $? to get the return value. say: echo $?

Aliases 

Aliases provide a way of conveniently recalling frequently used commands with a user-defined shorthand name. The alias statement associates a list of words with an alias name. The "=" is optional. Parentheses are used around the wordlist if it contains special characters such as i/o redirection operators that should be part of the alias definition. 

alias name [ = ] ( wordlist )
alias name [ = ] wordlist 

alias name prints the definition of that alias; alias pattern prints the definitions of all the aliases whose names match the pattern. alias without any arguments prints the definitions of all the aliases. 

alias
alias name
alias pattern 

unalias namelist discards the specified aliases; unalias pattern discards all the aliases whose names match the pattern. 

unalias namelist
unalias pattern

Procedures 

this may be of problem
Procedures defined by the proc statement can recursively call other procedures. They can be referred to inside an expression or as a new command, in which case any value returned is written to stdout. There is an implicit return statement at the end of every procedure definition. 

proc name ( [ namelist ] )
    statement_list
return [ expr ]
end 

The proc statement with no arguments prints a list of all the procedures that have been defined; if the argument is a name, that one procedure is listed; if the argument is a pattern, all procedures whose names match the pattern are listed. 

proc
proc name
proc pattern 

unproc namelist (where namelist is a series of names separated by commas) discards the specified procedures. unproc pattern discards all procedures whose names match the pattern are discarded. 

unproc namelist
unproc pattern



sed usage:

Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]
-n, --quiet, --silent
    suppress automatic printing of pattern space
-e script, --expression=script
    add the script to the commands to be executed
-f script-file, --file=script-file
    add the contents of script-file to the commands to be executed
-i[SUFFIX], --in-place[=SUFFIX]
    edit files in place (makes backup if extension supplied)
-c, --copy
    use copy instead of rename when shuffling files in -i mode
    (avoids change of input file ownership)
-l N, --line-length=N
    specify the desired line-wrap length for the `l' command
--posix
    disable all GNU extensions.
-r, --regexp-extended
    use extended regular expressions in the script.
-s, --separate
    consider files as separate rather than as a single continuouslong stream.
-u, --unbuffered
    load minimal amounts of data from the input files and flush the output buffers more often
--help display this help and exit
--version output version information and exit

To delete trailing whitespace from end of each line, enter:
    $ cat input.txt | sed 's/[ \t]*$//;s/abc/def/' > output.txt
to delete all empty lines from a file called /tmp/data.txt, enter:
    $ sed '/^$/d' /tmp/data.txt
GNU Sed support -i option to edit files in place:
    $ sed -i '/Windows/d' /tmp/data.txt

No comments: