如何在 bourne shell 脚本中使用 grep 命令在两者之间有空格时搜索新的输入参数



我想在文件中搜索用户输入的模式,内容如下。

the goof of the spoof
the horse of the sun

bourne shell 脚本 a.sh 如下

#!/bin/sh
var=$1
grep $var file
>>sh a.sh 'the horse'
grep: can't open horse
file:the goof of the spoof
file:the horse of the sun

请帮我解决这个问题。所以基本上我想知道如何工作,如果用户输入参数之间有空格。

引用变量:

#!/bin/sh
var="$1"
grep "$var" file

当您在$var中保留一个不带引号的空格时,会使 shell 将空格前的单词解释为搜索模式,将空格后的单词解释为它正在搜索的文件。

最新更新