Showing posts with label examples. Show all posts
Showing posts with label examples. Show all posts

Tuesday, November 22, 2011

shell script Part4. file handling


Utility small scripts.

Reading directories using wildcards

  for file in /home/user/*
  do
    if [ -d "$file" ]
    then
      echo "$file is a directory"
    elif [ -f "$file" ]
    then
      echo "$file is a file"
    else
      echo "$file is neither a file nor a directory"
    fi
  done

  Observe that in the checks $file is enclosed in double quotes since the names can also contain spaces and in that case we would want the whole name to be treated as one element for the FOR loop.

Reading and displaying the contents of the passwd file in some meaningful format.

Now the passwd file contains lines of records in the following format, denoting the information about each of the users(both visible and system)

login_name:password:user_id:group_id:description_of_account:HOME_directory_location:default_shell_for_user

We would like to present each user's information in a tree-like structure, that makes things more readable. Yes, you can write them to a file for future readability.

  IFS=$'\n'
  for line in `cat /etc/passwd`
  do
    echo "Values: $line"
    IFS=:
    for field in $line
    do
      echo "  Field: $field"
    done
  done

Shell Script examples for user Input. Part6


Handling User Inputs from the shell - Code snippets


1.  Grab all parameters
  
    count=1
    echo "No of parameters = ${!#}"
    while [ $count -le $#]
    do
      echo "param[$count] = ${$count}"
      count=$[$count+1]
    done
   

    But this does not work well when no of command line parameters exceeds 9.

2.  Grab all parameters
   
    count=1
    for var in $*
    do
      echo "Param[$count] = $var"
      count=$[$count+1]
    done
 
    count=1
    for var in $@
    do
      echo "param[$count] = $var"
      count=$[$count+1]
    done
 
    Using this with
    file a b c d1
 
    will produce
    Param[1] = a
    param[1] = a
    param[2] = b
    param[3] = c
    param[4] = d1
   

    For explanation look HERE.


3.  Use of shift to process parameters

    count=1
    while [ -n "$1" ]
    do
      echo "param[$count]: $1"
      count=$[$count + 1]
      shift
    done
   

    For explanation look HERE.

4.  Using shift to process options and parameters

    Suppose your code expects options a,b(with 2 parameters), c, d and a set of parameters.
   
    count=1
    while [ -n "$1" ]
    do
      case "$1" in
      -a) echo "Found -a option";;
      -b) echo "Found -b option with value $2 and $3"
          shift 2;;       # since we know we have consumed 3 values from the list of parameters.We shift one at the end of each iteration
      -c) echo "Found -c option";;
      -d) echo "Found -d option";;
      --) shift           # We use the -- separator to delineate options from parameter set
          break;;
      *)  echo "$1 not an option";;
      esac
      shift     # this is for whatever option we might have found, we need to shift them out
    done
 
    count=1
    for param in "$@"     # now only params remain
    do
      echo "Param[$count] = $param"
      count=$[$count + 1]
    done
   

    The dependency on the "--" is the only drawback of this method.
    For explanation look HERE.

5.  using getopt to process parameters

    set -- `getopt ab:c:d "$@"`
    while [ -n "$1" ]
    do
      case "$1" in
        -a) echo "Found -a option" ;;
        -b) echo "Found -b option with parameter $2"
            shift ;;
        -c) echo "Found -c option with parameter $2"
            shift;;
        -d) echo "Found -d option";;
        --) shift
        break;      #this character means options have ended
        *)  echo "Not supported option $1";;
      esac
      shift
    done
 
    count=1
    for param in "$@"     # now only params remain
    do
      echo "Param[$count] = $param"
      count=$[$count + 1]
    done
   

    For explanation look HERE.
    But this fails when the parameters have spaces in them.

6.  using getopts to process parameters.

    count=1
    while getopts ab:c:d opt
    do
      case "$opt" in
        -a) echo "Found -a option" ;;
        -b) echo "Found -b option with parameter $OPTARG";;
        -c) echo "Found -c option with parameter $OPTARG";;
        -d) echo "Found -d option";;
        *)  echo "Found unknown option $opt";;
      esac
    done
 
    shift $[ $OPTIND - 1 ]
 
    count=1
    for param in "$@"     # now only params remain
    do
      echo "Param[$count] = $param"
      count=$[$count + 1]
    done
   

    For explanation look HERE.