【问题标题】:Bash (.sh) file not generating any output as same as terminal command in Ubuntu/LinuxBash (.sh) 文件不生成与 Ubuntu/Linux 中的终端命令相同的任何输出
【发布时间】:2020-10-27 15:33:07
【问题描述】:

需要帮助!我已经编写了以下 Ubuntu 终端命令并生成输出。现在我想通过编写下面的 (.sh) 脚本检查来做同样的事情:

Ubuntu terminal command:

sudo journalctl | grep -E 'authentication failure' | grep -E 'testusr1' | more
Output:
Jul 05 13:44:56 khUbuntu su[20464]: pam_unix(su-l:auth): authentication failure; logname= uid=1000 euid=0 tty=pts/1 ruser=kh rhost=  user=testusr1
Jul 05 14:56:44 khUbuntu su[23766]: pam_unix(su-l:auth): authentication failure; logname= uid=1000 euid=0 tty=pts/2 ruser=kh rhost=  user=testusr1

然后我写了以下word.sh(shell脚本),但它没有像上面的终端命令那样生成任何输出,我写错了什么??

#!/bin/bash

# User input:
echo "Enter what you want to Search : "
read inputword

#userinput="authentication failure AND testusr1"

b=$(echo ${inputword%*AND*})   # remove AND from the inputword and get 'authentication failure'
length=${#b}
echo "$length" # get the length of b
removeBack1=${b%%*( )} #remove any white space from tail
length=${#removeBack1} #check length again to verify with the top lenth $b
echo "$length"
echo "This is 1st value:$removeBack1"

a=$(echo ${inputword#*AND})    # removing word AND from inputword and get 'testusr1'
length=${#a}
echo "$length"
removeBack2=${a%%*( )}
length=${#removeBack2}
echo "$length"
echo "This is 2nd value:$removeBack2"

## --- This is not generating any output -----------
sudo journalctl | grep -E '$removeBack1' | grep -E '$removeBack2' | more

【问题讨论】:

    标签: linux bash shell ubuntu terminal


    【解决方案1】:

    我认为您希望在最后一个命令中包含变量,而不是字符串文字:

    sudo journalctl | grep -E "$removeBack1" | grep -E "$removeBack2" | more
    

    注意单引号到双引号的变化 - single 将字符串视为原始,而 double 扩展变量。

    【讨论】:

      猜你喜欢
      • 2017-05-01
      • 2019-04-01
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多