Showing posts with label cshell. Show all posts
Showing posts with label cshell. Show all posts

Wednesday, May 25, 2011

cshell redirection

Character Action

>
Redirect standard output

>&
Redirect standard output and standard error

<
Redirect standard input

>!
Redirect standard output; overwrite file if it exists

>&!
Redirect standard output and standard error; overwrite file if it exists

|
Redirect standard output to another command (pipe)

>>
Append standard output

>>&
Append standard output and standard error

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

Wednesday, September 24, 2008

cshrc

####################################################
  #!/bin/tcsh
unset autologout
  set ignoreeof
  set filec
  set history = 200
  set osversion=`uname`
  xmodmap -e 'keycode 22 = BackSpace'
   
  setenv MANPATH /usr/man:/usr/openwin/man
   
  setenv PATH .
  setenv PATH $HOME/bin:$PATH
  setenv PATH /bin:$PATH
  setenv PATH /sbin:$PATH
  setenv PATH /usr/bin:$PATH
  setenv PATH /usr/sbin:$PATH
  setenv PATH /usr/local/bin:$PATH
  setenv PATH /usr/X11R6/bin:$PATH
  setenv PATH /magnum/tools/novas/verdi-10.2/bin:$PATH
  setenv PATH /magnum/tools/cadence/RC62USR2/bin:$PATH

####################################################

