Ste*_*udy 0 centos shell-script
#### #!/usr/bin/sh
FILE="/home/steven/.bash_profile"
STRING="export PATH="$PATH:/opt/mssql-tools/bin""
if [ -z $(grep "$STRING" "$FILE") ]; then
echo 'the string is exist' >&2
else
echo 'the string doesnt exist' >&2
fi
Run Code Online (Sandbox Code Playgroud)
#### # .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
#### # User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
Run Code Online (Sandbox Code Playgroud)
为什么它总是返回 is 存在?
我们应该转义STRING 变量中的$和"。否则会扩大$PATH
将其声明为:
STRING="export PATH=\"\$PATH:/opt/mssql-tools/bin\""
Run Code Online (Sandbox Code Playgroud)
正如@muru 所评论的那样,请尝试
if grep -q "$STRING" "$FILE" ; then
echo 'the string exists' ;
else
echo 'the string does not exist' ;
fi
Run Code Online (Sandbox Code Playgroud)
由于export文件 bashrc 的第 12 行缺失,它总是会报告它不存在。