1

This question already has an answer here:

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

marked as duplicate by αғsнιη, Kiwy, Timothy Martin, jimmij, Romeo Ninov Mar 22 '18 at 8:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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 )
  • 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

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