Google Answers Logo
View Question
 
Q: Bash shell script assignment ( No Answer,   0 Comments )
Question  
Subject: Bash shell script assignment
Category: Computers > Programming
Asked by: derekwtp-ga
List Price: $10.00
Posted: 12 Apr 2003 15:29 PDT
Expires: 28 Apr 2003 08:06 PDT
Question ID: 189748
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?

Request for Question Clarification by studboy-ga on 13 Apr 2003 11:07 PDT
Hi

You did a

set -- $PATH 

This changes everythign in your argument list.

Let me know if this suffice as an answer.  Thanks!

Request for Question Clarification by studboy-ga on 13 Apr 2003 11:14 PDT
So move 

IFS=$OLDIFS 
IFS=: 
set -- $PATH 
IFS=$OLDIFS

inside your case statement after you parse your command line

Request for Question Clarification by studboy-ga on 14 Apr 2003 11:31 PDT
Hi Derek,

Did that work for you?

Thanks!

Clarification of Question by derekwtp-ga on 19 Apr 2003 14:02 PDT
yeah it worked thanks sorry for the delay
Answer  
There is no answer at this time.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy