Bash 中的 Argc 和 Argv

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45158709/
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-09-18 16:17:53  来源:igfitidea点击:

Argc and Argv in Bash

linuxbashshellargvargc

提问by

I written the following script which gets name of a file and then assemble and link the file. But it doesn't work. What is the problem with it?

我编写了以下脚本,它获取文件名,然后组合并链接该文件。但它不起作用。它有什么问题?

EXPECTED_ARGS=2


if [ $# -ne $EXPECTED_ARGS ]
then
        echo "[+] Assembling with Nasm."
        nasm -f elf32  -o .o

        echo "[+] Linking ..."
        ld .o -o 

        echo "[+] Done!" 

else
        printf  "\nInvalid number of arguments, please check the inputs and try again\n"

fi;

When I run it without passing any args, it doesn't shows following error:

当我在不传递任何参数的情况下运行它时,它不会显示以下错误:

printf  "\nInvalid number of arguments, please check the inputs and try again\n"

回答by Paul afk

Ok, try like this

好的,试试这样

define a variable ARGC=$#

定义一个变量 ARGC=$#

and you if statement will look like

你 if 语句看起来像

if [ $ARGC -ne $MAX_ARGS ]; then

如果 [ $ARGC -ne $MAX_ARGS ]; 然后

Legend:

传奇:

-ne = not equal

-ne = 不相等

-gt = greater than

-gt = 大于

-eq = equal to

-eq = 等于