linux - 接受任何参数的 Shell 脚本

标签 linux string bash shell if-statement

这很奇怪。

这是我的文件

#!/bin/bash
num=$#
clear
ip=$1
op=$2
ts=$3
echo $ts
if [ $ts=='32' ]; then
        echo "32 Bit"
elif [ $ts=='64' ]; then
        echo "64 Bit"
else
        echo "NONE"
fi
echo "Complete!"

现在,该文件还有更多内容,但是 if 语句无论如何都会显示“32 位”,即使我运行 ./compile.sh input output 64。

我也试过在变量周围加上引号。第一个 echo 语句实际输出“64”。

最佳答案

使用单个 = 来测试字符串。

if [ $ts = "32" ]

所以你的脚本现在变成了这样:

#!/bin/bash
num=$#
clear
ip=$1
op=$2
ts=$3
echo $ts
if [ $ts = "32" ]; then
        echo "32 Bit"
elif [ $ts = "64" ]; then
        echo "64 Bit"
else
        echo "NONE"
fi
echo "Complete!"

编辑:
正如 Chris 提到的那样,即使使用双 = 它也可以工作,但你必须在 == 周围留出空格,如下所示

if [ $ts == "32" ]

关于linux - 接受任何参数的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21039862/

相关文章:

.net - 基本字符串比较中完全奇怪的行为

bash - 别名在 Bash 脚本中不起作用

linux - 编写一个脚本来删除 Linux 中不同文件夹中存在的文件

java - 如何转义Java正则表达式中的字符

c# - 删除小数而不四舍五入

linux - bash -x 或 set -x

linux - 为什么 `gzip`压缩同一个文件时会生成不同的二进制文件?

java - 无法在 Centos7 上运行 jmeter 4.0 脚本

ruby Watir : cannot launch browser in a thread in Linux

linux - 编译内核报错: stdio. h: No such file or directory