1

Working on a bash script to connect a iscsi client to a server.

How do I put this command:

 iscsiadm -m discovery -t st -p 10.0.0.30

in a if to check if the connection was established, if not it will return a error.

Also if connection found the target will connect:

 iscsiadm -m node -l -T iqn.2014-02.com.example -l -p 10.0.0.30:3260

1 Answer 1

2

You can make use of the exit status which will only be zero if the command executed correct.
So in you script you just execute the command and if exit status $? is not zero you know the connection was not properly made.

example:

iscsiadm -m discovery -t st -p 10.0.0.30
if test $? -ne '0'
then
        echo "Error"
else
        iscsiadm -m node -l -T iqn.2014-02.com.example -l -p 10.0.0.30:3260
fi
3
  • Where do test come from ? Like this: test=iscsiadm -m discovery -t st -p 10.0.0.30 Feb 27, 2014 at 14:57
  • test is an intern bash command, it reads in this case the exitcode from the previous command and compares this to zero.
    – Requist
    Feb 27, 2014 at 15:19
  • Thanks! I tried it on my different commands to check if them was excecuted, and it worked. So this is like universel way to check if a command was succeed.. Mar 1, 2014 at 11:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .