use conditional in bash script to check string argument

半腔热情 提交于 2019-12-03 10:12:17

What about the shorter :

#!/bin/bash

[[ $1 == A ]] && echo "A" || echo "not A"

?

And a beginner version (identical logic) :

#!/bin/bash

if [[ $1 == A ]]; then
    echo "A"
else
    echo "not A"
fi

Like Scott said, you have a syntax error (missing space).

explanations

Change the first line to:

if [ "$1" == "A" ]

The -eq operator is for integers. And as someone else mentioned, the space does matter before the ']'.

See here: http://tldp.org/LDP/abs/html/comparison-ops.html

Try putting a space between "A" and ].

You need a space after "A"

if [ "$1" -eq "A" ]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!