Bash 从日志文件中读取

Bash 从日志文件中读取,bash,Bash,我想编译一个bash脚本,并选择要搜索的内容 ./mybash log.txt root@usa18:/home#muster?: ERROR list of all lines containing ERROR end or search again?: exit 这和你希望做的事情相似吗 #!/bin/bash read -p "search string:" muster saved="$IFS" IFS=$'\n' for i in $(grep -i $muster $1); d

我想编译一个bash脚本,并选择要搜索的内容

./mybash log.txt
root@usa18:/home#muster?: ERROR 
list of all lines containing ERROR
end or search again?: exit

这和你希望做的事情相似吗

#!/bin/bash

read -p "search string:" muster
saved="$IFS"
IFS=$'\n'
for i in $(grep -i $muster $1); do
  # $i now contains the line of which did match the search string
done
IFS="$saved"

以下是另一个版本:

#!/bin/bash

# Check the numer of parameters
if (( $# > 0 ))
then
    input="$1"    # Always enclose filename with quotes
else
    echo "USAGE: $0 filename" >&2
    exit 1
fi

while :      # true
do
    read -p "muster?:" muster
    grep  "$muster" "$input"

    read -p "Search again?:" ans
    if [[ $ans == [nN]* ]]
    then
        break
    fi
done
对不起,我解释得不好 谢谢你的帮助 我的问题是我确实理解了死亡和

#!/bin/bash
input=$1
read -p "what want you search? :" SEARCH
grep -e "{SEARCH}" "$1" > newlog.txt

老实说,我不明白问题是什么。你可以通过解释你的脚本应该做什么以及会发生什么来改进你的问题,理想的方式是人们可以轻松地转移到自己的机器上进行测试;read-p muster?:| sed您想通过管道向sed发送什么?现在,cat只会在控制台上显示文件,read不会写入管道,提示会转到stderr。$muster从何而来,我看不到它被设置。您是否尝试将代码粘贴到其中以查看它标记的语法和常见用法错误?祝你好运。请使用./script.sh文件名
#!/bin/bash
input=$1
read -p "what want you search? :" SEARCH
grep -e "{SEARCH}" "$1" > newlog.txt