User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 403,512 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,113 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 377 | Replies: 1
Reply
Join Date: Nov 2007
Posts: 108
Reputation: k2k is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
k2k k2k is offline Offline
Junior Poster

please take a look at my question

  #1  
Jun 28th, 2008
The code "named mywhich" below does basically the same thing as the "which" command in sh does. however, if i do >mywhich wrongcmd wrongcmd2 wrongcmd3

it will only returns wrongcmd3 not found ...... how do i make it like:
wrongcmd not found
wrongcmd2 not found
wrongcmd3 not found ???

I really wanna know this... any one can point out how will be highly appreciated. thank you

#!/bin/sh

all=FALSE


while getopts a option
do
  case "$option" in
       a) all=TRUE;;
      \?) echo "Usage: mywhich [-a] command"
          exit 1;;
  esac
done

if [ "$OPTIND" -gt "$#" ]
then
    echo "error: missing command name!"
    found=0
fi

if [ "$all" = TRUE ]
then
    shift                             #trash the -a and make command to $1
#!/bin/sh
#90.312 Programming Assignment1
#Henry Li

all=FALSE


while getopts a option
do
  case "$option" in
       a) all=TRUE;;
      \?) echo "Usage: mywhich [-a] command"
          exit 1;;
  esac
done

if [ "$OPTIND" -gt "$#" ]
then
    echo "error: missing command name!"
    found=0
fi

if [ "$all" = TRUE ]
then
    shift                             #trash the -a and make command to $1
    while [ "$#" -ne "0" ]
    do

      command=$1                       #first command being at $1
      for dir in `echo $PATH | sed 's/:/ /g'`
      do
        if [ -x "$dir/$command" ]
        then
          found=0
          echo "$dir/$command"         #if command is -x, then echo it with path
          else
          badcommand=$command

        fi
      done
    shift                              #shift second command to $1
    done


else                          #if all=FALSE
    while [ "$#" -ne "0" ]
    do
        command=$1
        for dir in `echo $PATH | sed 's/:/ /g'`
        do
           if [ -x "$dir/$command" ]
           then
               found=0
               echo "$dir/$command"
               break
           else

               badcommand=$command
           fi
        done
    shift                     #shift the 2nd command to $1
    done
fi

if [ "$found" != 0 ]
then
   echo "$badcommand is not found"
fi
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2008
Posts: 39
Reputation: omrsafetyo is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 8
omrsafetyo omrsafetyo is offline Offline
Light Poster

Re: please take a look at my question

  #2  
Jul 1st, 2008
You are only returning (echoing) the last iteration of badcommand. Your while/for/if loop above assigns the current argument that it is processing to the badcommand variable. You let that loop run through all arguments and THEN exit out of the loop and echo. It's only echoing the last command it processed as "not found".

You need to move your echo into your else statement.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 12:49 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC