8

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"
  • 2
    First of all, you must reverse the logic of your `if...then...else` construct. Besides, contrary to the C convention, in bash `$0` doesn't count as an argument, thus `EXPECTED_ARGS` must be 1 in your script – Leon Jul 18 '17 at 06:08
  • I set it to 1 but nothing changed. –  Jul 18 '17 at 06:10
  • 1
    You must also reverse your `if ... then ... else` statement (i.e. print an error when the actual count of arguments is not equal to the expected count of arguments, and do the job otherwise) – Leon Jul 18 '17 at 06:11
  • -ne is not equal... you swapped the conditions. if [ $# -eq $EXPECTED_ARGS ] .... – Zibri Mar 23 '18 at 10:02
  • From my perspective, looking for Bash's ARGC equivalent (with Google) (IE `$#` or `ARGC=$(( $# ))`), the question linked to as the original of the "duplicate" TOTALLY DOESN'T ANSWER that. Making me want to improve the answers here, yet I cannot. – dlamblin Jul 05 '18 at 08:05

1 Answers1

2

Define a variable: ARGC=$#

and your if statement will look like

if [ $ARGC -ne $MAX_ARGS ]; then

Legend:

  • -ne = not equal

  • -gt = greater than

  • -eq = equal to

Melebius
  • 6,183
  • 4
  • 39
  • 52
Paul afk
  • 87
  • 4