1

Trying to run the following script:

echo "Is autofs Enabled?"
cmd=`systemctl is-enabled autofs`
echo $cmd
if [[ $cmd = "enabled" ]]; then
echo "Yes autofs is enabled"
elif [[ $cmd = "disabled" ]]; then
echo "No autofs is disabled"
else echo "Autofs not found"
fi

If autofs is not installed, script results in:

Failed to issue method call: No such file or directory
Autofs not found

Expected output of script if autofs is not installed:

Autofs not found

How do I change the script to not print "Failed to issue method call: No such file or directory"?

Edit: Thanks everyone. Answers were exactly what I was looking for.

1
  • Rather than adding a separate "Thank you", upvote the answer(s) that helped you, and consider "accepting" the answer that was the most helpful in resolving your issue. These are the best ways of showing gratitude on this site. Accepting an answer not only marks the question as resolved, but also signals to future readers that the accepted answer actually solved the issue. More information about this is available here: unix.stackexchange.com/help/someone-answers Commented Dec 15, 2019 at 8:13

2 Answers 2

1

How about redirect the error stream to /dev/null?

[ $(systemctl is-enabled autos 2>/dev/null) = "enabled" ] && echo true || echo false

On a side note using backticks while assigning the output to a variable is lowest common denominator for shell portability modern shells recommend migrating to assigning variables from commands to $()

0

You can examine the message (stdout) produced by systemctl as well as its exit status (the latter via the $? shell variable). The error message you seek to avoid is delivered to stderr, so you need to redirect that stream if you want to suppress it. Non-zero exit status can indicate either that the device was not found or that it was disabled (see the manual for more information).

#!/bin/bash -l    
service=$1
echo "Is $service enabled?"
msg=$(systemctl is-enabled "$service" 2>/dev/null) # capture message
res=$? # capture exit code

if [ -z "$msg" ]; then  # message is empty if service not found
  echo "$service not found"
else  # service could be enabled, disabled, static, etc.
  if [ "$res" -eq 0 ]; then # enabled
    echo "Yes $service is enabled: $msg"
  else
    echo "No, $service is disabled"
  fi
fi

Saving that script as tmp.sh, you can test with various inputs:

$ ./tmp.sh autofs
    Is autofs enabled?
    Autofs not found
$ ./tmp.sh anacron.timer
    Is anacron.timer enabled?
    Yes anacron.timer is enabled: enabled
$ ./tmp.sh runlevel6.target
    Is runlevel6.target enabled?
    No, runlevel6.target is disabled

Given the variety of other cases, you might be better off using a case statement for more granular handling of the status, e.g.,

case "$msg" in
    enabled*)  echo "Yes $service is enabled" ;;
    disabled) echo "No, $service is disabled" ;;
    linked*) # etc..
    ;;
    static*) # etc..
    ;;
esac

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.