Linux Ksh 和 if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21110054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:51:15  来源:igfitidea点击:

Ksh and if statements

linuxshellif-statementscriptingksh

提问by Lahkra

I have done very little programming in C++, and I am not really understanding how to use code in KornShell (ksh) on the Knoppix OS. The resources given to us by the professor has been little, so it is hard to work out. The two text books we have for class talks mostly about commands in variations of UNIX, though little to do with ksh.

我很少用 C++ 编程,我不太了解如何在 Knoppix OS 上使用 KornShell (ksh) 中的代码。教授给我们的资源很少,所以很难解决。我们课堂上的两本教科书主要讨论 UNIX 变体中的命令,尽管与 ksh 关系不大。

Write a shell script that accepts exactly one argument which must be a positive integer. (My second one has two)

编写一个只接受一个参数的 shell 脚本,该参数必须是一个正整数。(我的第二个有两个)

Ksh Code:

Ksh 代码:

NUMBER=
read -p NUMBER
# Test that one argument was input.
if [[ $# -ne 1 ]];then
  echo "Please enter an integer as an argument"
  exit 1
elif [[ $NUMBER -le 0 ]];then # Test value of argument is less than or equal to zero
    echo "Please enter a number > 0" 
    exit 1
fi

while [["$NUMBER" -ne 1]];do
    printf $NUMBER
if [[$NUMBER -gt 1]];then
        printf ","
    fi
NUMBER=$(($NUMBER-1))
done
printf $NUMBER

When I run this from the shell I keep getting "Please enter an integer as an argument" as an output, though the entry was 3, or something like that.

当我从 shell 运行它时,我不断收到“请输入一个整数作为参数”作为输出,尽管条目是 3,或者类似的东西。

I noticed that there was not anything for user input, so I tried to enter this myself with

我注意到用户输入没有任何内容,所以我尝试自己输入

read -p NUMBER

before the if statement.

在 if 语句之前。

What am I missing from the code to continue on with the rest of the script to be run?

我在代码中遗漏了什么以继续运行脚本的其余部分?

回答by choroba

You should double quote your variables in [ ... ]conditions, or use the extended [[ ... ]]conditions (yes, a third type of condition). For numeric operations, (( .. ))are usually used (oh my, fourth type!).

您应该在[ ... ]条件中用双引号引用变量,或使用扩展[[ ... ]]条件(是的,第三种条件)。对于数字运算,(( .. ))通常使用(哦,天哪,第四种类型!)。

See man bashor Advanced Bash Scripting Guide.

请参阅man bash高级 Bash 脚本指南

回答by Dropout

The first passed argument should be acquired by using

应通过使用获取第一个传递的参数

NUMBER=

don't know what you mean by $X..

不知道你说的什么意思$X..

Note: $0 is the command executed, for example in ./myscript.sh foo

注意:$0 是执行的命令,例如在 ./myscript.sh foo

the $0 = ./myscript.shand $1 = foo

$0 = ./myscript.sh$1 = foo

After you set it to you variable NUMBERyou can check if it's a positive integer:

将其设置为变量后,NUMBER您可以检查它是否为正整数:

if [[ $NUMBER =~ ^[0-9]+$ ]]; then
    echo "It's ok!"
else
    echo "BAD :("
fi

回答by YoMismo

To accept just one argument you should check the number of arguments passed to the script (which is what I think your teacher is asking):

要只接受一个参数,您应该检查传递给脚本的参数数量(我认为您的老师正在询问):

if [[ $# -ne 1 ]] 

$# gives you the number of arguments passed to your script, so it is easy to check the number of argumets that are being passed to it. Now you only need to check if it is an integer and the sign. I don't know what exactly is what your teacher want you to know, but you can use awk's functions to check wether the parameter is a number, an integer and its sign (the sign is also easy with ksh).

$# 为您提供传递给脚本的参数数量,因此很容易检查传递给它的参数数量。现在你只需要检查它是否是一个整数和符号。我不知道你的老师到底想让你知道什么,但是你可以使用 awk 的函数来检查参数是数字、整数及其符号(符号也很容易使用 ksh)。

回答by jlliagre

You should remove the read -p NUMBERinstruction which defeat the previous one.

您应该删除read -p NUMBER击败前一个指令的指令。

You shoud then fix that line:

然后您应该修复该行:

elif [[ $NUMBER -le 0 ]];then

to

elif [ "$NUMBER" -le 0 ];then

The last part is using the wrong tests and is missing an ending new line:

最后一部分使用了错误的测试并且缺少结束的新行:

while [["$NUMBER" -ne 1]];do
    printf $NUMBER
if [[$NUMBER -gt 1]];then
        printf ","
    fi
NUMBER=$(($NUMBER-1))
done
printf $NUMBER

should be

应该

while [ $NUMBER -ne 1 ];do
    printf $NUMBER
    if [ $NUMBER -gt 1 ];then
        printf ","
    fi
    NUMBER=$((NUMBER-1))
done
printf "%s\n" $NUMBER