0

I get the error on the 7th line. Any ideas? I checked for spaces and there are none.

#!/bin/bash
if test $# -eq 0
then
    echo "No arguments"
elif test $# -eq 1
    echo "$1"
elif test $# -eq 2
    echo "$1 $2"
else
    echo "More than 2 arguments"
fi
  • Are your quotes actually and ? Can you make sure they are " throughout? – doneal24 Feb 22 at 16:52
  • Yes all quotes are " and not ' – Kyan Hamad Feb 22 at 16:53
  • plug for shellcheck.net – Jeff Schaller Feb 22 at 16:55
  • @KyanHamad: That isn't what Doug was getting at. The quotes you pasted here are curly quotes generally created in programs like microsoft word and will not work in code. You need to ensure you have straight quotes, regardless if it is a double or single quote. – Jesse_b Feb 22 at 17:00
  • @Jesse_b I was using straight quotes. The issue I found out was that I needed to use then statements after the elif as well as quoting the $#. Thanks for clarifying! – Kyan Hamad Feb 22 at 17:10
3

The syntax for if/elif/else/fi requires a then after each "elif":

#!/bin/bash
if test "$#" -eq 0
then
    printf 'No arguments\n'
elif test "$#" -eq 1
then
    printf '%s\n' "$1"
elif test "$#" -eq 2
then
    printf '%s %s\n' "$1" "$2"
else
    printf 'More than 2 arguments\n'
fi

I've also replaced your echos with printfs and quoted the instances of $#.

2

You need a then after elif.

#!/bin/bash
if test $# -eq 0
then
    echo "No arguments"
elif test $# -eq 1
then
    echo "$1"
elif test $# -eq 2
then
    echo "$1 $2"
else
    echo "More than 2 arguments"
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.