简体   繁体   English

如何在BASH中打印整行

[英]How to print entire line in BASH

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. 我是bash脚本的新手,我一直在努力打印出整条生产线但却无法找到工作方式。

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. 而不是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. if [ "$MOTD" = "WARNING" ]部分,我已经尝试{print$0}但仍然无法使其工作。

I guess you want to get the first line of /etc/motd , not the first word. 我想你想得到/etc/motd的第一行,而不是第一个单词。 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. 您可以在bash中检查String contains,以获取有关检查字符串是否包含bash中特定子字符串的更多信息。

Perhaps it would be simpler to do the whole thing in awk: 也许在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. 条件NR==1并且块末尾的exit意味着仅处理文件的第一行。

The code above is the most similar to your bash script but you could make it a lot shorter using variables: 上面的代码与您的bash脚本最相似,但您可以使用变量使其更短:

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

You are using only using variable MOTD and it is having only value WARNING. 您只使用变量MOTD,它只有值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. 或者,如果/ etc / motd中有多行,则需要只打印一行。

#!/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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM