Search code examples
shellmount

Running individual commands works but not when combines in a shell script.. Why?


I need to run the following set of commands in a shell script

modprobe nbd
sudo qemu-nbd -c /dev/nbd0 path/to/image/file
sudo mount /dev/nbd0p1 /mnt/temp
python copyFiles.py
sudo umount /mnt/temp
sudo qemu-nbd -d /dev/nbd0
sudo rmmod nbd

When I individually run these commands it works fine, but when I put them in a shell script and executed that shell script, I always end up with an error in the mount command.

So I threw in a sleep 1 before mount and it works as expected.

What could be the reason behind this? (Some sort of asynchronous call registration delay/ race condition?)


Solution

  • mount error: mount point /mnt/temp does not exist

    So it seems the directory /mnt/temp doesn't exist when you are running it as a shell script. Just create it or add this in your script somewhere before the mount command:

    mkdir /mnt/temp 2>&1 /dev/null