Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash script conditional, else not working

Here is my script

#!/bin/bash

sudo pkexec ip link set can0 up type can bitrate 1000000
echo $?
result=$?

if [[ result -eq 0 ]]
then
  echo "Node will initialize"
else
  echo "Node will not initialize"
fi

It will just read the exit status of the above terminal command and will print out messages according to the condition. As I run the script, even the result is equal to 0 or 1 or 2, it will print "Node will initialize". what could be wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Order matters!

With result=$? you get the result of the echo $? command.

Do the assignment first, and print the value of $result instead:

sudo pkexec ip link set can0 up type can bitrate 1000000
result=$?
echo $result
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading