shellScript.ref

mail.
use /usr/ucb/Mail, exist in linux and solaris

cat FILE | Mail -s "subject line" tho01@cs

(linux mail does not support -s subject option, while solaris will not use the
first line of the message that has 'Subject: bla' in it as the subject.)


wisdom from Mohit:
set absolute path at the beginning of the script.  
this way, no need to define commands with full path.
The only other issue is, same command, such as awk, maybe in multiple places,
which is not really same (linux awk is solaris nawk).  Path sequence need to
be assigned carefully for such commands.  In a very heterogeneous platform,
maybe hard and hard path prefered...


---

echo $PATH | tr ':' '\012'
\012 is the octal value for linefeed, note that \015, CR, will not work in unix.

---

$NUM = `expr 10 + 10`
$NUM = `expr 10 \* 10`
$NUM = `expr 10 \/ 10`


--------

LD_PRELOAD_OPTS=...
This environemnt will allow specific library to be "preloaded", before LD_LIBRARY_PATH modules get loaded.  This way, program that has specific requirements for libs but may otherwise conflict with studd in LD_LIBRARY_PATH can be preferentially loaded first, thus used first and resolve incomptabilities issues.



-------

emulating main:

have fn called
main() 
{ ... }

then at end of file, call it as 
main $*

$* = all arguments

The actual calling of the fn does not need () before args.
fn really become mini shell scripts w/in the script.

----


for programs that insist of having a terminal attached to it, 
use this in the shell script, and the whole script will seems to have a terminal attached to it (don't know what is implication of stdin).

exec 0 /dev/null
done

Also, do not put quotes around $List in the line of the for loop, or the whole thing will be considered a
single entity, instead of being space delimited.




----

#.profile from db2profile for setting up path
# eg use after declaration:
# AddtoString PATH /usr/local/bin
#
# Function to avoid repetitive environment variable entries
#
# addition by Tin, add only if dir actually exist
#
AddtoString()
{
  var=$1
  addme=$2
  if [ -d $2 ]; then
    awkval='$1 != "'${addme?}'"{print $0}'
    newval=`eval /usr/bin/echo \\${$var} | /usr/bin/awk "${awkval?}" RS=:`
    eval ${var?}=`/usr/bin/echo $newval | /usr/bin/sed 's/ /:/g'`:${addme?}
    unset var addme awkval newval
  fi
}




---


Not really shell script, but commands




================================================================================

csh craps:

set path = ( $path /bin /usr/local/bin /usr/bin /usr/bin/X11 ~/bin /sbin /usr/sbin . )
##---->>>  don't use $PATH above, it must be lower case $path in the parenthesis.  
##---->>>  or else it will consider the colon delimited list as a unit and path won't work,
##---->>>  even when displayed correctly!!  another reason why csh is bad!! =)

setenv PATH            "${PATH}:${AMGEN_HOME}/bin"
###--->>> above works, ${PATH} is the right thing to use.  (it seems braces {} are required around var name.

##-->> $path is a space delimited list
##-->> $PATH is a colon delimited list
##-->> setenv expects a colon delimited list
##-->> set    expects a space delimited list


setenv  DISPLAY hostname:1
unsetenv DISPLAY		# undo setenv
set ...
unset ...			# undo set


CSH variable evaluation.
If a variable is not defined, trying to access it will give an error.
I guess it is like C programming language after all.
sh will just print blank, which I think is easier to deal with in shell script.

But anyway, in CSH, to guard against such error, need to test the variable 
being defined before using it.  eg

if ($?TRACE_LOG) then
        setenv TRACE_LOG "$TRACE_LOG openeye_processed"
else
        setenv TRACE_LOG "openeye_processed"
endif


if ($?MANPATH) then
        setenv MANPATH "${MANPATH}:/usr/share/man:/usr/man:/usr/local/man"
else
        setenv MANPATH "/usr/share/man:/usr/man:/usr/local/man"
endif

These kind of check should be done for things like LD_LIBRARY_PATH, etc.
There is probably an easier way, using :q or something



csh if statement, string comparison

set machine = `hostname`
if !( ( ${machine} == "firth" ) || ( ${machine} == "firth.amgen.com" ) ) then
        echo "no Firth here... do work"
else
        echo "it is Firth here"
endif

http://www.unet.univie.ac.at/aix/aixuser/usrosdev/c_shell_cmds.htm#A279911bb ::
C Shell Expressions and Operators
Operator 	What it Means
() 	change precedence
~ 	complement
! 	negation
*/ % 	multiply, divide, modulo
+ - 	add, subtract
<< > > 	left shift, right shift
<= >= < > 	relational operators
== != =~ !~ 	string comparison/pattern matching
& 	bitwise "and"
^ 	bitwise "exclusive or"
| 	bitwise "inclusive or"
&& 	logical "and"
|| 	logical "or"



Things to have in .cshrc ::

 http://mail.hudat.com/~ken/help/unix/.cshrc
#alias ins2path  'if ("$path:q" !~ *"\!$"* ) set path=( \!$ $path )'
#alias add2path  'if ("$path:q" !~ *"\!$"* ) set path=( $path \!$ )'
add2path ${JAVA_HOME}/bin


I/O redirection

cat foo >& bar		# >& = stdout + stderr  (sh use  2>&1 at the end)
cat foo |& tee bar	# | instead of >, otherwise same as above.