Translate to your Language

Disclaimer Statement

Total Pageviews

Track

Follow us on FaceBook

About

Powered by Blogger.

Blog Archive

Popular Posts

Wednesday, December 26, 2012

Basic korn shell notes

by Unknown  |  in Other at  8:12 PM


korn shell notes
----------------------------------------------------------------------
Commands:
----------------------------------------------------------------------
   # - comment line
   >  - output to a file
   >>  - append to a file
   <  - input from a file
   <<!  - redirect from inside a shell script until next !
   VAR=value - assigns value to shell variable
   echo "$VAR did something" - $ in front of variable name prints value
----------------------------------------------------------------------
Special Variables:
----------------------------------------------------------------------
   $$ - current process id PID
   $* - all command line arguments
   $# - number of command line arguments
   $0 - shell program name
   $3 - third argument on command line
   $? - return code of last command 0 is usually successful
----------------------------------------------------------------------
Control Structures:
----------------------------------------------------------------------
end of if is if spelled backwards
elif is else if
----------------------------------------------------------------------
   if [ $VAR = XXX ]
   then
      echo "do something if true"
   elif [ -f $FILE -a -r $FILE ]
      echo "$FILE is a file and is readable"
   else
      echo "do something else if not true
   fi
----------------------------------------------------------------------
   if VAR is null then the above if gives syntax error
   if [ "$VAR"x = "XXX"x ] will fix problem
----------------------------------------------------------------------
   read FILE_NAME - does input in variable
   TODAY=`date +"%y/%m/%d %A"` - output date command into variable TODAY
----------------------------------------------------------------------
end of case is case spelled backwards
options on case statements do pattern matching
----------------------------------------------------------------------
   case $FILE in
      /DW/*)
          echo "is a program"
          ;;
      /xfer*)
          echo "is data"
          ;;
      *)
          echo "* is always true"
          ;;
   esac
----------------------------------------------------------------------
   while [ -f $FILE ]
   do
      echo "something if FILE is a UNIX file"
      break - will break out of loop
   done
----------------------------------------------------------------------
pipe from command
----------------------------------------------------------------------
   ls dw* |
   while read FILE_NAME
   do
      grep mailx $FILE_NAME
      if [ $? -eq 0 ]
      then
         echo "$FILE_NAME has mailx in it"
         rm $FILE_NAME
      fi
   done
----------------------------------------------------------------------
redirect input from file
----------------------------------------------------------------------
   while read FILE
   do
      echo $FILE
   done <inputfile
----------------------------------------------------------------------
   for FILE in $*
   do
      # FILE values come from command line arguments
      rm $FILE
   done
----------------------------------------------------------------------
true false statements
----------------------------------------------------------------------
spaces around [ and ] are absolutely neccessary
also spaces around <>=!= are absolutely neccessary
----------------------------------------------------------------------
test -d /dirname   is same as   [ -d /dirname ]
-f checks for file
-d checks for dir
-r is file readable
-x is file executable
-eq numeric test
-gt numeric test
-ge numeric test
-lt numeric test
-le numeric test
-a and
-o or
!= not equal
<> also not equal
----------------------------------------------------------------------
Example:
----------------------------------------------------------------------
if [ $# -lt 3 ]
then
   echo "Usage: $0 sys dir file"
   exit 3
fi
----------------------------------------------------------------------
UNIX commands:
----------------------------------------------------------------------
   lp -d cc099_hp4si28 filename - prints file
   cat file1 file2 file3 >newfile - combines file1,2 and 3
   tail -3 file1 - displays last 3 lines of file1
   head -4 file1 - displays first 4 lines of file1
   cp file1 file2 - copies file1 to file2
   cp file1 dir - copies file1 into dir
   mv file1 file2 - renames file1 to file2
   mv file1 file2 file3 dir - moves files 1 thru 3 into dir
   wc -c file1 - counts chars in file1
   wc -l file1 - counts lines in file1
   ls -l & - runs ls -l in background
   ls -l | mailx -s"subject" userid - mails results of ls command
   mailx -s"testing" kyennara - mails message to kyennara (^d stops)
   df -k - disk free space in 1K blocks
   bdf - nicer version of abpve
   du -s -k /DW - disk usage summary of files in /DW in 1K blocks
   grep regexpression file1 file2 - will print lines that match exp
   grep -v regexpression file1 - will print lines that do not match exp
   grep -i regexpression file1 - will ignore case
   fgrep - fast grep
   egrep - extended grep
   egrep -f regexpfile file1 - will do multiple regexp from regexpfile
      the expressions are line1 or line2 or line3
   date | cut -c4-7 - cut columns 5 thru 8 month from date command
   date | cut -d" " -f4 - cut field 4 time from date command
   TABLE=`echo "$TABLE          " | cut -c1-10`
      - will force TABLE to be 10 chars long
   grep $LOGNAME /etc/passwd | cut -d":" -f3 - cuts UNIX group id
   pr -t -s -m file1 file2 - will print file1 and file2 side by side
   basename $FILE_NAME - will print just the file name not the path
   dirname $FILE_NAME - will print the dir the file is in
   hostname - prints the UNIX machine name
   set - $LINE - will parse LINE into $1 $2 $3 etc
   ROWS_NOT_LOADED=`expr $ROWS_NOT_LOADED + $ROWS` - will do math
   exit 3 - will exit with return code of 3
   who - list users
   who -a - lists all fields
   ps -ef - lists process table
   ps -ef | grep $LOGNAME - lists my process table
   ps -f - lists my process table (better method)
   ps -ef | grep bawillia - lists Bruce's process table
   ps -fu bawillia - lists Bruce's process table (better method)
----------------------------------------------------------------------
find all the files with yy and not yyyy
if grep.list has yy[^y]
----------------------------------------------------------------------
   find . -type f -exec egrep -n -f grep.list {} /dev/null \;
   USER=kduke
   ps -fu $USER | grep -v PID |
   while read LINE
   do
      set - $LINE
      PID=$2
      # will kill all $USER processes
      kill $PID
   done
----------------------------------------------------------------------
Notes:
----------------------------------------------------------------------
   Normally ksh is setup to do vi commands on history.
   ESC puts you in vi mode.
   Arrow keys will not work.
   Use h,j,k,l for left,down,up and right.
   /^vi will find your last vi command.
   dd will create a blank line then hit return to get out of vi mode.
----------------------------------------------------------------------

0 comments:

© Copyright © 2014Bigdata-dwbi. by