Essential Unix

What is a Shell?
A Shell is a Program that reads commands and execute them. Another name for shell is a command interpreter. MS-DOS is shell for the Windows Operating System.

Unix Shell such as bash, ksh, csh, & tcsh, provide numerous convenience and feature for both interactive and programmatic purposes.

There are many (25+) Unix shells, but the most popular are sh, ksh, bash, csh, and tcsh.

Why Program in shell instead of (Perl, Python etc)?
(i) You may not have the Perl or Python available. E.g. for system Administrator, when the system is first coming up, the shell may be the only option.

(ii) For Simple File based manipulation shell is more easier and efficient then perl.

Interactive Versus Scripts?
When you manually type commands to the shell, you are running an interactive shell. The shell feature beneficial for interactive user are different from those needed when running a script, in which shell reads commands from a file.


In interactive use, the shell features that minimize typing and tedium are important. For scripting or programmatic use, flexibility, power and expresiveness are more important. 
 

Relational and Loop statements

Scripting


(i) IF statement

The if construct looks as follows with an optional else and multiple option elif (else if)

if [ -d $ff ]; then

   echo "Dir:  $ff"

fi  


if [ -d $ff ]; then 

    echo "Dirctory:  $ff, here is the total size:"
    du -s $ff

elif [ -f $ff ]; then 

  echo "File: $ff"

else 

  echo "what the heck is $ff?"
  ls -l $ff

fi 




(ii) While statment
The while command is the only way to loop in the shell. The looping continues so long as the EXPR returns true.
while EXPR; do 
 body 
done  



For example to store all the command line arguments but the last one in cmdarg in a script

#! /bin/bash 

cmdarg=""
while [ $1 -gt 1 ] do 
   cmdarg="$cmdarg $1"
   shift
done
last=$1
shift 



(iii) For statment
The for construct is "foreach" in that a variable is assigned each value in a list one by one. For example, to find out which file in the subdirctory oratips are
text files, we run the file command and grep for the word text.

for VAR in LIST; do 
  body
done

for ff in oratips/* ; do 
 if file $ff | grep text > /dev/null ; then 
  echo "File $ff is text"
fi 
done 



(iv) Case Statement
The Case statement lets you determine if a starting SSS, which is almost always contained by a variable VVV, matches any of several "cases". For example we test which state a traffic signal is in via:

case $TrafficSignal in 
    red )  echo  "stop" ;;

    yello|ornge ) echo "decesion time..." ;;

    green ) echo "Go...";;

    default ) echo "Unknown color ($TrafficSignal)" ;;

esac  
---------------------------------

Syntax of Control Structures 

It is possible to write any shell script in a single line. In practice, it is sometimes convenient to do so. For example, in a makefile, shell commands spanning more than one line are ugly and error prone.


When processing control construction, the ba/k/sh shells need a delimiter, either a newline or a ; (semi-colon), to terminate arbitrary commands. Thus, after the keyworkds if, then, do, while we do not need a delimeter, but before the fi or done, we need a delimeter indicating the end of the previous command. As an example remove the delimiters from the following command to see the susuing confusion.


% if echo then fi if then ; then ls fi fi; fi


Thus in the following, $x$ means wither a newline or a ; delimeter.
Thus the following for if statements are all equivalent


if EXPR $x$ then STMT(S) $x$ fi #general Systax
if [ -f /bin/mv ]$x$ then echo "looks like unix" $x$ fi
if [-f /bin/mv ]; then echo "looks like unix" ; fi
if [ -f /bin/mv ]; then
echo "looks like unix"
fi

if [-f /bin/mv ]
then
echo "looks like unix" ; fi


The Syntax for Control constructs are


if : if EXPR then STMS(S) fi
if else : if EXPR then STMT(S) elif EXPR; then STMT(S) fi
for: for VAR in LIST do STMT(S) done
while: while EXPR do STMT(S) done
case : case VALUE in [[PATTERN [ - PATTERNS]) STMTS;;] esac
 
 

No comments:

Post a Comment