Bash 将命令输出与字符串进行比较

Sha*_*ary 3 bash git-bash

输出相同,并且总是 echo need to pull。如果我删除条件周围的引号$textif则会引发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)

Gil*_*not 6

最好这样做,=~对于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)