cannot 'cd' into iPod filesystem

Discussion in 'Mac Programming' started by phatqao, Jul 26, 2010.

  1. phatqao macrumors member

    phatqao

    Joined:
    Jul 20, 2010
    Location:
    PA and/or NYC
    #1
    i'm attempting to write a script that copies the contents of an iPod to a desktop folder. the problem comes when the cd command returns a "No such file or directory" error after the script attempts to change the working directory to one inside the iPod. the interesting part is that outside of a script, cd'ing into the iPod works just fine - however, when the script attempts it, the directory apparently does not exist. The iPod does appear in the /Volumes directory, and i have manually copied some files from it with no issue.
    Code:
    cd /Volumes
    VOLUMES=$(ls)
    
    for vol in $VOLUMES ; do
          if [ -O $vol ] ; then
          		echo $vol > $HOME/Desktop/pod.txt
          		ipod=$(sed -e "s/ /\\\ /g" -e "s/'/\\\'/g" $HOME/Desktop/pod.txt)
          fi
    done
    
    mkdir $HOME/Desktop/iPod_Contents
    cd /Volumes/$ipod/iPod_Control
    cp Music/ $HOME/Desktop/iPod_Contents
    
    exit 0
    can anyone shed light on why my script can't cd into my iPod?
     
  2. SDub90 macrumors 6502a

    Joined:
    Nov 9, 2009
    Location:
    Long Island
    #2
    What's the actually volume name for your ipod? Spaces or characters may be breaking the path in the script.
     
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    Yes you should always use quotes around the paths when using a variable:

    Code:
    cd "/Volumes/$ipod/iPod_Control"
    I can't tell you how many times I run into this with other people's build scripts ;)
     
  4. phatqao thread starter macrumors member

    phatqao

    Joined:
    Jul 20, 2010
    Location:
    PA and/or NYC
    #4
    that's what i was attempting to avoid with the sed command - it's meant to replace every space with a '\ ' and every apostrophe with a \', thus escaping them. i think i did that part right...?
     
  5. phatqao thread starter macrumors member

    phatqao

    Joined:
    Jul 20, 2010
    Location:
    PA and/or NYC
    #5
    tried this, and although the filepath in the error message looks exactly like it would were i to type it in myself, the error "no such file or directory" persists.
    my output:

    Code:
    Emmett-Butlers-MacBook-Pro:~ emmettbutler$ podrip.sh
    /Users/emmettbutler/Documents/Scripts/podrip.sh: line 14: cd: /Volumes/MBUTLER4\'S/iPod_Control: No such file or directory
    cp: Music/: No such file or directory
    
    Emmett-Butlers-MacBook-Pro:~ emmettbutler$ cd /Volumes/MBUTLER4\'S/iPod_Control
    
    Emmett-Butlers-MacBook-Pro:iPod_Control emmettbutler$ ls
    Music	      iTunes
    
    as you can see, running the script yields the error, but manually cd'ing into the ipod works just fine - what gives?
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Please post your complete latest code.

    After looking at your original code and reading the responses, you're going about this in an unnecessarily complex way, because you don't completely understand shell quoting. I'm pretty sure this can be greatly simplified, but I need to see your latest code.
     
  7. phatqao thread starter macrumors member

    phatqao

    Joined:
    Jul 20, 2010
    Location:
    PA and/or NYC
    #7
    my latest complete code, which yielded the above errors.

    Code:
    #!/bin/bash
    
    cd /Volumes
    VOLUMES=$(ls)
    
    #cycle through mounted volumes and find the one owned by user
    for vol in $VOLUMES ; do
          if [ -O $vol ] ; then
          		echo $vol > $HOME/Desktop/pod.txt #save ipod name to file
          		#sed adds backslashes to escape spaces and apostrophes
          		ipod=$(sed -e "s/ /\\\ /g" -e "s/'/\\\'/g" $HOME/Desktop/pod.txt) 
          fi
    done
    
    #create directory to store music
    mkdir $HOME/Desktop/iPod_Contents
    
    #navigate to the ipod music folder
    cd "/Volumes/$ipod/iPod_Control"
    
    #copy music to desktop
    cp Music/ $HOME/Desktop/iPod_Contents
    
    #exit
    exit 0
    
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    Thanks for posting the code.

    I may take me a few hours to get to this. That thing called "a real job" takes precedence. If anyone else wants to take a shot, go for it.

    I'll be testing with a USB thumb drive with the name "MBUTLER4'S" because I don't have an iPod to use. Please do me a favor by posting the output of this command, when your iPod is connected:
    Code:
    ls -l /Volumes
    
     
  9. phatqao thread starter macrumors member

    phatqao

    Joined:
    Jul 20, 2010
    Location:
    PA and/or NYC
    #9
    thanks so much for taking the time to help. listing the Volumes in greater detail shows:
    Code:
    total 24
    drwxrwxrwx@ 1 emmettbutler  staff  8192 Jul 27 12:00 MBUTLER4'S
    lrwxr-xr-x  1 root          admin     1 Jul 27 11:41 Macintosh HD -> /
    
     
  10. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    In the latest code it looks like you do your escaping AND quote the path. Try pulling out the sed escape stuff and see how it treats you.

    As an aside, if you're only looking for things that have iPod_Control subdirectories...
    ls -d /Volumes/*/iPod_Control
    might treat you right.

    -Lee
     
  11. phatqao thread starter macrumors member

    phatqao

    Joined:
    Jul 20, 2010
    Location:
    PA and/or NYC
    #11
    thanks lee

    i have a much simpler program now that does almost everything i need:
    Code:
    #!/bin/bash
    
    ipod=`ls -d /Volumes/*/iPod_Control`
    echo $ipod
    
    #create directory to store music
    if [ ! -e "$HOME/Documents/iPod_Contents" ] ; then
    	mkdir "$HOME/Documents/iPod_Contents"
    else
    	mkdir "HOME/Documents/iPod_Contents_2"
    fi
    
    #navigate to the ipod music folder
    cd "$ipod"
    
    #copy music
    cp -rv Music/ $HOME/Documents/iPod_Contents
    
    #open files
    open $HOME/Documents/iPod_Contents/
    
    #exit
    exit 0
    
    but one thing still troubling: the created directory is not visible in finder...my guess is it has something to do with the "@" sign in its permissions listing
    Code:
    drwxr-xr-x@   5 emmettbutler  staff   170 Jul 27 16:32 iPod_Contents
    
    does anyone know what the @ refers to and/or how to make the file visible in finder?

    edit: figured it out - it's a finder attribute that makes the file not hidden, but invisible. i'm now using the xattr -d option to remove the attribute
    working program:
    Code:
    #!/bin/bash
    
    ipod=`ls -d /Volumes/*/iPod_Control`
    echo $ipod
    
    #create directory to store music
    if [ ! -e "$HOME/Desktop/iPod_Contents" ] ; then
    	mkdir "$HOME/Desktop/iPod_Contents"
    else
    	echo "$HOME/Desktop/iPod_Contents already exists"
    	exit 0
    fi
    
    #navigate to the ipod music folder
    cd "$ipod"
    
    #copy music to desktop
    cp -rv Music/ $HOME/Desktop/iPod_Contents
    
    #make F## folders visible
    echo "Please enter your password"
    for fnum in `ls "$HOME/Desktop/iPod_Contents"` ; do
    	sudo xattr -d com.apple.FinderInfo "$HOME/Desktop/iPod_Contents/$fnum"
    done
    
    #make iPod_Contents visible
    sudo xattr -d com.apple.FinderInfo "$HOME/Desktop/iPod_Contents"
    
    #open folder
    open "$HOME/Desktop/iPod_Contents"
    
    #exit
    exit 0
    thanks to everyone who helped! (and any more advice is greatly appreciated)

    http://blog.nanorails.com/articles/2007/12/12/removing-the-quarantine-attribute-on-your-application
     
  12. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #12
    Maybe use the -X option to the 'cp' command:
    http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/cp.1.html


    The man page knows:
    http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/ls.1.html
    If the file or directory has extended attributes, the permissions field printed by the -l option is followed by a '@' character. Otherwise, if the file or directory has extended security information (such as an access control list), the permissions field printed by the -l option is followed by a '+' character.​
     
  13. cqexbesd macrumors regular

    Joined:
    Jun 4, 2009
    #13
    Just a general hint when debugging shell scripts...

    sh -x script

    It will print out each line before it is run but after variable expansion etc.
     

Share This Page