Simple, but its killing me! (Variable Substition in BASH)


Results 1 to 6 of 6

Thread: Simple, but its killing me! (Variable Substition in BASH)

  1. #1
    Join Date
    Dec 2002
    Posts
    346

    Simple, but its killing me! (Variable Substition in BASH)

    Hey all, long time without a post!

    I think what I'm doing is simple, but I'm apparently looking at it the wrong way and am now here asking for help!

    Just a simple BASH for loop to read the file path from a text file (clean.txt) echo the variable for debug purposes, and scp it to a server I have using port 50 for SSH.

    I've already formatted the entries in clean.txt to handle spaces correctly, using sed replacement.

    Example from the clean.txt file:
    Code:
    /MP3/NAS000000001/Barenaked\ Ladies/Barenaked\ Ladies\ -\ Barenaked\ For\ The\ Holidays/20\ Auld\ Lang\ Syne.mp3
    /MP3/NAS000000001/Barenaked\ Ladies/Barenaked\ Ladies\ -\ Barenaked\ For\ The\ Holidays/14\ Deck\ the\ Stills.mp3
    /MP3/NAS000000001/Bob\ Dylan/Single\ songs/House\ Of\ The\ Rising\ Sun.mp3
    For loop thats failing:
    Code:
    IFS=$'\n'; for f in `cat clean.txt`; do echo -e "\n$f"; sleep 1; scp -P 50 "$f" pete@demo.home.com:/MP3/backup/f; done
    STDOutput:
    Code:
    /MP3/NAS000000001/Barenaked\ Ladies/Barenaked\ Ladies\ -\ Barenaked\ For\ The\ Holidays/20\ Auld\ Lang\ Syne.mp3
    Warning: Permanently added 'demo.home.com,xx.xxx.59.143' (RSA) to the list of known hosts.
    /MP3/NAS000000001/Barenaked\ Ladies/Barenaked\ Ladies\ -\ Barenaked\ For\ The\ Holidays/20\ Auld\ Lang\ Syne.mp3: Invalid argument
    
    /MP3/NAS000000001/Barenaked\ Ladies/Barenaked\ Ladies\ -\ Barenaked\ For\ The\ Holidays/14\ Deck\ the\ Stills.mp3
    Warning: Permanently added 'demo.home.com,xx.xxx.59.143' (RSA) to the list of known hosts.
    /MP3/NAS000000001/Barenaked\ Ladies/Barenaked\ Ladies\ -\ Barenaked\ For\ The\ Holidays/14\ Deck\ the\ Stills.mp3: Invalid argument
    Using a properly placed echo statement, I know what is being passed:
    Code:
    /MP3/NAS000000001/Barenaked\ Ladies/Barenaked\ Ladies\ -\ Barenaked\ For\ The\ Holidays/20\ Auld\ Lang\ Syne.mp3
    scp -P 50 /MP3/NAS000000001/Barenaked\ Ladies/Barenaked\ Ladies\ -\ Barenaked\ For\ The\ Holidays/20\ Auld\ Lang\ Syne.mp3 pete@demo.home.com:/MP3/backup/f
    But if I copy that echo statement and run it from the terminal, IT JUST WORKS!
    Code:
    scp -P 50 /MP3/NAS000000001/Barenaked\ Ladies/Barenaked\ Ladies\ -\ Barenaked\ For\ The\ Holidays/20\ Auld\ Lang\ Syne.mp3 pete@demo.home.com:/MP3/backup/f
    Warning: Permanently added 'demo.home.com,xx.xxx.59.143' (RSA) to the list of known hosts.
    20 Auld Lang Syne.mp3                                                                                              2%  128KB 128.0KB/s   00:33 ETA

    I don't get it! Anyone have any insight?

    Thanks,
    Pete

  2. #2
    Join Date
    Dec 2002
    Posts
    346
    Uh-oh, 30 lurkers and no replies! That doesn't bode well for my problem! :-)

  3. #3
    Join Date
    Dec 2002
    Posts
    346
    Solved, or rather band-aided.

    I'm wondering if I've discovered a bug in BASH; I can get around it for my current task by setting a shell alias equal to the echo statement: please note, this is an awful awful looking hack.

    This loop WILL do what I expected it to do though:

    Code:
    IFS=$'\n'; for f in `cat clean.txt`; do echo -e "\n$f"; sleep 1; alias currentfile=`echo "rsync -e \"ssh -p 50\" -av --stats --progress "$g" pete@demo.home.com:/MP3/backup/"`; currentfile; done

  4. #4
    Join Date
    May 2004
    Location
    Arizona
    Posts
    76
    2 thoughts:
    Is '/MP3/backup/f' a directory on the receiving end? Or should that be '/MP3/backup/$f'

    This seemed to work in my testing:
    Code:
    while read line; do scp "$line" myserver:; done<testfile1.txt
    Last edited by Bryon Speede; 02-18-2011 at 10:49 AM.
    n00b

  5. #5
    Join Date
    Dec 2002
    Posts
    346
    The command should have ended at /MP3/backup. Copy-paste error (Or an error from editing the post).

    I'll have to try the while loop: Thanks for the interest!

  6. #6
    Join Date
    Sep 1999
    Location
    Cambridge, UK
    Posts
    509
    You're over-complicating things. By setting IFS you are telling bash to treat spaces in input as part of the input and not a delimiter. But then you embed backslashes in the filename to escape the spaces. Your original script would work with the backslashes removed from the input file. It would also work with the original input if you did
    Code:
    eval scp "$f" ...
    As an alternative you could do something like
    Code:
    rsync --files-from=clean.txt -a / $DEST/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •