Santana Santana - 5 months ago 11
Bash Question

Simple bash shell script to ensure that provides two arguments

I'm trying to write simple greetings script in bash to ensure that the user provides at least two arguments. 1)The first argument provides how long we want to delay before the greeting prints. 2) The second argument provides what message we want to display. Sorry I'm fairly new to shell scripting.

#!/bin/sh
if[$# -ge 2]
then
sleep $1
shift
banner $*
else
echo "Usage: Greeing seconds word(s)"
fi

Answer

You should use:

#!/bin/sh
if [ $# -ge 2 ]
then
    sleep $1
    shift
    banner $*
else
    echo "Usage: Greeing seconds word(s)"
fi

You were missing:

  • a space after if
  • a space around [ and ]