need help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help
# 1  
Old 04-15-2008
need help

Code:
if [ $# = 0 ]
then
     echo "$USAGE"
     exit 1
fi

while getopts lfso: c
do
   case $c in
   l)      tail -$1 /etc/passwd
           exit 2;;
   f)      head -$1 /etc/passwd
           exit 3;;
   s)      grep $1 /etc/passwd | cut -d: -f7
           exit 4;;
   o)     OARG=$OPTARG;;
      \?) echo "$USAGE"
          exit 5;;
   esac
   done
shift `expr $OPTIND - 1

Why it is not taking the $1 input in each option..can anyone suggest me the solution please...SmilieSmilieSmilieSmilieSmilieSmilie

Last edited by Yogesh Sawant; 04-15-2008 at 02:32 AM.. Reason: added code tags
# 2  
Old 04-15-2008
Replace the comparison operator "=" with "-eq". We use -eq for integer comparison and = for strings.
# 3  
Old 04-15-2008
i m refering to options in the case
tail -$1 /etc/passwd

thanks
# 4  
Old 04-15-2008
Use $OPTARG instead of $1 to catch the arguments
# 5  
Old 04-15-2008
you mean to replace $1 with $OPTARG ?
# 6  
Old 04-15-2008
: s missing

Sorry for overlooking things. Try this code.

Code:
if [ $# = 0 ]
then
     echo "$USAGE"
     exit 1
fi

while getopts l:f:s:o: c
do
   case $c in
   l)      tail -${OPTARG} /etc/passwd
           exit 2;;
   f)      head -${OPTARG} /etc/passwd
           exit 3;;
   s)      grep ${OPTARG} /etc/passwd | cut -d: -f7
           exit 4;;
   o)     OARG=$OPTARG;;
      \?) echo "$USAGE"
          exit 5;;
   esac
   done
shift `expr $OPTIND - 1

# 7  
Old 04-15-2008
it does work thanks alot buddy
cheers
problem solvedSmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question