i need help with a script!

Dear readers

I usually don’t ask things like this but I’m having problems with a script that I have to make due to tomorrow.
It’s for school, so yes, basically it’s homework, but I don’t understand any of this since we’ve just seen the basics before we got a substitute teacher. (To be specific, the only “hard” part we’ve seen during class before this guy showed up were pipe-commands)

So I really really need your help because I have no idea what I’m doing atm in my script and it only returns syntax errors! Here’s our assignment:

Create a script that does the following:
Simulate the “which”-command.
Ask the user the name of a file and permit him to add a parameter. The output of the script has to be the path to that file or an error message. Search through the file systems for that file. If you can’t locate the file, then search for files with a name that contains the name of the searchfile.
If you still can’t locate the file, return an error message. Also give a summary report:
- how many file systems you’ve looked through
- how many directories you’ve scanned

That’s our assignment, after 2 weeks of opensuse we can hardly delete directories. So here’s what I have so far, the syntax error code;

http://i14.photobucket.com/albums/a347/Rude616/sofar.jpg

I would REALLY appreciate it if you’d help me!

Just for starters, cut the space when you are assigning values. e.g. “PARMIS = $2” should be “PARMIS=$2”. It is a good practice to put everything in quotes when yo are comparing things in “if” condition. e.g. “if “$#” -eq “1” ]” is always a better thing to do.

Are you passing some variables at the command line, because, $# gives you the number of command line parameters, i.e. if your script name is “test” and you are using “test a b c” then a,b and c are three command line parameters passed to the script and $# will be 3. For using read you may need to use some varibale. e.g “read value” instead of just read, then the value which you eneter when console asks will be stored in “value”.

Hope I have helped you. Please correct me if I am not right… :slight_smile:

ok thanks, i’m still having problems solving it, but you’ve been a great help (especially for the read command)
cheers!

I know evil…

#!/bin/bash

type -P “$1”

hey… it doesn’t use which.
:slight_smile:

GuaranaForce wrote:
> Dear readers
>
> I usually don’t ask things like this but I’m having problems with a
> script that I have to make due to tomorrow.
> It’s for school, so yes, basically it’s homework, but I don’t
> understand any of this since we’ve just seen the basics before we got a
> substitute teacher. (To be specific, the only “hard” part we’ve seen
> during class before this guy showed up were pipe-commands)
>
> So I really really need your help because I have no idea what I’m doing
> atm in my script and it only returns syntax errors! Here’s our
> assignment:
>
> Create a script that does the following:
> Simulate the “which”-command.

That at least limits the number of directories where the file can hide, as
‘which’ only searches the directories in $PATH.
So use the $PATH environmental to begin with, instead of the find command.

> Ask the user the name of a file and permit him to add a parameter. The
> output of the script has to be the path to that file or an error
> message. Search through the file systems for that file. If you can’t
> locate the file, then search for files with a name that contains the
> name of the searchfile.

Bash 3.0 knows about regexp. So e.g.you can use


SEARCHEXP='^.*'$SEARCHTERM
for dir in $PATH;do
FileInPath=$dir/$SEARCHTERM
if  $FileInPath ~= $SEARCHEXP ]]; then
echo $File$SEARCH
fi
done

Set nocasematch to make the regexp case insensitive.

> If you still can’t locate the file, return an error message. Also give
> a summary report:
> - how many file systems you’ve looked through
> - how many directories you’ve scanned
>
>
> That’s our assignment, after 2 weeks of opensuse we can hardly delete
> directories. So here’s what I have so far, the syntax error code;
>
> [image: http://i14.photobucket.com/albums/a347/Rude616/sofar.jpg]

That’s not a whole lot for two weeks work…
Anyway, good luck (you could use it).

On Wed, 2009-06-10 at 19:41 +0000, cjcox wrote:
> I know evil…
>
> #!/bin/bash
>
> type -P “$1”
>
>
> hey… it doesn’t use which.
> :slight_smile:
>
>

I reread the “which” problem… of course it’s NOT which at all, not
even close. NOT (no). Tell the instructor they need a clue.

What is being asked for is a simple file finder… where the
implementaton needs to get it done the long and hard way.

On Wed, 2009-06-10 at 19:49 +0000, LittleRedRooster wrote:
> GuaranaForce wrote:
> > Dear readers
> >
> > I usually don’t ask things like this but I’m having problems with a
> > script that I have to make due to tomorrow.
> > It’s for school, so yes, basically it’s homework, but I don’t
> > understand any of this since we’ve just seen the basics before we got a
> > substitute teacher. (To be specific, the only “hard” part we’ve seen
> > during class before this guy showed up were pipe-commands)
> >
> > So I really really need your help because I have no idea what I’m doing
> > atm in my script and it only returns syntax errors! Here’s our
> > assignment:
> >
> > Create a script that does the following:
> > Simulate the “which”-command.
>
> That at least limits the number of directories where the file can hide, as
> ‘which’ only searches the directories in $PATH.
> So use the $PATH environmental to begin with, instead of the find command.

yeah… I got tricked by this as well… it’s NOT which. From the rest of
the description your supposed to search a while hierarchy, even /.

It’s really a poorly written assignment.

Identification of filesystem requires a query for EVERY file/dir (again,
the assignment is ambiguous).

I wouldn’t do a thing without clarification… but that’s just me… or,
worst case, I’d do the WHOLE assignmet, which is a real mess (lots of
portability issues will come up). Unless we can assume Linux. My guess
is that the students really can’t do the assignment as written.

It’s possible that assignment becomes less ambiguous depending on what
the students have actually learned in those two weeks.

Yuk. What a mess.