Ill post the assignment and my script as well as the error I am
getting and hopefully you can see where I went wrong cuz i sure can :
)
My focus is:
a) getting this script done because I am behind;
b) any help please let me know what i am lacking and an explaination.
Write a shell script to locate executable files. This script takes a
list of file names from the command line and determines which would be
executed had these names been given as commands.
* The search path should be based only on the user's PATH
environment variable. You shall not use the Unix which command, the
ksh whence (type) command, or the bash type command.
* The script should find only the first occurrence of the "file".
If the file is not found, the script should print an error message
that the file was not found in the user's path. (Both the filename and
the users path should be printed.)
* If the first parameter is '-a', then the script should print all
occurrences of the executable file in the user's path. Again if the
file was not on the path, an error message should be displayed.
* Project usings temporary files will not be graded.
Note:
* The shell variable PATH defines the search path for the
directory containing the command. Alternative directory names are
separated by a colon (:). The current directory can be specified by
two or more adjacent colons, or by a colon at the beginning or end of
the path list.
* If the command name contains a / then the search path is not used.
Otherwise, each directory in the path is searched for an executable
file.
usage: mywhich [-a] command ....
Examples: The locations of these programs may vary on different
systems and the users PATH environment variable.
prompt> mywhich ls
/bin/ls
prompt> mywhich -a cc
/bin/cc
/usr/ucb/cc
prompt> mywhich ./mywhich
./mywhich
prompt> mywhich fooblar
fooblar not found
prompt> mywhich ksh sh csh bash
/usr/bin/ksh
/bin/sh
/bin/csh
/usr/local/bin/bash
check to see if $1 is a -a, if it is set a varible, FINDALL=true, get
rid
of the $1; otherwise, set FINDALL=false
for all of the positional paramters (ie: for FILE )
do
check to see if $FILE is already a PATHNAME. (ie: it has a / in it);
if
it does, then check to see if $FILE is executable and a regular file ;
if it is, then echo it; otherwise, echo $FILE not found.
if $FILE is not a PATHNAME, then look for $FILE on each element of
the PATH, don't forget to fix the path for any special
set FOUND=FALSE
for P in (expand the path changing :'s to spaces and takge care of
special cases)
if $P/$FILE exits and is executable and an ordinary file;
then display the $P/$FILE, set FOUND=TRUE and if FINDALL is
FALSE, then exit the loop.
done
if FOUND is false, then print $FILE not found
done
#!/bin/bash
echo "$1 $2"
FINDALL=FALSE
case $PATH in
:*)
PATH=".:$PATH"
;;
*::*)
PATH=`echo $PATH | sed -e 's/::/:.:/g'`
;;
*:)
PATH="$PATH:."
;;
esac
command="$1"
IFS=$OLDIFS
IFS=:
set -- $PATH
IFS=$OLDIFS
case $command in
*/*)
echo $command
;;
-a)
echo "-a stuff first $1 $2"
shift
command="$1"
echo "after shift $1"
echo "$command"
FOUND=false
IFS=:
for P in $PATH
do
if [ ! -d "$P/$command" -a -x "$P/$command" ]
then
FOUND=true
echo $P/$command
continue
fi done
if [ "$FOUND" = false ]
then
echo "Command $command not found in your
search path"
fi
;;
*)
FOUND=false
for P
do
if [ ! -d "$P/$command" -a -x "$P/$command" ]
then
FOUND=true
echo $P/$command
break
fi done
if [ "$FOUND" = false ]
then
echo "Command $command not found in your search
path"
fi
;;
esac
---error please not the t/sing echo statements i put in the script
[root@localhost room]# sh two -a ls
-a ls
-a stuff first /adabas/bin /adabas/pgm
after shift /adabas/pgm
/adabas/pgm
Command /adabas/pgm not found in your search path
why is the $1 changing to the path variable? |