Re: [Help-bash] Help With School Assignment, Learning Bash Scripts
From:
Eduardo A . Bustamante L贸pez
Subject:
Re: [Help-bash] Help With School Assignment, Learning Bash Scripts
Date:
Thu, 6 Nov 2014 15:38:02 -0800
User-agent:
Mutt/1.5.21 (2010-09-15)
On Thu, Nov 06, 2014 at 05:18:00PM -0500, nick wrote:
> I will look into the links for learning more about shell scripting , on the
> other hand I
> unable to figure out how to do this part of the assignment too and have
> goggled various
> questions but to not valid. I will paste the parts of the question below.
> Cheers Nick
> 1.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.
>
> Here is my code so far:
> !/bin/bash
> shopt -s extglob nullglob
> dir=$1
> for file in "$dir"/!*(.old)
> do
> [[ $file == *.old ]] || mv -- "$file" "$file.old"
> done
Wow! Your code is very nice :) I'm glad you are reading the links.
The sad part is that your teacher is asking you to do what you
SHOULDN'T be doing. I mean, your code does it the right way, and the
teacher asks for the broken way.
This is the broken approach:
#!/bin/bash
# BROKEN BROKEN
for file in $(ls -1 "$1" | grep -v '\.old$'); do
mv "$file" "$file.old"
done
# BROKEN BROKEN
The reason this is broken is that you're relying on filenames not
having spaces, or new lines, which they can! :( It's so sad that this
is your teacher...