1

I'm relatively new to Bash scripting but I'm very familiar with Python and many Bash commands. I was wondering if it's possible to use a command such as ifconfig eno1 and use the output as a condition to be used in an if statement. Basically, if you issue:

 ifconfig eno1 

and you get an active IP, is there any way to save the output of that to a variable as a 1/0 or true/false, to be used in the following if statement example (assuming ifoutput is the variable name):

 if [ "$ifoutput" == "$True"] then echo connection is active! fi 

I thought of possibly calling a Python script that does essentially the same thing, however I was thinking I would run into the same issue; being no way to get an individual return value from the Python script to be used in the if statement. Please advise and please go easy on me -- as I said I am relatively new to Bash scripting! I already have methods of doing this strictly using Python, however I am trying to learn more about Bash scripting. Thanks in advance!

1
  • you could write it to file using > filename.txt or pipe it to a different command, | nextCommand. Commented Aug 6, 2018 at 15:22

1 Answer 1

4

ifconfig will return a numeric 8-bit value when it completes (like any *nix process that exits cleanly).

Typically zero (0) is used to indicate success, and another value is used to indicate failure. This return code is available as $?.

ifconfig will also output to stdout, for example:

eth0 Link encap:Ethernet HWaddr 02:42:ac:14:00:02 inet addr:172.20.0.2 Bcast:172.20.255.255 Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:20430143 errors:0 dropped:0 overruns:0 frame:0 TX packets:20966475 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:46532669457 (46.5 GB) TX bytes:44114090048 (44.1 GB) 

If you are trying to determine if ifconfig eth0 was successful (i.e: permissions depending, the interface exists), then you could do one of the following:

ifconfig eth0 if [ $? -eq 0 ]; then echo yay fi 
if ifconfig eth0; then echo yay fi 
ifconfig eth0 && echo yay 

Hiding ifconfig's output can be achieved by appending a > /dev/null:

if ifconfig eth0 > /dev/null; then echo yay fi 

You asked about printing the return code, and this is a gotcha for beginners...

You can simply do this:

ifconfig eth0 echo $? 

But after calling on echo, the return code ($?) will be echo's return code, not ifconfig's... To deal with that, just assign it to a temporary variable:

ifconfig eth0 RET=$? echo ${RET} if [ ${RET} -eq 0 ]; then echo yay fi 

Note: [ is a built-in command that can evaluate expressions, the square braces ([ / ]) are not actually part of the if syntax:

$ help [ [: [ arg... ] Evaluate conditional expression. This is a synonym for the "test" builtin, but the last argument must be a literal `]', to match the opening `['. 

If you are actually trying to get the IP address of an interface, then you might want to use a pipeline, like one of the following (there are many ways to achieve the same result):

ifconfig eth0 | grep -Eom1 'inet addr:([0-9]{1,3}\.){3}[0-9]{1,3}' | cut -d: -f2 ifconfig eth0 | sed -re '/inet addr:/!d;s/^[^:]+://;s/ .*$//' 
3
  • Thank you! Initially I had thought of using the exit code, but since I am still fairly new , I didn't realize the exit codes would be similar to what i'm used to working with in Python! Thanks for the additional information as well! I feel confident I can carry out my project now! is there any way to show the exit code upon execution? Commented Aug 6, 2018 at 15:38
  • 2
    No problem - and of course! echo $?... see my edit for explanation of a gotcha though. Commented Aug 6, 2018 at 15:40
  • tldp.org/guides.html -> Advanced Bash-Scripting Guide - has a lot of information regarding Bash and scripting. Commented Aug 6, 2018 at 18:49

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.