1

I am not good in bash, i can do basic but when it comes into multiple if statements, then i am a no brainer for them.

I currently have the following statement made.

var=$(cat /sys/block/vda/queue/rotational)
dtype='nil'
if [ $var = 0 ]; then
        dtype=' SSD '
elif [ $var = 1 ]; then
        dtype=' HDD '
fi

Since many machines use sda instead of vda, im looking for a way to make it that there would be multiple? if statements (atleast thats how i understand it)

Basically. If the first var=$(cat /sys/block/vda/queue/rotational) command gives me and error, it would not print that one out. and it would choose this command instead. (Only if the first one is not working)

var1=$(cat /sys/block/sda/queue/rotational)
dtype='nil'
if [ $var = 0 ]; then
        dtype=' SSD '
elif [ $var = 1 ]; then
        dtype=' HDD '
fi
ilkkachu
  • 138,973
TheSebM8
  • 459

1 Answers1

2

In your specific case, you could simply use ||:

var1=$(cat /sys/block/sda/queue/rotational || cat /sys/block/vda/queue/rotational)

This would execute your first command and if the first one return an error execute the second one.

As mention in your comment if you want to avoid error output on the first command just use redirection:

var1=$(cat /sys/block/sda/queue/rotational 2>/dev/null || cat /sys/block/vda/queue/rotational )
Kiwy
  • 9,534
  • Is there a way to surpress the error in this case also? NVM. i totally forgot the 2>/dev/null part by myself. Thanks for the double || :P didnt know of this. – TheSebM8 Mar 21 '18 at 13:29
  • redirecting the error to /dev/null is the way to go here. But in fact I think this question has been answer a lot of time on the site. Sometime trying to rephrase your question in a more general way helps to find the correct question ;-) I'm happy it helped – Kiwy Mar 21 '18 at 13:36