Pages

Affichage des articles dont le libellé est tcsh. Afficher tous les articles
Affichage des articles dont le libellé est tcsh. Afficher tous les articles

vendredi 9 août 2013

Exit from child script to parent script with error message


In csh, use

exit [n]

Suppose you want to write a script named readable that returns a 0 if a file is readable and a 1 if the file is not readable.
     if [ -r "$1" ] then
          exit 0
     else 
          exit 1
     endif
You pass readable a single argument, which is a filename. If the file is a readable file, the return code is a 0; otherwise, a 1 is returned. Now in your parent shell you can use the $? ($status-csh) variable to test whether the file is readable are not. For example, your parent shell might look like this:
     readable somefile
     if [ $? ] then
          echo "Processing the file"
          ...
     else 
          echo "Cannot read the file"
     endif
This provides you with the capability to tell whether a child shell script completed with or without errors.


Note: this useful text is from http://alasir.com/

jeudi 1 août 2013

@: Badly formed number, octal problem in shell

In a csh script, I have :

set a = "08"
printf "%03i" $a
printf: 008: value not completely converted


set c = "08"
@ c ++
@: Badly formed number.

This happens when the variable a is equal to 018,..., 098, as well as 09, 019 .... 099.
This is because: 1) while shell treats numbers starting with 0 ("08") as octal, but not as integer number; 2) 8 and 9 do not exist in octal; 3) my tcsh version: tcsh --version is tcsh 6.17.00 (Astron) . (In fact in an older version tcsh 6.13.00 (Astron), the same error does not happen !)

The solution is to remove the leading 0s from the variable before we apply the commands such as printf or @. for example:

set a = "008"
set a  =  `echo $a   |   sed   's/^0*//g`
@ a ++







mardi 22 mai 2012

deal with array in csh/tcsh

In the shell enviroment of tcsh, I have a variable $zz which is an array:

---echo $zz
la1_22066 la1_22073 la1_22080 la1_22087 la1_22094 la1_22101 la1_22108 la1_22115 la1_22122 la1_22129 la1_22136 la1_22143 la1_22150 la1_22157

--- to get the first element in the array: echo $zz[1]
--- to get the last element in the array:  echo $zz[$#zz]  (in csh perhaps it works also with echo $zz[$])
--- to get all the elements in the array:  echo $zz[*]

vendredi 25 novembre 2011

insert some space in a specific position of a line of text

For given a line from the results of grep, we use awk to insert some space between the position of 130 and 131:

grep SX Sx_xxxxxxxxx92201M017xxx.lageos_coresta | awk '{printf("%s
%s\n",substr($0,0,130),substr($0,131))}'


---substr($0,0,130): $0 is the input string/text, 0 is the starting position, 130 is the ending position. This is to get the first 130 characters in the input string

---substr($0,131): 131 is the starting position. This is to get the substring starting from 131th column in the input string.

mardi 22 novembre 2011

replace some words in a text file with tcsh

#!/bin/tcsh

set mot_tochange = "0 lageos1_"
set new_mot = "-1 lageos1_"

set commonpath = "/home/xwang/gin/directeur/"
set testpath = "22066_22430_test/la1"

foreach f (`ls $commonpath$testpath*`)
echo "f= $f"
set filename = `basename $f`
set new_f = "/home/xwang/gin/directeur/22066_22430_eq_v9.2/$filename"


cat $f | head -n 28 > $new_f
grep "$mot_tochange" $f | sed "s;$mot_tochange;$new_mot;g" >> $new_f
cat $f | tail -n 36 >> $new_f
end