#! /bin/csh -fset osversion=`uname`alias a aliasa cls cleara ls ls -F --colora acroread "acroread \!* &"a l lsa L lsa LS lsa lcd lsa ld 'ls -d \!*|\grep /$'#a ld '\ls --color -F1 | \grep /$'#a ld 'find . -type d -maxdepth 1'a lc 'ls -1 \!*|grep -c ""'a lls lla llt ll -rta le lessa f finda fm "ftp sb1500-001.magnumsemi.com"a h historya h1 head -1a h2 head -2a k kill -9a kd "kdiff3 \!* &"#a g xvia g gvima gd g -da go g -oa gO g -Oa gt g --remote-tab-silenta vo v -oa vO v -Oa grep grep --colora gc grep --colora gl grep -la glw grep -lwa gw grep -wa pd pushda rf rm -fa v via vr vi -Ra qk 'qstat |\grep -w "\!*"|awk "{print $1}"|xargs qdel'a x 'xterm -bd "#7f7f00" -fg "#7f7f7f" -bg black -cr "#ff7f7f" -geometry 101x35 -fn 9x15bold -sl 2000 &'a qx 'Q xterm -bd "#7f7f00" -fg "#7f7f7f" -bg black -cr "#ff7f7f" -geometry 101x35 -fn 9x15bold -sl 2000 &'a qxf 'Q xterm -bd "#7f7f00" -fg "#7f7f7f" -bg black -cr "#ff7f7f" -geometry 101x35 -fn 9x15bold -sl 2000'a so sourcea CD cda cdl "cd ~/goto_lynley;l"#a cdc "cd ~/thunder2/run/r3ksource/csrc/test"#a cds "cd ~/thunder2/run/sim"a cdh "cd ~/thunder2/hdl"a . pwda .. "cd .."a ... "cd ../.."a .... "cd ../../.."a ..... "cd ../../../.."a ...... "cd ../../../../.."a Q "qrsh -l mem_free=25G -l num_proc=4 -now no -V -noshell -cwd \!*"a Qsh 'echo "start at `date`"; qrsh -l mem_free=25G -l num_proc=4 -now no -V -noshell -b y -cwd -l lic_sim=1 -l simh \!*;echo "finish at `date`"'#a Q 'qrsh -l mem_free=25G -l num_proc=4 -now no -noshell -cwd \!*'a udc "unsetenv DISPLAY"a la ls -aa lt ls -t -la ll ls -la lm 'll |more'a lr 'ls -ltd'a pvnc 'ps aux |grep vnc'a rm rm -ia boo '\rm #*; \rm *~'a cdd cd ..a cp cp -ipa nc ncveriloga h history#a gvim gvim -reverseunalias test### hg/mercurial relateda hl "hg log|more"a hv hg viewa hs "hg status|grep -v ^\?"a hjs "hg jstatus|grep -v ^\?"a hgg "hg glog|grep ^@; echo 'latest hg log:';hg log|head -2"a hj "hg jpull |tee ~/hg.log; hg jupdate |tee -a ~/hg.log;date"set HOSTNAME=`hostname`if ($HOSTNAME =~ *bj*) then a hjpu 'hg jrebase /proj/lynley/ws/hgmirror/mrepo/mrepo > /dev/null;set startTime=`date`;hg jpull --root \!*|tee ~/hg.log; hg jupdate --root \!*|tee -a ~/hg.log;date; hg jrebase ssh://$USER@mrepo//magnum/hg/mrepo > /dev/null;\echo -e "hjpu started at\t$startTime\n\tended at\t`date`"' a hjpuc 'hg jrebase /proj/lynley/ws/hgmirror/mrepo/mrepo > /dev/null;set startTime=`date`;hg jpull --root \!*|tee ~/hg.log; hg jupdate -C --root \!*|tee -a ~/hg.log;date; hg jrebase ssh://$USER@mrepo//magnum/hg/mrepo > /dev/null;\echo -e "hjpu started at\t$startTime\n\tended at\t`date`"'else a hjpu 'set startTime=`date`;hg jpull --root \!*|tee ~/hg.log; hg jupdate --root \!*|tee -a ~/hg.log;date; \echo -e "hjpu started at\t$startTime\n\tended at\t`date`"' a hjpuc 'set startTime=`date`;hg jpull --root \!*|tee ~/hg.log; hg jupdate -C --root \!*|tee -a ~/hg.log;date; \echo -e "hjpu started at\t$startTime\n\tended at\t`date`"'endifa hjpul 'hjpu hw/fe/Lynley/proj/dev@\!*'a hjpui 'hjpu hw/fe/interconnect/verif/dev@\!*'a hjpuit 'hjpui tip'a hpu "hg pull \!*; hg update \!*"a simvision 'simvision -waves \!* &'a sv "simvision waves.shm/waves.trn"if ($osversion == "Linux") then set mach_type="`uname -m`" a vi vim #alias vi "/home/lfchen/bin/vim -v" #alias vim "/home/lfchen/bin/vim" #alias vi "/usr/bin/vim -v" #alias vim "/usr/bin/vim" #alias vi "/usr/X11R6/bin/gvim -font MiscFixed -reverse" #alias vim "/usr/X11R6/bin/gvim -font MiscFixed -reverse" #alias gvim "/usr/X11R6/bin/gvim -font MiscFixed -reverse" alias xterm "/usr/X11R6/bin/xterm" #setenv VIMRUNTIME "/cirrus/linuxapps/local/share/vim/runtime" #setenv VIMRUNTIME "/home/lfchen/share/vim/runtime" alias xvi vim -g #if ($mach_type == "x86_64") then #alias xvi "/cirrus/linuxapps/local/bin/bit64/vim -g" #endifendif## for xml flowa cdroot "so ~lfchen/scripts/mgnmFlow/cdRoot"a cdr "cdroot;ssm"a ssm "source ~lfchen/scripts/mgnmFlow/set_mrepo"## set my personal favorite IPNAME by force. But I don't need to override now.#setenv IPNAME interconnectsetenv ipname interconnectsetenv intf hw/fe/common/rtl/interfaces/devsetenv tiwir mtools/veriTools/dev/tiwirsetenv tests hw/tests/$ipname/dev/svsetenv verif hw/fe/$ipname/verif/devsetenv run $verif/runsetenv src $verif/srcsetenv rtl hw/fe/$ipname/rtl/devsetenv xml xml/impl/ip/$ipname/devsetenv ovm hw/fe/common/verif/ovmsetenv ovmahb $ovm/ahb/dev/08.20.002-s/vr_ahb/svsetenv ovmocp $ovm/ocp/dev/srcsetenv ovmmcbus $ovm/mcbus/dev/srcsetenv ovmcbus $ovm/cbus/dev/srca cdmem 'cdr;cd hw/fe/common/memories/Lynley/dev'a cdgen 'cdr;cd mtools/generators/dev'a cdinc 'cdr;cd hw/fe/Lynley/verif/dev/common/include'a cdinc 'cdr;cd hw/fe/common/rtl/interfaces/dev'a cdxml 'cdr;\\ if (a"\!*" != a"") \\ cd xml/impl/ip/\!*/dev;\\ if (a"\!*" == a"") \\ cd xml/impl/ip/$ipname/dev;\\ 'a cdrtl 'cdr;\\ if (a"\!*" != a"") \\ cd hw/fe/\!*/rtl/dev;\\ if (a"\!*" == a"") \\ cd hw/fe/$ipname/rtl/dev;\\ 'a cdsdc 'cdr;\\ if (a"\!*" != a"") \\ cd hw/fe/\!*/gate/asic/dev;\\ if (a"\!*" == a"") \\ cd hw/fe/$ipname/gate/asic/dev;\\ 'a cdrun 'cdr;\\ if (a"\!*" != a"") \\ cd hw/fe/\!*/verif/dev/run;\\ if (a"\!*" == a"") \\ cd hw/fe/$ipname/verif/dev/run;\\ 'a cdverif 'cdr;\\ if (a"\!*" != a"") \\ cd hw/fe/\!*/verif/dev;\\ if (a"\!*" == a"") \\ cd hw/fe/$ipname/verif/dev;\\ 'a cdsrc 'cdr;\\ if (a"\!*" != a"") \\ cd hw/fe/\!*/verif/dev/src;\\ if (a"\!*" == a"") \\ cd hw/fe/$ipname/verif/dev/src;\\ 'a cdtests 'cdr;\\ if (a"\!*" != a"") \\ cd hw/tests/\!*/dev/sv;\\ if (a"\!*" == a"") \\ cd hw/tests/$ipname/dev/sv;\\ 'setenv shortName `echo $HOSTNAME|cut -f1 -d"."`#try DISPLAYa d 'echo $DISPLAY'a sdf 'echo $DISPLAY >! ~/.display'a sdfe 'echo $DISPLAY |tee ~/.display'set myTTY=`who am i | awk '{ print $2 }'`set myHOSTNAME=`w | grep -w "$myTTY" | awk '{ print $3 }'`set myVNC=94if (! -f ~/.display) touch .displaya vdc "setenv DISPLAY ${myHOSTNAME}:$myVNC.0; sdf" #engvnca pdc "setenv DISPLAY 172.18.20.31:0; sdf" #bj pca mdc "setenv DISPLAY milcitrix.ad.magnumsemi.com:0.0; sdf" #172.16.4.229:0a gdc "setenv DISPLAY ${USER}-pc.ad.magnumsemi.com:0.0; sdf" #172.16.4.165:0a mvnc vncserver -geometry 1350x1000 :94### personal settingsif ($USER != lfchen) exitif ("$myHOSTNAME" =~ milfarm*) then setenv DISPLAY `cat ~/.display` #echo login from $myHOSTNAMEelse if ("$myHOSTNAME" =~ engvnc*) then vdc sdfelse if ("$myHOSTNAME" =~ milcitrix*) then mdc sdfelse if ("$myHOSTNAME" =~ ${USER}*) then gdc sdfelse #login from pc if ($HOSTNAME =~ *bj*) then pdc else gdc endif sdfendif
###########DISPLAY SET###########################
alias sdp setenv DISPLAY 172.16.18.5:10

