关于shell中$VARIABLE ${VARIABLE} "$VARIABLE"

这是一个关于添加记录到指定文件的小工具

首先给出正常运行的代码

#!/bin/bash
echo "Choose an action to executive!"
echo "1|append):add a line to the file
2|cat)print the content of the file
3|quit)exit the process"

read action
case $action in
    1|append)
        echo "the file you're going to edit is: $1"
        echo "type \"cat\" for content of $1, type \"quit\" to leave"
        echo "and enter what you want to append, then it will add to thelast of the file......"
        for ((i=0;i<100;i=i+1))
        do
        echo "you've edit the $1 for $i time(s)"
            read content
            if [ "$content" == "cat" ]; then
                    less $1
                else if [ "$content" == "quit" ]; then
                    {    echo "process suspend ...exit"
                        exit 0
                    }
                        else
                        {    echo "$content" >> $1
                            echo "any else?---->"
                        }
                    fi
            fi
        done
        ;;
    2|cat)
        cat $1
        ;;
    3|quit)
        echo "you choose to leave, thank you for using Enote"
        exit 0
        ;;
    *)
        echo "choose an action to executive, if not i'll quit directly!!"
        ;;
esac


这里采用字符串对append功能进行测试

当24行的echo命令的引用变只要采用了$号作引用后,不论加不加“”双引号在面,变量是否加{}都没有影响输出

now, echo without "" & {}
now echo string is with {} but no ""
it works

但是对于shell中条件判断的test命令来说,并不是一回事,这里采用的字符串比较(包含多个word,所以才有这样的问题)
now, test the 'test' command contains quoted string without ""
it replys: ./Enote: line 17: [: too many arguments
now, two 'test' command are without "" in quoting variables
then,it replys: ./Enote: line 17: [: too many arguments
./Enote: line 19: [: too many arguments
now, in command 'test' {} are added to quote variables
replys: ./Enote: line 17: [: too many arguments
./Enote: line 19: [: too many argumequit
now, it changes to original
replys nothing

可见,对于 [ "string1" == "string2" ] 是不能去引号的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值