本文介绍了-bash:[:=:预期为一元运算符.没有给出参数时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有我的shell脚本 myscript.sh

I have my shell script, myscript.sh below

#!/bin/sh
if [ $1 = "-r" ]; then
    echo "I am here"
fi

如果我使用运行.myscript.sh -r ,它与消息我在这里一起很好地工作.

If I run with . myscript.sh -r, it works well with message I am here.

但是,如果我只运行.myscript.sh ,它会投诉

But if I just run with . myscript.sh, it complaints

-bash:[:=:期望一元运算符

我的脚本中缺少什么?

推荐答案

您需要在$ 1前后添加引号.

You would need to add quotes around $1.

if [ "$1" = "-r" ]; then
    echo "I am here"
fi

当$ 1为空时,您会得到if [="-r"]这是一个语法错误.

When $1 is empty you are getting if [ = "-r"] which is a syntax error.

这篇关于-bash:[:=:预期为一元运算符.没有给出参数时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 00:04
查看更多