Sys Admin Pocket Survival Guide - Rosetta Stone of Unix Scripting

Home

awk

shellScript

Perl

Python

PHP


 

sh/bash, csh/tcsh, awk, perl

Summary of differences of various shell and scripting language.

 

Function 

sh/ksh/bash

csh/tcsh

perl

awk

notes

Login shell

/etc/profile

.profile

.bash_profile

/etc/csh.cshrc

.cshrc / .tcshrc

 

 

 

Interactive shell

(not always sourced)

.bashrc

/etc/csh.login

.login

 

 

 

Source command

.

source (bash only)

source

 

 

 

 

 

 

 

 

 

Set variable 

export LD_LIBRARY_PATH=/lib:/usr/lib

export PATH=/bin:/usr/local/bin

LD_LIBRARY_PATH /lib:/usr/lib

set path (/bin /usr/local/bin)

$num=1

$var=SomeText

 

 

Eval variable

echo $PATH

echo $PATH

 

 

 

Special variable

 

path converts to PATH

 

 

 

Current script/shell command name

$0

$0

 

 

 

All param (whole line)

$*   (exclude $0)

 

$_

$0

 

First param

$1

 

$_[0]

$1

 

Last param of previous command

$!

 

 

$NF (?)

 

I/O redirect

dmesg > stdout 2> stderr

dmesg > my.log 2>&1

dmesg | tee my.log 2>&1

dmesg >& my.log

dmesg |& tee my.log

 

 

dmesg may never spill anything to stderr, test with a command that might.

 

 

 

 

 

 

 If statement

if [“myText” = $STR ]; then

    Cmd;

    Cmd;

else

    if [ ... ]; then

        Cmd;

    fi

fi

 

 

 

 

 

Bash has elif:

if [ cond ]; then

       cmd

elif [ cond ]; then

       cmd

else

       cmd

fi

 CASE

case “$STRING” in

  str1)

     cmd

     ;;

  Str2)

     Cmd

     ;;

  *)

     Default-cmd

     ;;

esac

 

switch ( “$STRING” )

case str1:

  cmd

  breaksw

default:

  cmd

  breaksw

endsw

 

 

sh NOTE: ;; is the termination of a case, it will not "fall" thru to the commands

in the next case (as in c?).  if needing to mach various items, can use with

FOR.  Note that a case will break away from the for loop!!  So, use break only

when really want to stop analyzing more items in the case/for loop.

 

 

For

$LIST=”a b c”

for ITEM in $LIST; do

    echo $ITEM

done

foreach ITEM ($LIST)

    Cmd

end 

 

 

Be careful with quoting $LIST.  Don’t put quotes around $LIST in the for command itself, or it may end up treating it as single item with embedded space in it.

while

 

 

 

 

 

goto

cat >/dev/null << LABEL

# sh code that 

# will be skipped

LABEL

# code that will be exeucted

goto LABEL

...

LABEL:

...

 

 

GOTO is considered taboo, but used judiciously, it is the best solution in some circumstance.  Bourne shell don’t have it, use a hack of heredocs.

 

 

 

 

 

 

Numeric comparison

-eq  -lt  -gt    

5 –gt $X

 

 

 

If $X evaluates to NULL, error is easier to understand if variable is placed in right hand side of =.  Thus, 5 –gt $X is easier better than $X –lt 5. 

String comparison

=    

(double check, not == ??)

==  !=

 

 

 

Non-zero length string

-n STRING

 

 

 

Part of the TEST command

Zero lenth string

-z STRING

 

 

 

 

File Evaluation

 

 

 

 

 

file exist

-e

 

 

 

Part of the TEST command 

File is regular file

-f

 

 

 

 

file is executable

-x

 

 

 

 

file is a directory

-d

 

 

 

 

Logical operator

-a -o  !

[[ –f soft.sh ]] && . soft.sh

&&  ||  !

test –f soft.csh && source soft.csh

 && || !

&&  || !

ksh:

if [[ `echo $OUTPUT1 | wc -l` != 0 || `echo $OUTPUT2 | wc -l` != 0 ]]; then

 

sh:

if [ `echo $OUTPUT1 | wc -l` != 0 -o `echo $OUTPUT2 | wc -l` != 0 ]; then

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 Long line here will wrap automatically

NEWline ok



Commnad/Options Differences between diff hw platforms


