为什么在bash字符串相等性测试中出现意外的运算符错误? [英] Why am i getting an unexpected operator error in bash string equality test?

查看:65
本文介绍了为什么在bash字符串相等性测试中出现意外的运算符错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第四行的错误在哪里?

if [ $bn == README ]; then

如果我将其写为

if [ $bn == README ]
then

if [ "$bn" == "README" ]; then

上下文:

for fi in  /etc/uwsgi/apps-available/* 
do 
        bn=`basename $fi .ini`
        if [ $bn == "README" ]
        then
                echo "~ ***#*** ~"
        else
                echo "## Shortend for convience ##"
        fi
done

推荐答案

您不能将==用于单括号比较([]).请改用single =.另外,您必须引用变量以防止扩展.

You can't use == for single bracket comparisons ([ ]). Use single = instead. Also you must quote the variables to prevent expansion.

if [ "$bn" = README ]; then

如果使用[[]],则可能适用,并且不需要引用第一个参数:

If you use [[ ]], that could apply and you wouldn't need to quote the first argument:

if [[ $bn == README ]]; then

这篇关于为什么在bash字符串相等性测试中出现意外的运算符错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