dropdown menu

SCRIPT:


KORN SHELL:
http://www.bolthole.com/solaris/ksh.html
http://www.well.ox.ac.uk/~johnb/comp/unix/ksh.html


SYMBOLS:
#!/usr/bin/ksh      <--this indicates the shell, which should be used to interpret the script

' '                 <--ignores all metacharacters between the quotes (echo '$HOME' --> $HOME) (with ssh variables are interpreted)
" "                 <--ignores all metacharacters except for $, ` and \ (echo "$HOME" --> /home/root) (with ssh everything is string-literal)
\                   <--ignores the special meaning of the following character (echo \$HOME --> $HOME)
                    (it is the continuation character as well, to continue a command in a new line)
${VAR}hello         <--{} distingueshes the variable from the surrounding text
~                   <-- represents the home directory of the user (~joe refer's to the home dir of joe)
------------------------------------
x=day
echo there is a ${x}long meeting      <--{} is used to eliminate the need of space after the variable
there is a daylong meeting
------------------------------------
x=`date` or x=$(date)     <-() is supported only in ksh
------------------------------------

arithmeic:
(( x += 1 ))                 <- counter adds 1 to x
(( x -= 1 ))                 <- counter minus 1 from x
x=$(($x+1))                  <- counter adds 1 to x

let command can be used for this as well (+,-,*,/):
let x=x+10
let x=x/2

read <variable>              <- reads from the input to the given variable
export variable=value        <- passes variable into a subshell

set -x                       <- shows real time info for debugging
set -u                       <- unsets variables

test command:
The test command evaluates the expression and returns a return code 0 if true.
test expression or [ expression ] or [[ expression ]] (leave a space near the squaer braces)
In newer Korn shell the modern notation [[...]] is used, which is an extension of the test command.

integer operations:
if [ $? -eq 0 ] ; then       <- equal
if [[ $? -eq 1 ]]            <- [[ is for test condition
if [ $? -ne 0 ] ; then       <- not equal
if [ $? -lt 0 ] ; then       <- less than
if [ $? -gt 0 ] ; then       <- greater than
if [ $? -le 0 ] ; then       <- less equal
if [ $? -ge 0 ] ; then       <- greater equal
$number1 -eq $number2        <- numbers are equal
$number1 -ne $number2        <- numbers are not equal

string operations:
$string1 = $string2          <- strings are identical
$string1 !=  $string2        <- strings are not identical

if [[ -e $FILE ]]            <- if file exist

read command:
It reads one line from standard input and assigns the values  of each field to a shell variable.


print command (printf):
printf '%20s\n' "-- PLAYER --"

The syntax is %[-]m[.n]s
%s is the string, [-] is left justified, m is no. of chars wide, [n] is print the first n chars of the string.
The above example shows the string “- PLAYER -” printed, right justified in string wide of 20 chars.

VAR1="Hello"
VAR2="Worldddd"

printf '%10s %-10.5s\n' $VAR1 $VAR2
Hello World

----------------------

RUNNIG A SCRIPT:

Each shell script is executed in a subshell. Variables defined in a shell script cannot be passed back to the parent shell.

.                         <---if you invoke a shell script with a . (dot) it runs in the current shell.
                          Variables defined in this script are defined also for the current shell.

./                        <- refers to files in the current working directory
~                         <- refers to the home directory

3 ways of running a script:
ksh scriptname            (read permission needed)
scriptname                (read and execute permission needed)(in the PATH variable must be defined the dir)
. scriptname              (read permission needed) (it runs in the current shell, a subshell will not be invoked)


$$                        process id
$0                        shell script name currently executing
$1, $2 ...                parameters which were invoked with the script
$*                        equal all arguments passed
$#                        number of arguments passed into shell script (if [[ $# -ne 1 ]]...)
$?                        retrurn code of last command executed
command1 && command2      if command1 successful then do command2 (ls s* && rm s*) (if [[ $ACE = "HIGH" && $banker_score -ge 11 ]])
command1 || command2      if command1 not successful then do command2 ( if [[ $ACE = "HIGH" || $banker_score -ge 11 ]])

exit n                    pass a return code to shell script
$!                        process ID of last background process

true                      this command always returns a true result
false                     this command always returns a false result

--------------------------------------------------

LOOPS:

for i in `cat file`
do
...
done


for i in /tmp/*            it will remove all files in /tmp
do
rm $i
done

while [[ $x -lt 9 ]]
do
...
done


read game?"Another Game (y|n)? "
      case $game in
            y|Y)  :;;            <- ":" means continue
            n|N)  EXT=1;;
      esac


--------------------------------------------------

CASE:
(it can happen that there is a problem with numerical variable so this is needed before the CASE condition: "x=echo $x")

case $x in
1 )
   echo
   ;;

2 )
   echo "Stopping the ovo agent..."
   ;;

* )
   echo "bye"
   ;;
esac

--------------------------------------------------

TIPS & TRICKS:

cat <file> | head -n $x | tail -1            <-displays a line from a file
awk 'NR==2' /root/settitle                   <--displays the 2nd line in the file:/root/setitle
/usr/bin/echo yes | command                  <-- input is automatically yes
echo "$FS2:\t it is mounted now"             <--\t will do a tab in the output
perl -e 'select(undef,undef,undef,.3)'       <--it will sleep for 0.3 second

---------------------

Run the script every 2nd Sunday of the month:
DAY=$(date "+%d")
[ $DAY -ge 8 -a $DAY -le 14 ] || exit        <- if $DAY greater than 8 and lesser than 14 the script will continue
...rest of the script                        if not (|| means condition is false) it will exit


it will check if a file older than a week and if yes, delete it:
35 21 * * 0 /usr/bin/find /home/traffic/ -name "trace" -mtime +7 -exec /usr/bin/rm {} \\;


leaving the cursor at the same position while counting:
x=1
while [[ $x -le 100 ]]; do
   tput cub 20
   print -n $x
   (( x += 1 )
done

6 comments:

  1. for loop script to change the MPIO settings in EMC disk
    ====================================================

    Note all the disks are in a file

    # cat disks
    hdisk90
    hdisk91
    hdisk92
    hdisk93
    hdisk94
    hdisk95
    hdisk96
    hdisk97
    hdisk89


    for i in `cat disks`
    > do
    > chdev -l $i -a algorithm=round_robin -a reserve_policy=no_reserve -a queue_depth=32 -a rw_timeout=180
    > done
    hdisk90 changed
    hdisk91 changed
    hdisk92 changed
    hdisk93 changed
    hdisk94 changed
    hdisk95 changed
    hdisk96 changed
    hdisk97 changed
    hdisk89 changed

    ReplyDelete
  2. very useful....and very nice to understand........keep writing..........

    ReplyDelete
  3. if possible post in depth bash scripting for beginners .......

    ReplyDelete
  4. Hello Everybody,
    My name is Mrs Sharon Sim. I live in Singapore and i am a happy woman today? and i told my self that any lender that rescue my family from our poor situation, i will refer any person that is looking for loan to him, he gave me happiness to me and my family, i was in need of a loan of S$250,000.00 to start my life all over as i am a single mother with 3 kids I met this honest and GOD fearing man loan lender that help me with a loan of S$250,000.00 SG. Dollar, he is a GOD fearing man, if you are in need of loan and you will pay back the loan please contact him tell him that is Mrs Sharon, that refer you to him. contact Dr Purva Pius,via email:(urgentloan22@gmail.com) Thank you.

    BORROWERS APPLICATION DETAILS


    1. Name Of Applicant in Full:……..
    2. Telephone Numbers:……….
    3. Address and Location:…….
    4. Amount in request………..
    5. Repayment Period:………..
    6. Purpose Of Loan………….
    7. country…………………
    8. phone…………………..
    9. occupation………………
    10.age/sex…………………
    11.Monthly Income…………..
    12.Email……………..

    Regards.
    Managements
    Email Kindly Contact: urgentloan22@gmail.com

    ReplyDelete