How do i check if all parameters are set in bash?

Hi,

I have 4 parameters passed to my shell and i validate if all four are entered using the following snippet:

if [ $# -ne 4 ]
then echo "Not entered" 
else echo "entered"
fi

I get the following output as 'Not entered even when i enter the values for all prompts.
Please advise.

Thanks.

Does any one or more parameter have a whitespace in it not contained inside quotes?

If so perhaps this is the reason. Without much more info like:-

1) OS in use.
2) Shell version in use.
3) A little more of your code to give us more insight as to what is going on.
4) Anything else that might be useful.

We can't give you much more help.

here is the full code snippet:

USERNAME="$1"
export USERNAME
REMOTE_SERVER="$2"
export REMOTE_SERVER
LOCAL_DIRECTORY="$3"
export LOCAL_DIRECTORY
REMOTE_DIRECTORY="$4"
export REMOTE_DIRECTORY
DIRECTION="$5"
export DIRECTION
LOG_FILEPATH="$6"
export LOG_FILEPATH
LOG_FILENAME="$7"
export LOG_FILENAME
BIN_PATH="$8"
export BIN_PATH
LOCAL_JOBNAME="$9"
export LOCAL_JOBNAME
REMOTE_JOBNAME="$10"
export REMOTE_JOBNAME

##Validating if all inputs were entered
echo "Entering the program `date`" 																				
if [ $# -ne 10 ]
then
echo "One or more inputs to this program were not entered." 													
echo "Please verify the Appworx module" 																		
echo "Aborting the program on `date`"																			
exit 1
else
echo "All inputs to this program have been entered" 															
fi

Hi,

You did not write what are the command line arguments given to the script.

Here is the sample run for your code:

$ cat try.sh
#!/bin/bash

if [ $# -ne 4 ]; then
       echo "not entered 4 cmd args"
else
 echo "Good"
fi

Output:

$ ./try.sh 123 345 345 56
Good

$ ./try.sh 123 345 345
not entered 4 cmd args

$ ./try.sh "123 345 345"
not entered 4 cmd args

As greet_sed and I have both pointed out, does any one or more of these paramters have whitespaces
in them without quotes or NULL values?

Possible ones are:-

USERNAME="$1"
LOCAL_DIRECTORY="$3"
REMOTE_DIRECTORY="$4"
LOG_FILEPATH="$6"
LOG_FILENAME="$7"
BIN_PATH="$8"
LOCAL_JOBNAME="$9"
REMOTE_JOBNAME="$10"

Example longhand OSX 10.7.5 default bash terminal:-

Last login: Wed Sep  7 20:38:15 on ttys000
AMIGA:barrywalker~> test()
> {
> echo $#
> }
AMIGA:barrywalker~> test "Hello World everybody"
1
AMIGA:barrywalker~> test Hello World everybody
3
AMIGA:barrywalker~> 

With this code, you would reject the input with "Not entered", if the caller supplied MORE than 4 parameters. This is at least a confusing error message. Here is how I would write it in bash:

if (( $# < 4 ))
then
  echo Only $# parameters supplied, 4 expected
elif (( $# > 4 ))
then
  echo You entered $(( $# - 4 )) 'parameter(s)' too many
fi

This would also help tracking down the error.