1

I have the following code in which I try to check if the machine obtains it's IP adress from dhcp or if it's static. The code has to work for both ubuntu and sles 12. Now if I'm using the code on sles I get an error that the file is not found which is normal. But I don't want the user to see this message. Is there a way to redirect the output of an if statement? Normal redirection doesn't seem to work.

I already tried putting if [[ ${statement} ]] &> /dev/null after the if statement but that doesn't change a thing.

if [[ ! "$(grep "dhcp4: true" /etc/netplan/50-cloud-init.yaml | awk '{print $2}')" == "true" ]] || [[ ! "$(grep 'BOOTPROTO' /etc/sysconfig/network/ifcfg-eth0 | cut -d '=' -f2 | sed "s/'//g" | xargs)" == "dhcp" ]]
then
    # more code...
fi  

If anyone has a better solution to check if the client obtains the IP via dhcp I would appreciate if you let me know.

So I tried a new approach but I'm not really happy with it. The approach looks like following:

if [[ ${osname1} == "Ubuntu" ]]
then
    statement="$(grep 'dhcp4: true' /etc/netplan/50-cloud-init.yaml | awk '{print $2}')"
    matchword="true"
else
    statement="$(grep 'BOOTPROTO' /etc/sysconfig/network/ifcfg-eth0 | cut -d '=' -f2 | sed "s/'//g" | xargs)"
    matchword="dhcp"
fi

if [[ ! ${statement} == ${matchword} ]]
then
  # more statements
fi
5
  • 1
    Possible duplicate of How can I have grep not print out 'No such file or directory' errors? Commented Jul 25, 2019 at 12:18
  • 1
    excellent improvement to your Q! We can remove what are now exteraneous comments. Good luck. Commented Jul 25, 2019 at 13:28
  • 1
    For starters, I don't think if [[ .... ]] &> /dev/null can capture any output (although I can see it might be a good idea). You need to redirect output of individual programs , or if when you're getting fancy, of a grouping like { echo stdout ; echo stderr >&2 ; } &> /dev/ null. I prefer using "old school" redirections, so { echo stdout; echo stderr >&2 ; } > /dev/null 2>&1 is the equivalent. So you want something like grep 'srchStr' /path/to/file 2> /dev/null | more stuff. Note that &> would also send stdout to /dev/null Commented Jul 25, 2019 at 14:32
  • 1
    I think your second idea/block of code is much easier to grasp what your goal is and will be easier for others to maintain, after you get promoted ;-). But that else condition is rather tortured. a trailing xargs without any program to invoke may be usable, but it's not traditional usage. You'd need to explain that in a comment in your code. But ... Hm.. see my answer below. Commented Jul 25, 2019 at 14:37
  • @shellter thanks for the feedback. I removed the xargs since it's not really necesarry. I don't remember why I had it in the command it's been a while since I touched this script but I have to maintain it now, in order for it to work on other distributions as well. I really appreciate your help! Commented Jul 25, 2019 at 14:45

1 Answer 1

1

Working with your 2nd code idea,

if [[ ${osname1} == "Ubuntu" ]] ; then
    if awk 'BEGIN{fnd=1};/dhcp4: true/{fnd=0} ; END{exit fnd}' /etc/netplan/50-cloud-init.yaml  ; then
       # we're setting the shell return value for fnd, so it's fnd=1 indicates error, 0 indicates success
           matchword="true"
        fi
    else
        # statement="$(grep 'BOOTPROTO' /etc/sysconfig/network/ifcfg-eth0 | cut -d '=' -f2 | sed "s/'//g" | xargs)"
        if awk 'BEGIN{fnd=1};/BOOTPROTO/{fnd=0);END{exit fnd}' /etc/sysconfig/network/ifcfg-eth0]] ;then
            matchword="dhcp"
    fi
fi

if [[ ${matchword} == "dncp" ]]
then
  # dhcp stuff statements
elif [[ ${matchword] = "true" ]] ; then
   # true stuff staments
else
   echo "Found value for matchword="XX${matchword}ZZ", can't continue"
   exit 1
fi

I would use a case statement for the final block.

Note that we don't need [[ ... ]] pairings when we test the return value of the called program, hence the awk 'END{exit retVal}' sort of code.

------- Edit -------

And because we are relying just on the finding of a certain term in a certain file and are NOT parsing the output in any way, this can be futher simplified by replacing the awk scripts with a simple grep -q, i.e.

if [[ ${osname1} == "Ubuntu" ]] ; then
    if grep -q 'dhcp4: true' /etc/netplan/50-cloud-init.yaml  ; then
       # grep returns true if it finds the search target in the supplied filelist
           matchword="true"
        fi
    else
        if grep -q 'BOOTPROTO' /etc/sysconfig/network/ifcfg-eth0 ;then
            matchword="dhcp"
    fi
fi

----------- end edit ---------

Untested as I don't have these OSes or the files and their contents available.

Hmm, I had one other comment to make, but got distracting cleaning up my code. When I think of it, I'll add it as a comment below.

IHTH

Sign up to request clarification or add additional context in comments.

3 Comments

@LaurentHee : Thanks for the "votes of confidence". I have make a few more changes and may have one more comment to add later. Glad this helped. Use your revised Q as a model for future Qs you post, and you'll get some good help. Good luck.
Thanks for the time you invest to help! You make the world a better place. I will keep that in mind. Wish you the best luck too.
@LaurentHee : Made another significant edit you might want to check out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.