3

I'm new to bash scripting and I've been trying to print out the entire line but couldn't find a way to work.

This 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 the WARNING:Authorized uses only All activity may be monitored and reported. , only "WARNING" appeared in the Details:

I believe the problem lies on the

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

and

if [ "$MOTD" = "WARNING" ] parts, I've tried {print$0} but still could not get it to work.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
fdfdfd
  • 501
  • 1
  • 7
  • 21
  • This is expected, as you are assigning only the first word of the message to the variable `MOTD`. – Tom Fenech Nov 13 '14 at 11:48
  • 1
    Some minor problems. You should not use `cat` with programs that can read data itself, like `awk`. You should avoid old and outdated back-tics, use parentheses. So change the `MOTS` line to: `MOTD=$(awk '{print $1}' /etc/motd)` – Jotne Nov 13 '14 at 11:49
  • 1
    @Skynet please take care when editing after a previous edit has already been submitted. Your edit undid a lot of improvements that I made to the question. – Tom Fenech Nov 13 '14 at 12:00
  • Ok sorry I didn't checked that another edit was undergoing @TomFenech – Arnab Nandy Nov 13 '14 at 12:02

3 Answers3

2

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

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

and then do the string comparison with

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

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

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
2

Perhaps it would be simpler to do the whole thing in awk:

awk 'NR==1{
    if($1=="WARNING") {
        print "Audit Criteria: Warning banner exists."
        print "Vulnerability: No."
    }
    else { 
        print "Audit Criteria: Warning banner does not exist."
        print "Vulnerability: Yes."
    }
    print "Details: " $0
    exit
}' /etc/motd

The condition NR==1 and the exit at the end of the block mean that only the first line of the file is processed.

The code above is the most similar to your bash script but you could make it a lot shorter using variables:

awk 'NR==1{if($1=="WARNING"){b="exists";v="No"}else{b="does not exist";v="Yes"}
printf "Audit Criteria: Warning banner %s.\nVulnerability: %s.\nDetails: %s\n",b,v,$0
exit}' /etc/motd
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • note `/etc/motd` normally contains a lot of lines. Hence, you may want to end everything with a `exit` to avoid a lot of output. – fedorqui Nov 13 '14 at 12:21
  • 1
    @fedorqui I wasn't aware of that, thanks for letting me know. I've edited to only process the first line, then exit. – Tom Fenech Nov 13 '14 at 12:27
  • Yes, motd stands for `m`essge `o`f `t`he `d`ay and is used to show many things (or none). It is commonly linked to `/etc/issue` and can contain security informations, the name of the server... – fedorqui Nov 13 '14 at 12:29
  • 1
    Thanks for the info. I thought it meant [Match of the Day](http://www.bbc.co.uk/programmes/b007t9y1) ;) By the way, information is uncountable (we don't say "informations") :) – Tom Fenech Nov 13 '14 at 12:38
  • Ooops yet another synonym for motd: my Mistake Of The Day! Or even Misspelling of the day :) – fedorqui Nov 13 '14 at 13:01
0

You are using only using variable MOTD and it is having only value WARNING.

#!/bin/bash

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

Or in case if you have multiple lines in /etc/motd and you need to print only one line then.

#!/bin/bash

MOTDL=`grep WARNING /etc/motd`
MOTD=`cat /etc/motd | awk '{print $1}'`    
if [ "$MOTD" = "WARNING" ]    
then
    echo "Audit Criteria: Warning banner exist."        
    echo "Vulnerability: No."       
    echo "Details: $MOTDL "    
else        
    echo "Audit Criteria: Warning banners does not exist."      
    echo "Vulnerability: Yes."      
    echo "Details: $MOTDL"    
fi
Sriharsha Kalluru
  • 1,743
  • 3
  • 21
  • 27