在第一击文件错误

问题描述 投票:-1回答:1

我写我的第一个bash脚本,我不断收到错误,我不是真正知道在那里我错了。下面是我想执行脚本:

#!/bin/ksh
#Script Name: printnum.sh
# Verify the number of arguments and exit if not equal to 1`enter code here`
$mynum = "5"
echo $mynum
if [$mynum -gt 1]
then
    printf "error: program must be executed with 1 argument\n"
    printf "usage: $0 value (where value >= 1)\n"
    exit 1
fi
# Verify argument is a positive number
if [$mynum -lt 1]
then
    printf "error: argument must be a positive number\n"
    printf "usage: $0 value (where value >= 1)\n"
fi
# Store command line argument in variable i
$mynum="$i"
# Loop and print $i while decrementing variable to =1 (with comma)
while [$i -gt 1]
do
    printf "$i, "
done

以下是我得到的错误:

./printnum.sh[3]: =: not found [No such file or directory]

./printnum.sh[5]: [: ']' missing
./printnum.sh[11]: [: ']' missing
./printnum.sh[16]: =: not found [No such file or directory]`enter code here`
./printnum.sh[17]: [: ']' missing
/export/home/hanko01/HOME/itec400/homework>

任何帮助这里将不胜感激!

linux ksh
1个回答
2
投票

几件事情第一:

  1. 家当应该是#!/斌/庆典如果试图使用bash脚本。 (如果已经使用bash不需要,通过打印$ SHELL在终端查询)
  2. 如果(以及同时)的条件下应适当地间隔开。对于如。如果/空间/ [/空间/条件/空/]
  3. 至于说在注释中,同时指派值,不要使用$。
  4. $#为命令行参数的数量。

您可以从纠正代码了解的其余部分:

#!/bin/bash
#Script Name: printnum.sh
# Verify the number of arguments and exit if not equal to 1 `enter code here`
if [ $# -ne 1 ]
then
        printf "error: program must be executed with 1 argument\n"
        printf "usage: $0 value (where value >= 1)\n"
        exit 1
fi
# Verify argument is a positive number
if [ $1 -lt 1 ]
then
        printf "error: argument must be a positive number\n"
        printf "usage: $0 value (where value >= 1)\n"
fi
# Store command line argument in variable i
i=$1
# Loop and print $i while decrementing variable to =1 (with comma)
while [ $i -gt 1 ]
do
        printf "$i, "
        i=$((i-1))
done
© www.soinside.com 2019 - 2024. All rights reserved.