Stuff that would be useful for shell scripting across different platforms  (text in italic are sample output):

Command

Flag

Meaning

Solaris (64-bit)

RHEL

SuSE

Ubuntu (32-bit)

OS/X Darwin

AIX

Irix

OSF1

HP-UX

whatami

 

Script from ANL

solaris-9

linux-rhel4-ia32

linux-sles10-x86_64

linux-sles9-x86_64

linux-debian_5.0-ia32

darwin-macosx_10.5-x86_64

 

irix-6

osf

 

 

 

 

 

 

 

 

 

 

 

 

 

uname

-s (default)

Kernel name

SunOS

Linux

Linux

Linux

Darwin

AIX ?

IRIX64

OSF1

 

 

-m

Machine hw name

sun4u

i686

x86_64

x86_64

i686

i386

machine code that embed serial number.

IP35

alpha

 

 

-p

Processor type

sparc

i686

x86_64

x86_64

unknown

i386

PowerPC

mips

alpha

 

 

-i

HW platform

SUNW,Sun-Fire-880

i386

x86_64

x86_64

unknown

n/a

 

n/a

n/a

 

 

-r

Release number, kernel release in Linux

5.10

2.6.18-92.el5

2.6.16.60-0.30-smp

2.6.28-17-generic

9.4.0

(OS X 10.5)

?

4.3.3

5.3

6.5

-R: 6.5 6.5.29m

V5.1

 

 

-u

 

 

 

 

 

 

serial number (of management frame)

 

 

 

 

 -L

 

 

 

 

 

 

first number = the LPAR partition number OS is running inside of

 

 

 

 

 

-a

All info

SunOS tinsun 5.9 Generic_122300-29 sun4u sparc SUNW,Sun-Fire-480R

Linux linux-tin2 2.6.18-164.9.1.el5 #1 SMP Tue Dec 15 20:57:57 EST 2009 x86_64 x86_64 x86_64 GNU/Linux

Linux vixen 2.6.5-7.308-smp #1 SMP Mon Dec 10 11:36:40 UTC 2007 x86_64 x86_64 x86_64 GNU/Linux

Linux buntu 2.6.28-17-generic #58-Ubuntu SMP Tue Dec 1 18:57:07 UTC 2009 i686 GNU/Linux

Darwin tinmac 9.4.0 Darwin Kernel Version 9.4.0: Mon Jun  9 19:30:53 PDT 2008; root:xnu-1228.5.20~1/RELEASE_I386 i386

 

IRIX64 leda 6.5 01090133 IP35

OSF1 pandora V5.1 1885 alpha

 

 

 

 

 

 

 

 

 

 

 

 

 

arch

 

Almost same as uname –m except for Solaris

sun4

i686

x68_64

x68_64

n/a

i386

 

n/a

n/a

 

 

 

 

 

 

 

 

 

 

 

 

 

release file

 

Full OS/distro descriptive name/info

 /etc/release

 /etc/redhat-release

/etc/lsb-release

/etc/SuSE-release

/etc/lsb-release

n/a

 

n/a

n/a

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The end (= for now =).

 

Toolkits

 

·  whatami.  Much better than arch, uname, etc.  http://www.mcs.anl.gov/hs/software/systems/msys/

·  softenv.  Multi-shell environment.  http://www.mcs.anl.gov/hs/software/systems/msys/ and http://www.teragrid.org/userinfo/softenv/ .   I am still on the fence whether it is better than doing the same thing in csh v sh script, especially when aliases still need to be coded manually.  One thing seems to be missing is to test whether the path is accessible before adding it.  There isn’t a check whether an path may already be included already. 

· 

· 

 

 

Links

 

·  A Rosetta Stone of Unix OS Languages (as per the author). It is a pretty good translation between the many different Unix OS.

·  A Rosetta Stone of Unix Shell Scripts  (sh vs ksh vs csh vs tcl)

·  Static table map of Unix OS commands from unixguide.net.

·  AIX vs Solaris commands mapping

·  Syntax across languages page (shell vs perl vs javascript vs java vs c/c++ etc)

·  Rosetta Stone for Code

· 

· 

 

 


[Doc URL: http://www.grumpyxmas.com/scriptRosetta.word.html ] Last Updated: 2009-09-25
(cc) Tin Ho. See main page for copyright info.



"LYS on the outside, LKS in the inside"
"ting"