Linux – How to print entire lines in BASH

How to print entire lines in BASH… here is a solution to the problem.

How to print entire lines in BASH

I’m new to bash scripting and I’ve been trying to print out whole lines but can’t find a way to work.

Here is my code

#!/bin/bash
MOTD=`cat /etc/motd | awk '{print $1}'`
if [ "$MOTD" = "WARNING" ]
then
    echo "Audit Criteria: Warning banner exist."
    echo "Vulnerability: No."
    echo "Details: $MOTD "
else
    echo "Audit Criteria: Warning banners does not exist."
    echo "Vulnerability: Yes."
    echo "Details: $MOTD "
fi

My output is:

Audit Criteria: Warning banner exist.
Vulnerability: No.
Details: WARNING:

Instead of warning: authorize use only
All events may be monitored and reported.
, only Warning appears in the details:

I think the problem is

MOTD=`cat /etc/motd | awk '{print $1}'` 

and

if [ "$MOTD" = "WARNING"] section, I’ve tried {print$0} but still can’t get it to work.

Solution

I’m guessing you want to get the first line of /etc/motd, not the first word. If so, use the following:

MOTD=$(head -1 /etc/motd)

Then a string comparison is made

if [[ $MOTD == WARNING* ]; then

You can see String contains in bash for more information about checking if a string contains a specific substring in bash.

Related Problems and Solutions