Issue
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?
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
Answered By - Some programmer dude Answer Checked By - Candace Johnson (WPSolving Volunteer)