Argc and Argv in Bash

23,335

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

Share:
23,335
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    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"