#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    58
    Rep Power
    9

    Simple bash script not working


    In the following script I'm trying to have an automated task to move all the screen shots I have taken with the "Prt Scr" button from ~/Pictures to ~/Pictures/ScreenShots
    Any ideas why it's not working. I did it at first with the mv command but was told by my instructor do it with a for loop because of globbing issues.


    Code:
    #!/bin/bash
    
    # This is a script to home keep my $HOME/Pictures directory from keeting clutered up
    # with screenshots by moving them into their own directory.
    
    ls ~/Pictures/Screenshots\ from*.png > /dev/null 2> /dev/null
    EXITSTATUS=${?}
    
    if [ ${EXITSTATUS} == 0 ]
    then
        for ${i} in $( ls ~/Pictures/Screenshot\ from*.png )
            do
                mv ${i} /home/garrett/Pictures/ScreenShots/
            done
    fi
    
    #mv /home/garrett/Pictures/Screenshot\ from*.png /home/garrett/Pictures/ScreenShots/
    #END#
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,441
    Rep Power
    1688
    Ahh, the delights of spaces in file names; they always add to the fun!
    Wht do the ls twice, once to see if there's any files to process, then once to move them? If there's none there the ls to process them will not actually do anything.
    You mix the use of ~ and the actual hard-coded directory name /home/garrett which means that if someone else should run the script it will move their files into your sub-directory.

    Do you get any error messages that suggest why it's not working?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo