为什么"〜"在这段代码中找不到shell?

时间:2017-10-09 03:16:03

标签: shell

我是shell的新手。这是代码: enter image description here

当我执行' zsh a'(脚本名称)时,结果为: enter image description here

为什么找不到'〜'?是'〜'是命令?还是因为环境变量?

1 个答案:

答案 0 :(得分:1)

您应该在shebang(zsh)行中指定#! - 并且您可以使用/usr/bin/env来确定可移植的位置(ish)。并且比较数值的运算符是-lt-gt-eq小于,大于等于。我还希望printf超过echo。总而言之,我想你想要

#!/usr/bin/env zsh    
printf "Please enter an integer ->"
read A
B=99
printf "B=%d\n" $B
if [[ "$A" =~ ^-?[0-9]+$ ]]; then
    if [[ "$A" -gt "$B" ]]; then
        printf "A>B (%d>%d)\n" "$A" "$B"
    elif [[ "$A" -lt "$B" ]]; then
        printf "A<B (%d<%d)\n" "$A" "$B"
    else
        printf "A=B (%d=%d)\n" "$A" "$B"
    fi
else
    printf "A (%s) is not an integer\n", "$A"
fi