>Unix Shell Programming file N-UnixV_Shell.htm begin

This technote shows how to program the various shells


II Editing a file ********************************************************
<cat> : concatenes and print files -------------------------------------------------------------------------------- -u : output no buffered -s : silent about non existent file -v : causes non-printing characters to be printed visibly ^n ascii. the range 100-137 (@,A...Z) .. DEL -> ^? -t causes tab to be printed ^I -e $ is appended at each eol -------------------------------------------------------------------------------- <more> 17:40 22/06/1995 17:14 05/07/1995 -------------------------------------------------------------------------------- arguments -r print control character as ^XX -s Squeeze multi commands q quit xx^B back xx pages xxf skip xx full screen xxs skip xx lines

g [#x=BOF] Goto Line #x G [#x=EOF]

h help ^dqsf^ or ?dqsf? search dqsf backward /pattern/ search pattern forward - <enter> back one page -1 <space> ^B back one page $,G last page g first page . refresh display


<tail> print tail of file
-f to see file grow -c XX to ignore first XX carachtes -n to display the n last lines

see <tail sample>


-------------------------------------------------------------------------------- <cut> - cut out seletected fields of each line of a file 22/07/1996 12:41 -------------------------------------------------------------------------------- -clist [file ...] -flist [-dchar] -s [file....] where list is: A comma separated list of integer field number (in increasing number) with optional - to indicates ranges: 1,4,7 1-3,8 -5,10 (short for 1-5,10) -clist => list specifie characher position -flist => list of fiels assumed to be separated in the file by a delimiter. Lines without field delimiters will be passed through intact (usefull for table subheading); unless -s is specified -dchar => the character following -d is the field delimiter. default is tab. Space or other characters whith special meaning to the sell must be quoted -s => suppress lines with no delimiters ex: ls -lgc|cut -c33- ls -lgc|cut -c33-40 -------------------------------------------------------------------------------- <paste> - merge same lines of several files or subsequent line of one file -------------------------------------------------------------------------------- paste file1 file2 ... paste -dlist file1 file2 ... paste -s [-dlist] file file2 ...

In all cases, lines are glued together with tab character, or with chara cters from an optional list.

-d Without this option, the new-line are replace with tab characters. list One or more characters replace the default tab as line concate nation. The list is used circulary. The list may contain special character \t \n \\ \0. The line from the last file is forced to be a new-line characters, not from list. -s Merge subsequent line rather than one from each input file. Use tab for concatenation. Regardless of the list, the very last character of the file is forced to be a new-line -------------------------------------------------------------------------------- <pg> :[ -number] [-p string] [-cefnrs] [+linenumber][+/pattern/] [file ...] -------------------------------------------------------------------------------- a kind of more -number => #line of a window -c cls before -e no pause at eof -f avoid splitting lines -n enter= next page. ==> automatic end of command -p string: prompt string with %d== page number -r shell escaped disallowed -s print all messages and prompts in standout mode (not inverse video) +linenumber => start at line number +/pattern/ => start at first occurence of pattern action: h => help q or Q => Quit !command => command passed to the shell

newline or blank => next page $ => last page of the file

l xx => go to line (+offsetline,-offsetline,absoluteline) d or ^D => scrolling half a screen back or forth if xx Skeep i screens of text iz xx => newline but sreen height=xx . ^L => redisplay current page xx/pattern/ => search xx'th pattern xx^pattern^ xx?pattern? => search xx'th pattern backward s filename => save input in the named file -------------------------------------------------------------------------------- <regex> - string extractor --------------------------------------------------------------------------------

-------------------------------------------------------------------------------- <echo> 17:40 22/06/1995 17:14 05/07/1995 -------------------------------------------------------------------------------- affiche une ligne dans stdout

echo "mt -f /dev/rmt2.1 fsf 1" |tee mt.log|eval <tee> <eval>

echo automatique >tty

toto.sh set -x echo "mt -f /dev/rmt2.1 fsf 1" |tee mt.log|eval

echo Vers stdout echoit.sh echo $1 eval $1

Execution dans le meme environement while .fsf

-------------------------------------------------------------------------------- <regexp> 19/07/1996 14:05 -------------------------------------------------------------------------------- special character: \( \) \{ \} \1 \2 \3 \4 \5 \6 \7 \8 \9

\x -> regular character x [ \ * -> repetition of next char ^ $

Expression: . -> match any character except '\n'

Expression in [set]:

matching list:

[abc] -> match a,b,c

nonmatching list: [^abc] -> match everything but abc

. * [[.xx.]ab] -> match every: a,b,xx

[=xx=] -> equivalence class (meta [.xx.]

range expression: [xx-yy] => between xx and yy. si a est une classe A,a,B,b,Cc,ch,D alors [[=a=]-D] equivalent a [AaBbCc[.ch.]D]

[a-m-o] <=> [a-mm-o] [ac-] <=> a || >=c [^ac-] <=> !a, && <=c [--] <=> character -.

isCharcater c style: [:alpha:],[:lower:],[:upper:],[:digit:],[:xdigit:], [:alnum:],[:space:],[:print:],[:punct:],[:graph:], [:cntl:],[:blank:]

ex: 'include*."ft-mem"' -> recherche ts les includes ft- -------------------------------------------------------------------------------- <cmp> - compare two files -------------------------------------------------------------------------------- cmp [ -l ] [ -s ] file1 file2 -l print Position and octal diff -s print nothing

to compare two files ignoring the first bytes (Oracle export files) <tail sample> <awk sample>

#this scipt compares two set of oracle export file for i in avant$1*.dat; do j="apres"`expr \( substr "$i" 6 100 \)` echo $i $j #cat $i|awk 'begin {x=0}{if (0!=1) print;}'>avant.log #cat $j|awk 'begin {x=0}{if (0!=1) print;}'>apres.log tail -c +149 $i>avant.log tail -c +149 $j>apres.log ls -l $i $j avant.log apres.log cmp -l avant.log apres.log if [ $# != 0 ]; then echo $i differ from $j; diff avant.log apres.log fi #cmp $i $j done #file cmp.sh end of file
-------------------------------------------------------------------------------- ScoUnix<diff> - give diff between two files -------------------------------------------------------------------------------- => affiche les lignes de differences: -r recursif - -------------------------------------------------------------------------------- <expr> - test existence of file or compare strings -------------------------------------------------------------------------------- 3. To use part of a string, enter:

FLAG=`expr "$FLAG" : "-*\(.*\)"`

This removes leading hyphens, if any, from the $FLAG shell variable. The colon operator gives the part of the FLAG variable matched by the subexpression enclosed between \( and \) characters (backslash, open parenthesis and backslash, close parenthesis). If you omit the \( and \) subexpression characters, the colon operator gives the number of characters matched.

If the $FLAG variable is set to - (hyphen), the command displays a syntax error message. This happens because the shell substitutes the value of the $FLAG variable before running the expr command. The expr command does not know that the hyphen is the value of a variable. It can only see:

- : -*\(.*\)

and it interprets the first hyphen as the subtraction operator. To eliminate this problem, use:

FLAG=`expr "x$FLAG" : "x-*\(.*\)"`

ls |grep -c `expr "toto.frm" : "\(.*\)\..*"`

echo `expr \( substr "jkl" 2 100 \)` => jk -------------------------------------------------------------------------------- <test> - test existence of file or compare strings -------------------------------------------------------------------------------- many operators (file exist, kind of file, string equal ...) <[ ]>

    -b FileName Returns a True exit value if the specified FileName exists and is a block special file.

    -c FileName Returns a True exit value if the specified FileName exists and is a character special file.

    -d FileName Returns a True exit value if the specified FileName exists and is a directory.

    -e FileName Returns a True exit value if the specified FileName exists.

    -f FileName Returns a True exit value if the specified FileName exists and is a regular file.

    -g FileName Returns a True exit value if the specified FileName exists and its Set Group ID bit is set.

    -h FileName Returns a True exit value if the specified FileName exists and is a symbolic link.

    -k FileName Returns a True exit value if the specified FileName exists and its sticky bit is set.

    -L FileName Returns a True exit value if the specified FileName exists and is a symbolic link.

    -n String1 Returns a True exit value if the length of the String1 variable is nonzero.

    -p FileName Returns a True exit value if the specified FileName exists and is a named pipe (FIFO).

    -r FileName Returns a True exit value if the specified FileName exists and is readable by the current process. -s FileName Returns a True exit value if the specified FileName exists and has a size greater than 0.

    -t FileDescriptor Returns a True exit value if the file with a file descriptor number of FileDescriptor is open and associated with a terminal.

    -u FileName Returns a True exit value if the specified FileName exists and its Set User ID bit is set.

    -w FileName Returns a True exit value if the specified FileName exists and the write flag is on. However, the FileName will not be writable on a read-only file system even if test indicates true.

    -x FileName Returns a True exit value if the specified FileName exists and the execute flag is on. If the specified file exists and is a directory, the True exit value indicates that the current process has permission to search in the directory.

    -z String1 Returns a True exit value if the length of the String1 variable is 0 (zero).

    String1= String2 Returns a True exit value if the String1 and String2 variables are identical.

    String1!=String2 Returns a True exit value if the String1 and String2 variables are not identical.

    String1 Returns a True exit value if the String1 variable is not a null string.

    Integer1 -eq Integer2 Returns a True exit value if the Integer1 and Integer2 variables are algebraically equal. Any of the comparisons -ne, -gt, -ge, -lt, and -le can be used in place of -eq.

    These functions can be combined with the following operators:

    ! Unary negation operator

    -a Binary AND operator

    -o Binary OR operator ( that is, the -a operator has higher precedence than the -o operator)

    \(Expression\) Parentheses for grouping


<grep> UNIX 18/07/96 19/07/1996 14:05
    grep [-E|-F] [-c|-l|-q] [-insvx] pattern file ou bien: grep [-E|-F] [-c|-l|-q] [-binsvx] -e pattern1 -e pattern2 file ou bien: grep [-E|-F] [-câbc|-l|-q] [-binsvx] -f pattern_file -e pattern2 file

    arguments -E regular exp -F fixed string

    -c count only printed -l filename only -q doesn't echo anything

    -i ignore case

    -n print line number -s no error print -v print everthing but match -x exact match (line==)

    -b print block number

    -e expression: (see <regexp>) grepfs== grep -l ex:<find> find . -type f -print |xargs grep 'Variables accessible ' /dev/null | more ou bien: grep -E 'addcmd(*) grep -ilE 'include*."ft-mem' *.c *.h find / -name '*.h' -print |xargs grep 'rpc_$' /dev/null|more ls|grep -c '.*'

see <find><xargs>
<awk> lspv -l hdisk$1| cut -f 1 -d' '| awk '{printf("d.file_name like \047/dev/r%s%%\ 047 or\n",$1)}' >> ~hdd.sql echo 'xx'|awk '{for (i=0;i<256;i++) printf("%c",i);}'|uuencode - using awk + Field and Pattern matching: this sample extract from a construction script all the TABLESPACE line '/TABLESPACE/', eliminate the dos CR ([\015]) and the SQL & command cat bb_base_gbs.tab|awk -F'[&\015]' '/TABLESPACE/ {printf("define %s='${ORACLE_SID}'_INDEX",$3)}'>INDEX_def.sql

see awk sample awk sample 2 joker


<sed>
see <sed overview>

ex:

mk_user() { create_user $1 $2 $3 profile.user.$1.sh $4 gcos $2 $4 chuser home=$USERDATA_BASE/$1/$2 pgrp=$1 groups=sigp,dba7x shell="/bin/ksh" $2 sed -e "\?$2?s/ksh/ksh -r/" /etc/passwd>temp.passwd cat temp.passwd >/etc/passwd}
this example look for file in subdirectories having a sting and modify the text subst() { tmpfile=$TMP/sub.$RANDOM.txt echo $1 $2 $3 $tmpfile cat $3>$tmpfile echo >>$tmpfile sed -e "\?$1?s/$1/$2/g" $tmpfile> $3 #rm $tmpfile } find . -name "*.par" -exec echo var1 ascii1 {} \; find . -name "*.par" -print|xargs -n 1 echo var1 ascii1 $1 find . -name "*.par" -print|xargs -n 1 subst var1 ascii1 toto.par
<vi> : 12:03 22/06/1995 25/07/1997

see <vi overview> screen oriented (visual) display editor


    0 arguents
      -r read after system crash -L list crashed file -wn set default windows size to n -R read only mode -x encryption method Nota all command starting with : => esc Mode Command elsewhise=> direct command
    I moving
      h j k l character [<] [V] [^] [>] b w e Word (forward/backward/end) 0 + - $ Line (BOL,UP BOL,DWN BOL,EOL) ^F ^B page (Forward Backward)

      B W E forward backward blank end delimited word H L Same Screen (M for middle) % ( { } ) match sentence/paragraph ^E ^Y scroll line ^D ^U scoll screen (Down Up)

    II Commands
      II.A moving
        % find matching () or {} [ ] next/previous fonction `` move cursor to previous context '' move cursor to next non white line mx mark postion with mark character x `x 'x move cursor to mark x/ first non white line marked by x nG goto line n/EOF ^L redraw screen Verbose ^G show filename and line :ta tagCR goto tag 'tag'
      II.B finding / Replacing
        ?text /text search for text N n repeat/reverse last find Fx fx find next/previous x ; , repeat / reverse last find Tx tx move to characer previous following x n| move to column n s/a/b/ **** REPLACE A par B ***** :%s/A/B/c :43,256s/A/B/g Replace in the range 43-256 :Add1,Add2 see man ed address Add $ LastLine . Current Line :.,$s/SGPD/TEST/g Modifie position courante jusqu'a fin de fichier :.,+16s/SGPD/SGPM/g Modifie posistion courante à +16 ligne
      II.C editing
        cwNEWesc change word to NEW o i a insert (new,begin OL,EOL) r R replace char/LINE r R change char/rest of line s S substiture char /line J join line ^H X x D delete char (current,last) until End Of Line ^W dw delete word (current,last) dd 3dd delete line, delete 3 lines :43,250d Delete Line 43-250 :0,.d Delete Begining of File :.,;d Delete End Of File y 3yy cut (yank) , yank 3 lines "xp "xyy "xd paste, yank,delete to/from buffer x P p paste before after < > shift ^D ^^D 0^D backtab one char / to begin of line (no,no,reset autoident) II.D File u U undo, restore current line . repeat last change "dp retrieve d'th last delete ZZ exit and save :wq! write file (change attribute before) :q!cr quit no save :e read file :e! discard change,read file :w [filename] write :w! force write to initial r/o file :n next file in arg list :n arglistCR specify new arg list
      II.E Misc
        eaS pluralize word :sh run shell :!cmd run command then return ^V quote non printable char :ll= Line Limit= :nu prefix with line number :set report=## Minimum before reporting ##yy ~ Change Case of caracter
      II.F Macro:
        To define a macro, enter the sequence of subcommands into a buffer named with a letter of the alphabet. The lowercase letters a through z overlay the contents of the buffer, and the uppercase letters A through Z append text to the previous contents of the buffer, allowing you to build a macro piece by piece.

        For example, to define a buffer macro named c that searches for the word corner and makes the third line after the word corner the current line, enter the following command:

        o /corner/+3

        Then press the Esc key and enter the following command:

        "c

        where c is the name of the buffer macro. @T to use it @@ to reuse it

        Ex: Copier Coller entre plusieurs fichiers: vi test/orasrv.ini ../export/t?/orasrv.ini

        Esc"a2yy -> recopie deux lignes dans k -> monter une ligne "ap -> verifer contenu de a EscN! -> passer au fichiers suivants "ap -> Paste buffer a Escw EscN -> save + passer fichier suivant

        Modifier un ensemble de fichier via <for> file index.vi :%s/SIGP_I/sigp_index/g :x for


<pr>: print file
    -m => merge -t => no header ex: pr -mt file1 file2=> concatene les deux fichiers en deux collones

    +page => start at page -colunn => Nb of column of output -a => multi column across page -d => double-space the output -eck => expand input tab to space tabulated (k1...kn) using char c (c:=tab) -ick => in input replace space by tab at postion (k1..kn) using c -n => print line number -wwidth => set width of a line -ooffset=> set left margin -llength=> nb of line per page -h header=> header instead of filename -p => pause before each page -f => use FF instead of sequences of LF -r => no diagnostic if file cannot be open -ssepar => separate column with carachter separ -F => fold the line of the input file


<date> - print current date & time

    Unix: setting date [-a] [- ] sss.fff [-u] [[ mmdd]HHMM | mmddHHMM[cc]yy] slowly adjust the time by sss.fff seconds or set the date according to the following format: cc century yy year mm month dd day number HH heure MM min *** [-u] grenwich format

    Aix: setting date date [-n] [-u] date [+Field descriptor] echoing date date [-u] [+Field descriptor]

    -u: Universal date

    [+ format]: display wdate

    %y (0-99) %Y(0-9999) year %h=%b,%B month name (abreviated,full) %a,%A weekday name (abreviated,full) %w week day (sunday=0) %d (00-31) %e ( 1-31) month day %j(001-366) year day %U (00-53) %W (00-53) year week (Starting sunday or monday) %H heure 24 %I (12) %p AM,PM %M min %S seconds (0-61=leap) other %Z time zone name %c country specific date and time format %D %m/%d/%y %r %I:%M:%0= %%=% %n =\n nota: % is active when + is set

    to synchronise with server: see ntpdate et setclock

<ntpdate> synchronise with a time server the time
<setclock> synchronise with a server the time
setclock sigp1|awk -f setclock.awk|date

with <awk sample 2> setclock.awk is

    { mm="01"; if ("Feb"==$2) mm="02" else if ("Mar"==$2) mm="03" else if ("Apr"==$2) mm="04" else if ("May"==$2) mm="05" else if ("Jun"==$2) mm="06" else if ("Jul"==$2) mm="07" else if ("Aug"==$2) mm="08" else if ("Sep"==$2) mm="09" else if ("Oct"==$2) mm="10" else if ("Nov"==$2) mm="11" else if ("Dec"==$2) mm="12"; dd=$3; HH=substr($4,1,2); MM=substr($4,4,2); SS=substr($4,7,2); yy=substr($5,3,2); printf "%s%s%s%s.%s%s",mm,dd,HH,MM,SS,yy }
sample to allow user to change the system date:
    sudate.sh
      #file sudate.sh $Header: E:/users/ftu/sources/pvcsproj/website/FR_Aix/n_unixv/n_unixv_shell.htv 5.0 Jul 04 2001 22:01:30 fturi $ #$Log$ #set -x echo "calling user: $1" echo "whoami `whoami`" echo "id `id`" default="mmddHHMM[.SSyy]" #default="%m%d%H%M.%S%y" if [ "$3" != "" ]; then format=$3 else format=$default fi format="%m%d%H%M.%S%y" if [ "$2" = "-h" ]; then echo 'sudate.sh $Header: E:/users/ftu/sources/pvcsproj/website/FR_Aix/n_unixv/n_unixv_shell.htv 5.0 Jul 04 2001 22:01:30 fturi $' echo usage: su_date [ New Clock] [+Format] echo 'format default: <'$default'> value=<'$format'>' echo usage: synchronise clock to sigp1 if no date passed as argument date +$format elif [ "$2" = "" ]; then echo synchronisation avec sigp1 echo setclock sigp1 date `setclock sigp1|awk -f setclock.awk` else echo date to set : $2 date +$format echo date $2 +$format date $2 +$format fi #file sudate.sh fin

    su_date.c

      #include <sys/types.h> #include <unistd.h> #include <stdio.h> main(int argc,char **argv) { char l_ordre[80] ; /* initialisation des variables */ /* ----------------------------- */ switch (argc>1) { case 1: sprintf(l_ordre, "$AGL_HOME/bin/sudate.sh `whoami` %s ", argv[1]) ; break; case 2: sprintf(l_ordre, "$AGL_HOME/bin/sudate.sh `whoami` %s %s", argv[1],argv[2]) ; break; case 0: default: sprintf(l_ordre, "$AGL_HOME/bin/sudate.sh `whoami` ") ; break; } system(l_ordre); }

    cc -o $AGL_HOME/bin/su_date $AGL_HOME/src/su_date.c acledit $AGL_HOME/src/su_date

      attributes: SUID SGID base permissions owner(root): rwx group(system): --- others: --- extended permissions enabled permit --x u:svEJ

    use of it:

      () ft@sigp3:/u03/app/agl/product/1.0/bin>id uid=0(root) gid=0(system) () ft@sigp3:/u03/app/agl/product/1.0/bin>su svEJ () ft@sigp3:/u03/app/agl/product/1.0/bin>id uid=2462(svEJ) gid=2400(SGPV) groups=7000(dba7x),2000(sigp) () ft@sigp3:/u03/app/agl/product/1.0/bin>su_date 12121212.1212 calling user: root whoami root id uid=2462(svEJ) gid=2400(SGPV) euid=0(root) egid=0(system) groups=7000(dba7x),2000(sigp) date to set : 12121212.1212 08041456.4299 date 12121212.1212 +%m%d%H%M.%S%y 12121212.1212 () ft@sigp3:/u03/app/agl/product/1.0/bin>su_date calling user: root whoami root id uid=2462(svEJ) gid=2400(SGPV) euid=0(root) egid=0(system) groups=7000(dba7x),2000(sigp) synchronisation avec sigp1 setclock sigp1 Wed Aug 4 14:56:49 CUT 1999 () ft@sigp3:/u03/app/agl/product/1.0/bin>exit () ft@sigp3:/u03/app/agl/product/1.0/bin>id uid=0(root) gid=0(system)


##VI <File> manipulation ************************************************
<alias> 25/07/1996 11:43 25/07/1996 11:43 --------------------------------------------------------------------------------

2) les macros -------------- Pour ecrire macro: alias nom_macro texte Ou texte peut etre entre "" ou non

une chaine de caractere peut etre forme de '' ou de "" pour faire apparaitre "" a l'interieur chaine, on l'encadre de '' exemple: % alias toto 'echo "toto"' % toto toto %

<alias> 3) les parametres pour les macros ---------------------------------

Reference: arguments de la ligne: !* en ligne: !* en chaine: "\!*" exemple: % alias toto 'echo "parametres: \!*"' % toto a b c parametres: a b c % <$> Reference a un argument particulier: <reference>:x n.b. <reference> peut etre ! !! !# !$ !* !nr !-nr exemple % alias toto 'echo "p0: \!:0 p1: \!:1 p3 a p5:\!:3-5 2 premier\!:-1"' % toto a b c d e p0: toto p1: a p3 a p5:c d e 2 premiertoto a

Reference au dernier: !$ exemple % alias dernier 'echo "dernier :\!$"' % dernier a b c d e dernier :e

Reference a la ligne entiere : !# exemple % alias commande 'echo "Commande entre: \!#"' % commande toto weteww wet Commande entre: commande toto weteww wet

Resume: \!# ligne entiere \!* arguments \!$ dernier argument \!:3 troisieme argument (arg0 = cmd) \!:2- les deuxieme , troisieme ... EXCEPTE LE DERNIER !

\!! cmd precedente

4) concatenation de plusieures commandes: -----------------------------------------

deux possibilites:

a: les tubes

ex:alias lsmore ls | more

b: les redirections

ex: alias ls-fichier ls > fichier-de-sortie

c: l'utilisation du ; ex: %alias toto 'echo "premiere ligne"; echo "deuxieme ligne"' %toto premiere ligne deuxieme ligne %


##NOTES sur le shell <ksh> <POSIX> <Korn Shell> <AIX>
extrait de man Korn Shell <for> for i in *.inp; do diff $i v2/$i; done <test> <cmp> <diff> <if> for i in *.inp;do if [ -s v2/$i ]; then echo v2/$i;fi;done

for i in *.inp; do ( if [ -s v3/$i ]; then cmp -s $i v3/$i;ii=$?; if [ 0 -eq $ii ] ; then rm v3/$i; else echo "keep v3/$i"; fi; fi); done

for i in *.inp; do ( if [ -s v3/$i ]; then cmp -s $i v3/$i;ii=$?; if [ 0 -eq $ii ] ; then echo "rm v3/$i"; else echo "keep v3/$i"; fi; fi); done

for j in *.lck; do ( i=v2/`expr "x$j" : "x\(.*\)lck"`inp; #echo $j $i; if [ -s $i ]; then cmp -s $i $j;ii=$?; if [ 0 -eq $ii ] ; then rm $i #echo "diff $i $j"; else echo "keep $i"; fi; fi); done

for old in *.lck; do ( mid=`expr "x$old" : "x\(.*\)lck"`mid; new=`expr "x$old" : "x\(.*\)lck"`inp; ln $old $mid mv $old $new mv $mid $old ); done

for j in *.inp; do ( i=/u08/app/sigp/P/admsources/inp/$j if [ -s $i ]; then cmp -s $i $j;ii=$?; if [ 0 -ne $ii ] ; then echo "echo $i $j `diff $i $j|grep -c '.*'`;diff $i $j|head"; fi; fi); done|sh

<$> <~> Home directory <~+> Replaced by the $PWD variable (the full path name of the current directory).

<~-> Replaced by the $OLDPWD variable (the full path name of the previous directory).

In addition, the shell attempts tilde substitution when the value of a variable assignment parameter begins with a tilde ~ character.

You can also substitute arithmetic expressions by enclosing them in ( ) (parentheses). For example, the command:

<$()> see ksh <$> echo Each hour contains $((60 * 60)) seconds

produces the following result:

Each hour contains 3600 seconds

The Korn shell or POSIX shell removes all trailing new-line characters when performing command substitution. For example, if your current directory contains the file1, file2, and file3 files, the command:

echo $(ls)

removes the new-line characters and produces the following output:

file1 file2 file3

To preserve new-line characters, insert the substituted command in " " (double quotes):

echo "$(ls)"


##NOTES sur le shell <sh> <sh-posix>10:58 15/06/1995 19/07/1996 16:31
SH:

<definition>

    a blank is a tab or a space a name is a sequence of ASCII, digits, underscore a parameter is a name, a digit or any of *,@,£,?,-,$ and ! pipeline commands | commands list sequence of one or more pipelines separated by ;,&,&&,or || ; => sequentiel execution & => asynchronous execution && || => conditional execution (zero,nonzero) of preceding pipelines
<list definition>
    A list is a sequence of one or more pipelines separated by ;, &, &&, or ||, and optionally terminated by ;, &, or |&. Of these five symbols, ;, &, and |& have equal precedence, which is lower than that of && and ||. The symbols && and || also have equal precedence. A semicolon (;) causes sequential execution of the preceding pipeline; an ampersand (&) causes asynchronous execution of the preceding pipeline (that is, the shell does not wait for that pipeline to finish). The symbol |& causes asynchronous execution of the preceding command or pipeline with a two-way pipe established to the parent shell. The standard input and output of the spawned command can be written to and read from by the parent shell using the -p option of the special commands read and print described later. The symbol && (||) causes the list following it to be executed only if the preceding pipeline returns a zero (non-zero) value. An arbitrary number of new-lines can appear in a list, instead of semicolons, to delimit commands
(list) => execute list in sub shell { list } => execute list in current (that is parent) shell
    name () { list } define a function which is referenced by name => following commands are only recognized as first command: if then else elif case esac for while until do done { }
<Comment>: £ `` \` => escape `

<set> <$><${}>

    Parameter:
      character $ is used to introduce parameters name=value [name=value]
    ${parameter} $* $@ => all parameters from $1 ${parameter:-word} => (set(parameter)&&parameter?parameter:word) ${parameter:=word} => (!set(parameter)||!parameter?parameter=word:) ${parameter:?word} => (set(parameter)&&parameter?parameter:word) ${parameter:+word} => (set(parameter)&&parameter?word:NULL) ex: echo ${d:-`pwd`} => echo pwd if d is not set or is null if the colon(:) is omitted from the above expressions, the shell only checks whether parameter is set or not. parameter automaticaly set: £ number of arguments - flag supplied to the shell on invocation ? decimal value returned by last command $ process number of the shell
1) les variables ------------------
    Deux type de variables set var_locale = <liste valeur> setenv var_globale valeur

    Utilisation des variables

    ex:

    fturi /u2/fturi [44 ]>set ia="/u2/fturi/ia" fturi /u2/fturi [45 ]>cd $ia fturi /u2/fturi/ia [46 ]>

    <$><set> tester si une variable existe: commande $?nom_variable ex: %echo $?non-definie 0 %echo $?HOME 1

    Utilisation:

    if ( ! $?APPEL_SHELL ) then setenv APPEL_SHELL -1 endif

    @ _tempo = $APPEL_SHELL + 1 setenv APPEL_SHELL $_tempo unset _tempo

<environment variable>
    HOME default home directory for cd PATH : execution path CDPATH : search path for the cd command MAIL : if set name of the mail file MAILCHECK : how often the shell check the mail in seconds MAILPATH if a colon (:) separated list of different mail files. SHELL:
Blank interpretation explicit null argument "" or '' are retained. Implicit are removed <Input / Ouput >
    <word use file word for stdin (file descriptor 0) >word sdtout (file descriptor 1) >>word append to file <<[-]word

    see <rm><cp> after parameter and command substitution the shell input su read up to the first line that literally match word or eof if - is append 1) leading tabs are stripped from word 2) leading tabs are stripped from the shell input 3) shell input is read up to the first line matching word or eof If any character of word are quoted:, no, additionel processing is done from stdin If none: 1) parameters and commands substitution occured 2) escaped \newlines are removed 3) \ must be used to quote the character \,$ and ` <&digit use file descriptor as stdin >&digit stdout <&- stdin is closed >&+ stdout is closed

<File Name> generation
    ? wildcard * wildword [..] set
<Quoting>
    ; & ( ) Command: : nothing, 0 .file => read command from file and return break [n] exit a loop continue [n] resume the nth iteration cd [arg] echo
<eval> [arg]
    The arguments are read as input to the shell and resulting command(s) executed
<exec> [arg]
    The command specified by the argument is executed in place of this shell whithout crating a new process. I/O may appear and, if no other arguments are given, cause the shell i/o to be modified. exemple d'execution de type <awk>. #! /usr/bin/awk -f { for (i = NF; i > 0; --i) print $i } see <at> <cron><su>
<exit> [n]
    cause the shell to exit with error [n]. default=last error.
<export> [name ...]
    the given name is marked for automatic export to the environment of subsequently executed commands. if no arguments are given, variables anmes that have been marked for export durring the current shell's execution are listed. Function names are not exported.
<getopts>
    use in shell to support command syntax.parses and check valid option.
<hash> [-r] [name ...]
    for each name, the location in the search path is remembered by the shell. the -r option causes to forget all remembered location.
<if> [file1 -nt file2] (newer) comparer deux fichier: 12) Test divers ===============
    taille fichier nul <if> (-f fichier) then commande_pour_fichier_non_nul else commande_pour_fichier_nul if (-z fichier) then commande_pour_fichier_taille_nul else commande_pour_fichier_plein

    -r filename Return true, or 1 if the user has read access. Otherwise it returns false, or 0. -w filename True if the user has write access. -x filename True if the user has execute permission (or search permission on a directory). -e filename True if file exists. -o filename True if the user owns file. -z filename True if file is of zero length (empty). -f filename True if file is a plain file. -d filename True if file is a directory.

    If file does not exist or is inaccessible, then all inquiries return false.

    test if [ ... -eq ... ] then { .... } else fi

    Operateurs: -eq -ne

    if commande -> teste valeur retournee par main

    <read> name -> lire variable

for indentifier in [set ];do .. ;done <$> for f1 in *.ini;do echo $f1;diff $f1 ../t3.sco/$f1 ;done for identifier in *.ini;do echo $f1;done

extrait de man sh-posix

11) Utilisation de boucle dans un script

      foreach var (wordlist) end

      ex: alias toto 'foreach file (\!* ));'\ 'debut $file 10 ;'\ 'end' .... ne marche pas

<for> identifier [ in word ... ] ;<do> list ;<done> Each time for is executed, identifier is set to the next word taken from the in word list. If in word ... is omitted, for executes the do list once for each positional parameter set (see Parameter Substitution below). Execution ends when there are no more words in the list.

<select> identifier [ inword ... ] ;<do> list ;<done> A select command prints on standard error (file descriptor 2), the set of words, each preceded by a number. If in word ... is omitted, the positional parameters are used instead (see Parameter Substitution below). The PS3 prompt is printed and a line is read from the standard input. If this line consists of the number of one of the listed words, the value of the parameter identifier is set to the word corresponding to this number. If this line is empty, the selection list is printed again. Otherwise the value of the parameter identifier is set to NULL . The contents of the line read from standard input is saved in the parameter REPLY. The list is executed for each selection until a break or end-of-file is encountered. <case> word in [[(]pattern [| pattern] ...)list;;] ... <esac> A case command executes the list associated with the first pattern that matches word. The form of the patterns is identical to that used for file name generation (see File Name Generation below).

<if> list; <then> list; [<elif> list; <then> list;] ... [<else> list;] <fi> The list following if is executed and, if it returns a zero exit status, the list following the first then is executed. Otherwise, the list following elif is executed and, if its value is zero, the list following the next then is executed. Failing that, the else list is executed. If no else list or then list is executed, if returns a zero exit status.

<while> list; <do> list; <done> <until> list; <do> list; <done> A while command repeatedly executes the while list, and if the exit status of the last command in the list is zero, executes the do list; otherwise the loop terminates. If no commands in the do list are executed, while returns a zero exit status; until may be used in place of while to negate the loop termination test.

<()> (list) Execute list in a separate environment. If two adjacent open parentheses are needed for nesting, a space must be inserted to avoid arithmetic evaluation as described below.

<{}> {list;} Execute list, but not in a separate environment. Note that { is a keyword and requires a trailing blank to be recognized. <[[ expression ]]><[[]]><test> Evaluates expression and returns a zero exit status when expression is true. See Conditional Expressions below, for a description of expression. Note that [[ and ]] are keywords and require blanks between them and expression. see <test>

<function(){}> function identifier {list;} identifier () {list;} Define a function referred to by identifier. The body of the function is the list of commands between { and } (see Functions below).

time pipeline The pipeline is executedand the elapsed time, the user time, and the system time are printed on standard error.


<sh programming> Programmation du shell sh
5) Les noms de fichiers -----------------------
    reference absolue: /chemin ex:

    fturi /u2/fturi/ia [46 ]>cd /u2/fturi/image fturi /u2/fturi/image [47 ]>

    reference relative : cd chemin, ex:

    fturi /u2/fturi/image [47 ]>cd ../ia fturi /u2/fturi/ia [48 ]>

    reference a partir du repertoire maison cd ~/chemin ,ex: fturi /u2/fturi/ia [48 ]>cd ~/image/gif fturi /u2/fturi/image/gif [49 ]>

    reference au directory actuel, ex: cp souce .

    Utilisation des sous-ensembles es: ls cache=".[A-Z,a-z,1-0]*" donne les fichiers caches a l'exception de . et de ..

6) Utilisation de fonctions puissantes: ---------------------------------------
    Taches de fond: il suffit de rajouter & exemple

    fturi /u2/fturi [21 ]>ls &;jobs [1] 5262 [1] + Running ls fturi /u2/fturi [22 ]>Mail compte.init ia mbox unix2.hlp News config.sys image notes use-unix.txt autoexec.bat cs448 image.txt source_systeme xv bin cs486 joker.awk toto xwin compte.cmd dead.letter lettre unix1.hlp

    [1] Done ls fturi /u2/fturi [22 ]>

    Programmation de jobs:

    commande at: echo 'xmessage -message "Cours DataBase Mc4058"' | at 12:55 Monday

    Listes de ces jobs : atq Delete job : atd

7) Application avancee: -----------------------
    modification du prompt automatiquement: alias cd 'cd \!$;set prompt="$LOGNAME $cwd [\! ]>"'

    Programmation d'un joker par la commande awk: alias joker 'ls \!#:2 | awk -f ~/joker.awk cmd="\!#:1" post="\!#:3" > cmd' alias jren 'ls \!#:1 | awk -f ~/joker.awk cmd="mv -i" post="\!#:2" > cmd' alias ver 'type cmd' alias run 'source cmd'

    le programme joker.awk est le suivant

    { chaine=$0 for (i = 0 ; i<=length ; i++) if (substr(chaine,i,1)==".") point=i

    nom=substr(chaine,1,point) suff=substr(chaine,point,length) print cmd " " chaine " " nom post }

8)execution d'un fichier de commande:
    a) execution en variable locale: chmod o+x fichier fichier

    b) execution sous shell source fichier

    REMARQUES IMPORTANTES: TOUTES LES MACROS DOIVENT ETRE DEFINIES: soit dans le fichier .cshrc ou bien dans un autre fichier, pour y acceder: source fichier-d-alias

9) Remarques importantes ------------------------
    alias ren mv -i

    mettre tjs -i derrier mv pour eviter d'ecraser un fichier

10) Utilisation des chaines de carcateres et des listes =======================================================
    a) chaine simple ----------------- fturi /u2/fturi [168 ]>echo toto toto

    b) resultat de fonction ------------------------

    fturi /u2/fturi [169 ]><hostname> lagrange fturi /u2/fturi [170 ]>echo "le host est `hostname`" le host est lagrange

    Les "" et '' entre '' le texte n'est pas evalue: par exemple

    fturi@descartes /u2/fturi/News [100 ]>echo ' $1 $2 $3' $1 $2 $3

    mais: fturi@descartes /u2/fturi/image/new [144 ]>echo " $1 $2 $3 "

    fturi@descartes /u2/fturi/image/new [145 ]>


Notes sur le shell <CSH>: 10:58 15/06/1995 10:17 23/06/1995
Differences par rapport à sh: ps marche differement Comment utiliser les avantages de csh sans ses inconvenients:

echo ` commandes ... ` | csh csh -b > force q brak from option processing. This allow to run a script without confusion. -n parse but not execute -V verbose before .cshrc |& both stderr and std are redirected

<history>

    History substitution !!:1 == !:1 !!$ == !$

    set history=10 ! Start a history substitution, except whenfollowed by a space character, tab, newline, = or (. !! Refer to the previous command. By itself, this substitution repeats the previous command. !n Refer to command-line n. !-n Refer to the current command-line minus n. !str Refer to the most recent command starting with str. !?str[?] Refer to the most recent command containingstr. !{...} Insulate a history reference from adjacent characters (if necessary).

    Word Designators A `:' (colon) separates the event specification from the word designator. It can be omitted if the word designator begins with a ^, $, *, - or %. If the word is to be selected from the previous command, the second ! character can be omitted from the event specification. For instance, !!:1 and !:1 both refer to the first word of theprevious command, while !!$ and !$ both refer to the last word in the previous command. Word designators include:

    # The entire command line typed so far. 0 The first input word (command). n The n'th argument. ^ The first argument, that is, 1. $ The last argument. % The word matched by (the most recent) ?s search.

    x-y A range of words; -y abbreviates 0-y. * All the arguments, or a null value if there is just one word in the event. x* Abbreviates x-$.

    x- Like x* but omitting word $. Modifiers After the optional word designator, you can add a sequence of one or more of the following modifiers, each precededby a :.

    h Remove a trailing pathname component, leaving the head. r Remove a trailing suffix of the form `.xxx', leav- ing the basename. e Remove all but the suffix. s/l/r[/] Substitute r for l. t Remove all leading pathname components, leaving the tail. & Repeat the previous substitution.

    g Apply the change to the first occurrence of a match in each word, by prefixing the above (for example, g&). p Print the new command but do not execute it. q Quote the substituted words, escaping further sub- stitutions. x Like q, but break into words at each space charac- ter, tab or newline. Unless preceded by a g, the modification is applied only to the first string that matches l; an errorresults if no string matches. The left-hand side of substitutions are not regular expressions, but character strings. Any character can be used as the delimiter in place of /. A backslash quotes the delimiter character. The character&, in the right hand side, is replaced by the text from the left- hand-side. The & can be quoted with a backslash.A null l uses the previous string either from a l or from a contex- tual scan string s from !?s. You canomit the rightmost delimiter if a newline immediately follows r; the rightmost ? in a context scan can similarly be omitted. Without an event specification, a history reference refers either to the previous command, or to a previous history reference on the command line (if any). ^l^r[^] This is equivalent to the history substitution: !:s^l^r[^].

    :£ entire command line :0 first input word :n n'th argument :^ first argument :$ last argument

    ex: 9:cat script.sql 10: more !9:1-> more script.sql 11: tail !!:1

<redirection>: <>> <>!> <>&> <>&!>

delete and redirect

<>>> <>>!> <>>& > <>>&! >

append the standard output

<set> <$> $var ${var} variable substitution $n ${n} => argv[n] $* => argv[*]

at: -l list -r remove atq == at -l atrm == at -r atrm -a all Vi: x delete i insert :quit! :x

mail: dp delete and next msg d delete current msg h history of messages q quit and delete e exit without delete

arguments: -n notify: parse command but do not execute them -v verbose


C La valse des systemes ======================= 3 systeme: tcsh csh sh, le plus puissant etant tcsh. Pour executer un scripte sous tcsh,csh: source filename Pour executer un scripte sous sh: sh filename Remarque: read variable existe sous sh !! mais il y a pas grand chose d'autres (ni <alias> ni source ...) En theorie un scripte commencant par #!/bin/sh commence sous sh, mais bon en theorie ...


<X11>
<X11 misc tools>
    =====================

    Calculatrice : xcalc Courier : xbiff xbiff++ suivi de souris : xeyes (@lagrange) brouillon : xclipboard Horloge: oclock (fun) xclock (serieux) dclock (digital) sunclock (spatial) xcalendar dclock When the clock is running, the user may change attributes by typing: r Toggles Reverse Video. s Toggles the seconds display. b Toggles the bell attribute. j Toggles the jump/scroll attribute. d Toggles the date format. m Toggles the military time format. a Toggles the alarm clock. q quit the program.

    xpclock genial jeux,divers : maze muncher puzzle glouton:xchomp go:xgosh robots: xrobots xtetris


<X11 drawing tools>
    dessin: xfig(ye!) xpic xpainr --------XV affichage d'une image Quand fenetre arrive mal positionnee Bouton 1 normal Bouton 2 1/8 Bouton 3 Bouton 1 Coordonnes Bouton 2 glisser -> sous cadre Bouton 3 Pannaeau de controle

    q: quit e: edition couleur t: rotation 90 sens retograde h: symetrie axiale Y-Y v: symetrie axiale X-X n: normal >: +10% shift > : *2 shift < : /2 <: -10% 4: format 4/3

    i: info (cliquer dedans pour faire disparaitre) s: smooth (anti aliasing) c: recupere sous cadre

    ^L:load

    xloadimage VISION D'UNE IMAGE GIF (Bof !)

    Vision d'une page: xditview ou xdvi

    conversion dvi-> txt : dvi2tty -w 132 -p 1,3:6,8 *.dvi > filnename concatenation fichier dvi : dviconcat

    tex dviconcat (1) concatenate DVI files

    dviconcat *dvi > ref

    HELP

    tex textty (1) preview a TeX or LaTeX .dvi file on an ordinary ascii te rminal tex dvi2tty (1) preview a dvi-file on an ordinary ascii terminal tex texdumb (1) print TeX DVI files out on a readable, plain text file

    groff-1.04 grodvi (1) convert groff output to TeX dvi format pbmplus anytopnm (1) attempt to convert an unknown type of image file to a po rtable anymap pbmplus pnmsmooth (1) smooth out an image pbmplus ppmdither (1) ordered dither for color images pbmplus psidtopgm (1) convert PostScript "image" data into a portable graymap

    tex dviimp (1) translate TeX DVI files to Imagen Impress commands tex pfdviimp (8) lpr prefilters for TeX DVI to Imagen Impress output tex pfdviimpr (8) lpr prefilters for TeX DVI to Imagen Impress output screendump (1) - dump a frame-buffer image to a file screenload (1) - load a frame-buffer image from a file

    gourmants en memoire: xmahjongg xmartin --------------------

    mysterieux: xgc xpaste xplot mail: xmh

    messagerie: xgopher

    Utilisation de rn Commande sous prompt h: help l: list des groupes g: souscription a un groupe u: desouscription a un groupe j: fin lecture du fichier s: sauvegarde w: sauvegarde sans header Next n:news suivante N:groupe suivant Previous p:news precedente P:groupe precedent

    Commande durant lecture <espace> page suivante ^B page precedente j fin article

    groupe: alt.binaries.pictures alt.binaries.pictures.d

    alt.binaries.pictures.fractals

    alt.fractals.pictures

    rec.arts.sf-lovers rec.arts.sf.movies soc.culture.europe soc.culture.french uw.cs.cs446 uw.cs.cs448 uw.cs.cs486

2 Editeur: <xedit> ================
    - Enregistrer : Cliquez la boite a cote de |Quit|Save|Load| filename-box et tapez le nom du fichier puis cliquez (Boution droit) dans Save

    - Nouveau document: Taprez nom nouveau document puis cliquez Load

    - Effacer portion de texte Marquer texte avec bouton gauche <Ctrl> W

    -Effacement caractere GAUCHE <Ctrl> H caractere DROIT <Ctrl> D ligne BAS <Ctrl> K ligne HAUT

    jusqu'a FIN LIGNE <Alt> K jusqu'a DEBUT LIGNE

    mot GAUCHE <Alt> H mot DROIT <Alt> D

    - Rappeler texte efface <Alt> Y

    - Echanger deux lettres <Ctrl> T - Sortir plusieures lettres identiques <Ctrl> U lettre _ Inserer ligne blanche <Ctrl> O - Retour chariot <Ctrl> M - Deplacement dans le texte Gauche <Ctrl> B Droite <Ctrl> F

    Mot Gauche <Alt> B Mot Droit <Alt> F

    Tab <Ctrl> I

    Haut <Ctrl> P Bas <Ctrl> N Scrolling haut <Ctrl> Z Scrolling bas <Option>Z

    Fin de ligne <Ctrl> E Debut ligne <Ctrl> A Debut fichier <Alt> V Fin Fichier <Option>V

    - Recherche / Remplacement <Ctrl> R en arriere <Ctrl> S en avant

    - Raffraichir ecran <Ctrl> L

    - Insertion <Alt> I

    - Copier texte Marquer texte

    Bouton MEDIUM

<mouse control>
    1 Fonctionnement des fenetres: ==============================

    - Mettre sur le bureau une fenetre BOUTON MEDIUM sur barre fenetre - Mettre tout derriere fenetre Bouton DROIT sur barre fenetre - Modifier taille fenetre BOUTON DROIT ou GAUCHE sur symbole @ de la fenetre - Modifier position fenetre BOUTON DROIT ou GAUCHE sur barre

    - Se deplacer dans l'ascenceur Bouton gauche: une page vers le bas Bouton medium: vers souris Bouton droit : une page vers le haut

    _ Marquer un texte: Bouton gauche en le glissant

    Bouton gauche :debut texte Bouton droit :fin de texte ou elargissement

    Bouton gauche clique 2 fois: marque le mot Bouton gauche clique 3 fois: marque la ligne Bouton gauche clique 4 fois: marque le paragraphe (separe par ligne vide) Bouton gauche clique 5 fois: marque le texte en entier

C La valse des systemes ======================= 3 systeme: tcsh csh sh, le plus puissant etant tcsh. Pour executer un scripte sous tcsh,csh: source filename Pour executer un scripte sous sh: sh filename Remarque: read variable existe sous sh !! mais il y a pas grand chose d'autres (ni <alias> ni source ...) En theorie un scripte commencant par #!/bin/sh commence sous sh, mais bon en theorie ...

L'aide qui suit marche pour csh ...


Last Update : $Date: Jul 04 2001 22:01:30 $