用于检查变量的其他用户的grep bashrc
我正在尝试grep用户的bashrc,以检查行是否已经存在。 我试过这个:用于检查变量的其他用户的grep bashrc,bash,syntax-error,Bash,Syntax Error,我正在尝试grep用户的bashrc,以检查行是否已经存在。 我试过这个: if [ [ su - ${1} -c "cat ~/.bashrc | grep ${2} | wc -c | tr -d ' '" ] -gt 0 ] if [ su - ${1} -c "cat ~/.bashrc | grep ${2} | wc -c | tr -d ' '" ] $1是一个用户名 $2是变量名 执行中 + '[' su - user -c 'cat ~/.bahsrc | grep PA
if [ [ su - ${1} -c "cat ~/.bashrc | grep ${2} | wc -c | tr -d ' '" ] -gt 0 ]
if [ su - ${1} -c "cat ~/.bashrc | grep ${2} | wc -c | tr -d ' '" ]
$1是一个用户名
$2是变量名
执行中
+ '[' su - user -c 'cat ~/.bahsrc | grep PATH | wc -c | tr -d '\'' '\''' ']'
line xxx: [: too many arguments
有可能这样做吗?我是否需要使用返回值(实际上所有的都是
$?=0
)如果您只是检查一行是否存在,则无需涉及wc
或tr
或其他任何内容;这应足以:
if su - ${1} -c "grep -q '${2}' ~/.bashrc" ; then
echo line exists in file
else
echo line does not exist in file
fi
如果在~/.bashrc
中找到$2
的内容,这将回显文件中存在的行
为了帮助理解这里发生了什么,请记住if
语句的语法是(来自bash
手册页):
其中list
是一个或多个命令。在本例中,我们使用grep
命令的退出代码来确定表达式的“真值”。如果您只是检查一行是否存在,则无需涉及wc
或tr
或其他任何内容;这应足以:
if su - ${1} -c "grep -q '${2}' ~/.bashrc" ; then
echo line exists in file
else
echo line does not exist in file
fi
如果在~/.bashrc
中找到$2
的内容,这将回显文件中存在的行
为了帮助理解这里发生了什么,请记住if
语句的语法是(来自bash
手册页):
其中list
是一个或多个命令。在本例中,我们使用grep
命令的退出代码来确定表达式的“真值”