输出相同,并且总是 echo need to pull。如果我删除条件周围的引号$text,if则会引发too many arguments错误。
var="$(git status -uno)" &&
text="On branch master Your branch is up-to-date with 'origin/master'. nothing to commit (use -u to show untracked files)";
echo $var;
echo $text;
if [ "$var" = "$text" ]; then
echo "Up-to-date"
else
echo "need to pull"
fi
Run Code Online (Sandbox Code Playgroud)
最好这样做,=~对于bash regex:
#!/bin/bash
var="$(git status -uno)"
if [[ $var =~ "nothing to commit" ]]; then
echo "Up-to-date"
else
echo "need to pull"
fi
Run Code Online (Sandbox Code Playgroud)
或者
#!/bin/bash
var="$(git status -uno)"
if [[ $var == *nothing\ to\ commit* ]]; then
echo "Up-to-date"
else
echo "need to pull"
fi
Run Code Online (Sandbox Code Playgroud)