1

I'm automating the installation of an NFS server. Prior to starting the firewall i want to check:

systemctl status firewalld 
Firewall status: ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: active (running) since Fri 2019-04-19 02:28:46 UTC; 27min ago Main PID: 129969 (firewalld) CGroup: /system.slice/firewalld.service └─129969 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid 

1) need to unmask firewalld

2) is firewalld dead

for this i execute

STATUS=`systemctl status firewalld` echo "Firewall status: ${STATUS}" MASKED=`grep -e "masked" $STATUS` DEAD=`grep -e "dead" $STATUS` 

however, the grep command fails with:

grep: unrecognized option '--nofork' Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information. grep: unrecognized option '--nofork' Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information. 

grep fails on the pattern: --nofork grep assumes it's an option

I have read various posts but haven't found a solution to this problem. I thought I might use "sed" to remove the "--" but it fao=ils with the same error

Cheers, Roland

5
  • are you doing this in bash? Commented Apr 19, 2019 at 3:17
  • yes, in bash script @jhnc Commented Apr 19, 2019 at 3:32
  • Yes I could use the exit code of the command: ``` systemctl status firewalld``` however, in both cases, if firewalld is masked or dead, the exit code is 3. And I have to know if firewalld is masked or dead Commented Apr 19, 2019 at 3:34
  • See mywiki.wooledge.org/Quotes Commented Apr 19, 2019 at 15:09
  • 1
    Thank you @EdMorton I will read the quote Commented Apr 20, 2019 at 12:07

1 Answer 1

2

$STATUS is a string, not a file name. In a POSIX shell, try:

MASKED=$(printf "%s" "$STATUS" | grep -e "masked") 

In bash, the pipeline can be eliminated by using a here-string:

MASKED=$(grep -e "masked" <<<"$STATUS") 

Comments

  1. It is best to use lower or mixed case for your shell variables. The system uses all caps for its variables and you don't want to accidentally overwrite one of them.

  2. Unless you explicitly want the shell to perform expansions, including word splitting or pathname expansion, place all your shell variables inside double quotes.

Examples

Observe that this generates the error that you see:

$ Status="Firewall status: ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: active (running) since Fri 2019-04-19 02:28:46 UTC; 27min ago Main PID: 129969 (firewalld) CGroup: /system.slice/firewalld.service └─129969 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid" $ grep -e "running" $Status grep: unrecognized option '--nofork' Usage: grep [OPTION]... PATTERN [FILE]... 

Both of these versions, however, work:

$ printf "%s" "$Status" | grep -e "running" Active: active (running) since Fri 2019-04-19 02:28:46 UTC; 27min ago $ grep -e "running" <<<"$Status" Active: active (running) since Fri 2019-04-19 02:28:46 UTC; 27min ago 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @John1024 I will try following that advice. Years back I had a teacher mentionig to use all upper case letters, and yes usually I put enclose variables in quotes and brackets.. And the example does work great.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.