命令在linux shell中找不到,并且在使用if else时出现语法错误。

问题描述 投票:1回答:1
#!/bin/bash
read n
sd=0
rev=0

if[[ $n -lt 0 ]];
then
echo "Not a positive number"
else
while [[ $n -gt 0 ]]
do
sd=$(( $n % 10 ))
rev=`expr $rev \* 10 + $sd`
n=$(( $n /10 ))
done

echo $rev

.scriptprog4.sh: 第6行: if[[ 123 -lt 0 ]]: 命令未找到.scriptprog4.sh: 第7行: 意外标记附近的语法错误。then' ./scriptprog4.sh: line 7:那么

linux bash shell unix
1个回答
1
投票

有一个缺失 fi 语句(强烈建议将代码缩进)。这是一个工作脚本。

#!/bin/bash
read n
sd=0
rev=0

if [[ $n -lt 0 ]]
then
    echo "Not a positive number"
else
    while [[ $n -gt 0 ]]
    do
        sd=$(( $n % 10 ))
        rev=`expr $rev \* 10 + $sd`
        n=$(( $n /10 ))
    done
fi

echo $rev

和一个执行。

$ ./scriptprog4.sh 
123
321

0
投票

.scriptprog4.sh: 第6行: if[[[ 123 -lt 0 ]]: 命令未找到。

是由于 if[[ 这两个词在bash中都是保留词,必须用空格隔开。

if [[ $n -lt 0 ]]; then
© www.soinside.com 2019 - 2024. All rights reserved.