if ($osversion == "Linux") then
  set mach_type="`uname -m`"
  alias pscheck "/bin/ps -Af | grep -v root"
else
  alias pscheck "/bin/ps -Af -o pid,etime,user,args | grep -v root"
endif

####################################################

#


set cd_cnt=0
alias sp 'set prompt="%{\033[33;44m%}\\![%.3\]%{\033[0m%}:\ ";\\
if("$shortName" !~ milfarm11*) set prompt="$shortName $prompt";\\
if("$cd_cnt" >= 20) \echo -e "PWD: \033[1;33;41m$PWD\033[0m";\\
if("$cd_cnt" >= 20) set cd_cnt=0\\
'
sp
alias cd 'chdir \!* ; sp; setenv PWD `pwd`; set cd_cnt=`echo $cd_cnt+1|bc -l`;' # redefine cd command

alias pushd 'if("\!:0-$" == pushd) pushd;\\
if("\!:0-$" != pushd) pushd \!$;\\
sp'
alias popd 'if ("\!:0-$" == popd) popd;\\
if ("\!:0-$" != popd) popd \!$;\\
sp'

####################################################
content of .dir_colors

# Configuration file for the color ls utility
# This file goes in the /etc directory, and must be world readable.
# You can copy this file to .dir_colors in your $HOME directory to override
# the system defaults.

# COLOR needs one of these arguments: 'tty' colorizes output to ttys, but not
# pipes. 'all' adds color characters to all output. 'none' shuts colorization
# off.
COLOR tty

# Extra command line options for ls go here.
# Basically these ones are:
# -F = show '/' for dirs, '*' for executables, etc.
# -T 0 = don't trust tab spacing when formatting ls output.
OPTIONS -F -T 0

# Below, there should be one TERM entry for each termtype that is colorizable
TERM linux
TERM console
TERM con132x25
TERM con132x30
TERM con132x43
TERM con132x60
TERM con80x25
TERM con80x28
TERM con80x30
TERM con80x43
TERM con80x50
TERM con80x60
TERM cons25
TERM xterm
TERM rxvt
TERM xterm-color
TERM color-xterm
TERM vt100
TERM dtterm
TERM color_xterm
TERM ansi
TERM screen
TERM screen.linux
TERM kon
TERM kterm
TERM gnome
TERM konsole

# EIGHTBIT, followed by '1' for on, '0' for off. (8-bit output)
EIGHTBIT 1

# Below are the color init strings for the basic file types. A color init
# string consists of one or more of the following numeric codes:
# Attribute codes: 
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
NORMAL 00 # global default, although everything should be something.
FILE 00 # normal file
DIR 04;36 # directory
LINK 01;36 # symbolic link
FIFO 40;33 # pipe
SOCK 01;35 # socket
BLK 40;33;01 # block device driver
CHR 40;33;01 # character device driver
ORPHAN 01;05;37;41 # orphaned syminks
MISSING 01;05;37;41 # ... and the files they point to

# This is for files with execute permission:
EXEC 01;32 

# List any file extensions like '.gz' or '.tar' that you would like ls
# to colorize below. Put the extension, a space, and the color init string.
# (and any comments you want to add after a '#')
.cmd 01;32 # executables (bright green)
.exe 01;32
.com 01;32
.btm 01;32
.bat 01;32
.sh 01;32
.csh 01;32
.tar 01;31 # archives or compressed (bright red)
.tgz 01;31
.arj 01;31
.taz 01;31
.lzh 01;31
.zip 01;31
.z 01;31
.Z 01;31
.gz 01;31
.bz2 01;31
.bz 01;31
.tz 01;31
.rpm 01;31
.cpio 01;31
.jpg 01;35 # image formats
.gif 01;35
.bmp 01;35
.xbm 01;35
.xpm 01;35
.png 01;35
.tif 01;35