•
•
•
•
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
![]() |
•
•
Join Date: Nov 2007
Posts: 108
Reputation:
Rep Power: 1
Solved Threads: 0
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
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•
•
Join Date: Apr 2008
Posts: 39
Reputation:
Rep Power: 1
Solved Threads: 8
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.
You need to move your echo into your else statement.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- C command-line I/O question (C++)
- Apache Alias Directive... mod_alias question (Linux Servers and Apache)
- Completely new to C++ and have question about using char (C++)
- Question (Geeks' Lounge)
- question on cooling (Cases, Fans and Power Supplies)
- Context-sensitive grammar question :( (Computer Science and Software Design)
- Welcome PC Mod Kingdom peeps! (Geeks' Lounge)
- Laptop LCD built into a car? (Monitors, Displays and Video Cards)
- Changing Network Configuration (*nix Software)
Other Threads in the Shell Scripting Forum
- Previous Thread: check for $1?
- Next Thread: Decision


Linear Mode