Argc and Argv in Bash

user7995295 picture user7995295 · Jul 18, 2017 · Viewed 18.9k times · Source

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 $1 -o $1.o

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

        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"

Answer

Paul afk picture Paul afk · Sep 7, 2017

Ok, try like this

define a variable ARGC=$#

and you if statement will look like

if [ $ARGC -ne $MAX_ARGS ]; then

Legend:

-ne = not equal

-gt = greater than

-eq = equal to