1
#!/bin/bash
loc=`echo ~/.gvfs/*/DCIM/100_FUJI`
rm -f /mnt/fujifilmA100
ln -s "$loc" /mnt/fujifilmA100

For some reason the variable * doesn't get substituted with the only possible value and gets given the value /home/chris/.gvfs/*/DCIM/100_FUJI. Does anyone have an idea of why?

Please note:

  • If global expansion fails, the pattern is not substituted. I ran the commands:

    chris@comp2008:~$ loc=``echo ~/.gvfs/*/DCIM/100_FUJI

    chris@comp2008:~$ echo $loc

    /home/chris/.gvfs/gphoto2 mount on usb%3A001,008/DCIM/100_FUJI

    So we can see the expansion should work

  • I have now switched to using:

    loc = ``find ~/.gvfs -name 100_FUJI

    I am just curious why it doesn't work as is.

  • Debugging output using sh -x

echo /home/chris/.gvfs/*/DCIM/100_FUJI

loc=/home/chris/.gvfs/*/DCIM/100_FUJI

rm -f /mnt/fujifilmA100

ln -s /home/chris/.gvfs/*/DCIM/100_FUJI/mnt/fujifilmA100

Casebash
  • 133

4 Answers4

3

You mentioned in your response to Tactical Vim that you're running this under sudo -- '~' is going to be expanded differently under sudo vs. under your normal user. Change it to '~chris' or hard code your home directory.

Joe H.
  • 1,917
  • Using ~ instead of ~chris did that directory to not be matched properly, but the * still manages to not be replaced – Casebash Apr 02 '10 at 10:03
2

Are you sure you entered the .sh file exactly as shown here? I just made a test script doing exactly the same thing as you and it works for me.

Maybe something to do with the strange directory name with spaces, %-signs, and commas?

davr
  • 1,729
2

Try running your script under 'sh -x' to see some debug output, perhaps it'll provide a clue. I've never seen echo used in this sort of way, it could be something related to that; does it work if you replace 'echo' with 'ls -d' (or similar, not sure your output) instead?

  • Tried: ls -d ~/.gvfs/*/DCIM/100_FUJI /home/chris/.gvfs/gphoto2 mount on usb%3A001,003/DCIM/100_FUJI

    But when I put ls -d in the script it threw Permission Denied even though I sudoed both the script and the ls

    – Casebash Sep 30 '09 at 12:12
  • As yesterday mentioned, ~ expands the the home folder of the user running the command (sic, it's more complicated). Try being explicit and naming the direct folder as he mentions. –  Oct 01 '09 at 19:36
0

Okay, I modified my script as follows:

loc=`echo ~chris/.gvfs/*/DCIM/`
sudo rm -f /mnt/camera
sudo ln -s "$loc" /mnt/camera

Notice that the sudo's are inside the script

Casebash
  • 133