help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Help With School Assignment, Learning Bash Scripts


From: Greg Wooledge
Subject: Re: [Help-bash] Help With School Assignment, Learning Bash Scripts
Date: Tue, 11 Nov 2014 08:52:26 -0500
User-agent: Mutt/1.4.2.3i

On Mon, Nov 10, 2014 at 09:34:58PM -0500, nick wrote:
> 
> Sorry Greg,
> Been busy with other work. I will paste the actual question below.
> Cheers Nick 
> Modify the command substitution that's being used to create the loop values
> that will be placed into the "filename" variable.
> 
> Instead of just an "ls $1", pipe the output into a "grep".  The "grep"
> will search for all filenames that DO NOT end in ".old".  This can easily
> be done with the "grep -v" option.
> 
> With this approach, you can get rid of the "echo ... | grep ..."  and the
> "if" control structure inside the loop, and simply do the rename.
> 
> Again, check that your script works correctly.

So the teacher isn't simply executing the script and checking the
results?  He or she is actually looking at HOW you did it, and
expecting to see certain questionable features in the code?

> Here is my script.
> #!/bin/bash
> shopt -s extglob nullglob dotglob
> dir=$1
> for file in "$dir"/!(*.old)
> do
>     mv -- "$file" "$file.old"
> done

This looks like it solves the actual problem (rename all the files
that don't end with .old), but if the teacher wants you to "write
sloppy code" instead of "solve this problem", we're back to what
I said earlier about choosing between doing it right, or getting a
good grade.

> On 14-11-07 01:43 PM, Greg Wooledge wrote:
> > On Fri, Nov 07, 2014 at 01:33:53PM -0500, nick wrote:
> >> Files created in 'testdir':
> >> file1  file2.old  file3old  file4.old
> >>
> >> Execution of 'oldfiles2 testdir':
> >>
> >>
> >> Files in 'testdir' after 'oldfiles2' was run:
> >> file1.old  file2.old  file3old.old  file4.old
> >>
> >> Error: 'for' does not seem to loop only through required filenames
> > 
> > Well, the results match what I thought the question was.  What *IS* the
> > actual homework question?  What is the expected set of filenames at
> > the end?

Notice that it doesn't say the results are wrong.  It says some cryptic
crap about your loop.  Is this a human being looking at the code, or
is it some sort of semi-automated parser scanning the code and looking
for "ls | grep -v"?

This is why we hate homework questions.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]