1

I have this code:

if  [ -f "/mnt/usb/test/linuxConfig.json" ]
   then
   echo "usb på plats"

What I want is when the file is found ( aka the usb is mounted) to set a variable as true.

I want to write a script that checks if the the usb is mounted and in case is not to try to mount it and if it fails to reboot the pi.

I need the variable to be true or false since I want to use a sleep command as well.

2

I don't really see where you want to use that variable as you could easily do everything you might want to do in the correct if-then-else branch:

if [ -f "my/file" ]; then
    echo 'Filen finns tillgänglig / the file is available'
else
    echo 'Filen är inte där / the file is not there'
    mount /mnt/something || { sleep 120; reboot; }
    # or  ... || shutdown -r +2 'Rebooting due to failed mount'
fi

To use a "boolean" variable:

found=0
[ -f "my/file" ] || found=1

if (( !found )); then
    # file was not found
else
    # file was found
fi
1

You can use any nonempty value as "true":

if [ -f /mnt/ust/test/linuxConfig.json ] ; then
    var=1
fi

if [ "$var" ] ; then
    echo Var is true
else
    echo Var is false
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.