我有一个如下变量。
variable = This script is not found
if [[ "$variable" = ~ "not found" ]];
then
echo "Not Found"
else
echo "Its there"
if
执行im低于错误时,
line 4: syntax error in conditional expression
./test.sh: line 4: syntax error near `found"'
./test.sh: line 4: `if [[ "$variable" = ~ "not found" ]]; '
任何人都可以指出我,我在这里失踪了什么?
答案 0 :(得分:22)
LIST="some string with a substring you want to match"
SOURCE="substring"
if echo "$LIST" | grep -q "$SOURCE"; then
echo "matched";
else
echo "no match";
fi
祝你好运;)
答案 1 :(得分:10)
在指定的位置与您的版本进行比较:
variable="This script is not found" # <--
if [[ "$variable" =~ "not found" ]] # <--
then
echo "Not Found"
else
echo "Its there"
fi # <--
你不能在赋值中放置=,并且你需要引用一个包含空格的字符串文字。你不需要尾随;如果你打算把它放在自己的路上。然后if-then以“fi”而不是“if”结束。
答案 2 :(得分:2)
这是if语句的正确构造
if [[ "$variable" =~ "not found" ]]; then
echo "Not Found";
else
echo "Its there";
fi
答案 3 :(得分:-1)
我尝试了以下代码,这些代码总是以真或假的方式返回相同的结果
if [[ "$variable" =~ "not found" ]]; then
echo "Not Found";
else
echo "Its there";
fi