How do I run this Unix Shell Script? Not sure what to input in the command line
ID: 3678457 • Letter: H
Question
How do I run this Unix Shell Script? Not sure what to input in the command line to run this script. When I do run this, I keep getting the following message:
"Command not found in your search path"
I should be getting an ou put for example:
Here is the script:
#!/bin/bash
echo "$1 $2"
FINDALL=FALSE
case $PATH in
:*)
PATH=".:$PATH"
;;
*::*)
PATH=`echo $PATH | sed -e 's/::/:.:/g'`
;;
*:)
PATH="$PATH:."
;;
esac
command="$1"
case $command in
*/*)
echo $command
IFS=$OLDIFS
IFS=:
set -- $PATH
IFS=$OLDIFS
;;
-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
Assignment 1
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, thelocate command, or the bash type command.
· The code for the script shall not use the UNIX ls command to determine if the file is executable or if it exists.
· The script should find only the first occurrence of the "file". If the file is not found, the script the following
<command> NOT FOUND
<command> would be replaced by the name of the "file" you didn't find.
· 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.
· The find command shall not be used.
· A project using 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, you just check if the command/file specified is executable and not a directory. 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 /bin/ls
/bin/ls
prompt> mywhich fooblar
fooblar not found
prompt> mywhich ksh sh csh bash
/usr/bin/ksh
/bin/sh
/bin/csh
/usr/local/bin/bash
Explanation / Answer
Follow the steps to make this script work.
1.First thing is that we need to set a path
set -- $PATH
This changes everythign in your argument list.
2.
If So move This states inside your case statemnt after you parse your command line
IFS=$OLDIFS
IFS=:
set -- $PATH
IFS=$OLDIFS
For this i have script alredy. hope that scrip works for you.
#!/bin/sh
case $PATH in
PATH=".:$PATH"
;;
PATH=`echo $PATH | sed -e 's/::/:.:/g'`
;;
PATH="$PATH:."
;;
esac
command="$1"
IFS=$OLDIFS
IFS=:
set -- $PATH
IFS=$OLDIFS
echo $command
;;
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.