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
jesse_b
  • 37,005

2 Answers2

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 $#.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
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
Freddy
  • 25,565