[GLLUG] Need help creating a shell Script

Bruce Smith blubdog at gmail.com
Sat Apr 8 09:16:24 EDT 2006


"mv -i" only prompts to overwrite when the destination filename
exists.  Without the "-i", mv will silently nuke the existing dest.
file.

Most distros have an alias in the default login script of "mv" to "mv
-i" (and "cp" to "cp -i") which is why you may be seeing that
behaviour in the shell now.  However aliases don't carry over to shell
scripts, so you need to be careful.

 - BS


On 4/8/06, Caleb Cushing <xenoterracide at gmail.com> wrote:
> I actually do have another question about this script. why doesn't mv -i
> prompt me before changing the files when it's run like this?
>
>
> On 4/7/06, Alec Warner <warnera6 at egr.msu.edu> wrote:
> > Caleb Cushing wrote:
> > > haven't tried it yet backing up the directory into a tarball that'll
> take a
> > > while. Do you think you could comment it for me? basically programming
> of
> > > any kind is a weak point for me. I'm trying to improve. but copying and
> > > pasting someone elses code may fix my issue but it won't fix my
> > > understanding. "give a man a fish... teach a man to fish..." sort of
> thing.
> > >
> > >>
> > >>
> > >>On 4/6/06, Bruce Smith < blubdog at gmail.com> wrote:
> > >>
> > >>>Disclaimer, backup the directory first!  :-)
> > >>>And copy/paste exactly!
> > >>>
> > >>>You can test without changing anything by adding an "echo" in front of
> > >>>the "mv" and optionally commenting out the "set" statements for better
> > >>>readability.
> > >>>
> > >>>One bug (feature?) is it will replace multiple spaces with only ONE
> > >>>underscore.
> > >>>
> > >>>You need to run it with the filename(s) as arguments.  i.e.:
> > >>>  /path/to/scriptname *
> > >>>  /path/to/scriptname a*
> > >>>  /path/to/scriptname 'File to Test this  Thing on.TXT'
> > >>>
> > >>>- BS
> > >>>
> > >>>
> > >>>#!/bin/sh            # Run using /bin/sh, the Bourne Shell
> > >>>infile=upper
> > >>>outfile=lower
> > >>>for f                # Not sure where 'f' comes from, but it's looping
> throught the standard input, IIRC
> > >>>do
> > >>>    if [ -f "$f" ]; then # man test will tell you what this does ;)
> > >>>        l="$(echo $f|sed 's/ /_/g'|tr "[:${infile}:]"
> "[:${outfile}:]")" # echo file, change ' ' to '_', globally, change upper to
> lower
> > >>>
> > >>>        if [ "$f" != "$l" ]; then # If the new filename doesn't equal
> the old filename..
> > >>>            set -x                # turn off echo
> > >>>            mv -i "$f" "$l"       # rename the file
> > >>>            set +x                # turn echo back on
> > >>>        fi
> > >>>     fi
> > >>>done
> > >>># end of script
> > >>>
> > >>>
> > >>>On 4/6/06, Caleb Cushing < xenoterracide at gmail.com> wrote:
> > >>>
> > >>>>I need/want to change the names of all the files and folders in my
> > >>>
> > >>>/music
> > >>>
> > >>>>directory to where they don't have spaces in them and all the names
> > >>>
> > >>>are in
> > >>>
> > >>>>lowercase. This is what I have so far:
> > >>>>
> > >>>>#!/bin/bash
> > >>>>
> > >>>>ls -R |                          # List directory recursively
> > >>>>sed -e 's/ /_/g' |             # Remove spaces change them to
> > >>>
> > >>>underscore
> > >>>
> > >>>>tr [:upper:] [:lower:] |     #change letters from upper to lower
> > >>>>
> > >>>>that gives me the output of what I would want the directory and file
> > >>>>structure to look like when I'm done. I am unsure however how to take
> > >>>
> > >>>that
> > >>>
> > >>>>output and move the files and directories to acually look like that
> > >>>
> > >>>when I
> > >>>
> > >>>>am finished. It's also the tricky part because if I don't do it
> > >>>
> > >>>right...
> > >>>
> > >>>>well it might be an even bigger pain to fix. I pretty sure I have a
> > >>>
> > >>>lot more
> > >>>
> > >>>>to do to get this workable... and I'm just having problems wrapping my
> > >>>
> > >>>brain
> > >>>
> > >>>>around it.
> > >>>>
> > >>>>snippet:before
> > >>>>#ls -R
> > >>>>
> > >>>>./3 Doors Down:
> > >>>>Away from the Sun  Seventeen Days  The Better Life   Thumbs.db
> > >>>
> > >>>desktop.ini
> > >>>
> > >>>>./3 Doors Down/Away from the Sun:
> > >>>>01-When I'm Gone-3 Doors Down-Away from the Sun.mp3
> > >>>>02-Away from the Sun-3 Doors Down-Away from the Sun.mp3
> > >>>>03-The Road I'm On-3 Doors Down-Away from the Sun.mp3
> > >>>>04-Ticket to Heaven-3 Doors Down-Away from the Sun.mp3
> > >>>>05-Running Out of Days-3 Doors Down-Away from the Sun.mp3
> > >>>>06-Here Without You-3 Doors Down-Away from the Sun.mp3
> > >>>>07-I Feel You-3 Doors Down-Away from the Sun.mp3
> > >>>>08-Dangerous Game-3 Doors Down-Away from the Sun.mp3
> > >>>>09-Changes-3 Doors Down-Away from the Sun.mp3
> > >>>>10-Going Down in Flames-3 Doors Down-Away from the Sun.mp3
> > >>>>11-Sarah Yellin'-3 Doors Down-Away from the Sun.mp3
> > >>>> AlbumArtSmall.jpg
> >
> >>>>AlbumArt_{C212FF41-147D-402C-BAC5-1284F8B4C802}_Large.jpg
> >
> >>>>AlbumArt_{C212FF41-147D-402C-BAC5-1284F8B4C802}_Small.jpg
> > >>>>Folder.jpg
> > >>>> Thumbs.db
> > >>>>desktop.ini
> > >>>>
> > >>>>snippet:after
> > >>>>
> > >>>>ls -R
> > >>>>
> > >>>>./3_doors_down:
> > >>>>away_from_the_sun
> > >>>>seventeen_days
> > >>>>the_better_life
> > >>>>thumbs.db
> > >>>>desktop.ini
> > >>>>
> > >>>>./3_doors_down/away_from_the_sun:
> > >>>>01-when_i'm_gone-3_doors_down-away_from_the_sun.mp3
> >
> >>>>02-away_from_the_sun-3_doors_down-away_from_the_sun.mp3
> > >>>>03-the_road_i'm_on-3_doors_down-away_from_the_sun.mp3
> >
> >>>>04-ticket_to_heaven-3_doors_down-away_from_the_sun.mp3
> >
> >>>>05-running_out_of_days-3_doors_down-away_from_the_sun.mp3
> >
> >>>>06-here_without_you-3_doors_down-away_from_the_sun.mp3
> > >>>>07-i_feel_you-3_doors_down-away_from_the_sun.mp3
> > >>>>08-dangerous_game-3_doors_down-away_from_the_sun.mp3
> > >>>>09-changes-3_doors_down-away_from_the_sun.mp3
> >
> >>>>10-going_down_in_flames-3_doors_down-away_from_the_sun.mp3
> > >>>>11-sarah_yellin'-3_doors_down-away_from_the_sun.mp3
> > >>>>albumartsmall.jpg
> >
> >>>>albumart_{c212ff41-147d-402c-bac5-1284f8b4c802}_large.jpg
> >
> >>>>albumart_{c212ff41-147d-402c-bac5-1284f8b4c802}_small.jpg
> > >>>>folder.jpg
> > >>>>thumbs.db
> > >>>>desktop.ini
> > >>>>
> > >>>>_______________________________________________
> > >>>>linux-user mailing list
> > >>>> linux-user at egr.msu.edu
> >
> >>>>http://mailman.egr.msu.edu/mailman/listinfo/linux-user
> > >>>>
> > >>>>
> > >>>>
> > >>>
> > >>
> > >
> > >
> > >
> ------------------------------------------------------------------------
> > >
> > > _______________________________________________
> > > linux-user mailing list
> > > linux-user at egr.msu.edu
> > > http://mailman.egr.msu.edu/mailman/listinfo/linux-user
> >
>
>
> _______________________________________________
> linux-user mailing list
> linux-user at egr.msu.edu
> http://mailman.egr.msu.edu/mailman/listinfo/linux-user
>
>
>



More information about the linux-user mailing list