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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do i check if all parameters are set in bash?
# 1  
Old 09-07-2016
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:
Code:
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.
# 2  
Old 09-07-2016
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.
# 3  
Old 09-07-2016
here is the full code snippet:

Code:
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

# 4  
Old 09-07-2016
Hi,

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

Here is the sample run for your code:
Code:
$ cat try.sh
#!/bin/bash

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

Output:

Code:
$ ./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

# 5  
Old 09-07-2016
Quote:
Originally Posted by Jesshelle David
here is the full code snippet:

Code:
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

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:-
Code:
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:-
Code:
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~>


Last edited by wisecracker; 09-07-2016 at 04:44 PM.. Reason: Added example.
This User Gave Thanks to wisecracker For This Post:
# 6  
Old 09-08-2016
Quote:
Originally Posted by Jesshelle David
Code:
if [ $# -ne 4 ]
then echo "Not entered" 
else echo "entered"
fi

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:

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getopt eval set parameters not happening when the script is called through an application!

Hi, There is an Informatica tool through which unix scripts can be called. Now the requirement in my project is that a parent script calls a child script but this parent script has to be called through the Informatica tool. In Parent script I'm using TEMP=`getopt -o x:y: -l area:,volume:... (1 Reply)
Discussion started by: Panna
1 Replies

2. Shell Programming and Scripting

Grep final set of parameters from fit.log gnuplot file

I would like to grep the final set of fit parameters from a gnuplot log file to form columns that look like this. a_1001 b_1001 x_1001 a_1002 b_1002 x_1002 a_1003 b_1003 x_1003 . . . . . . . . . a_1250 b_1250 c_1250 At... (8 Replies)
Discussion started by: kayak
8 Replies

3. Shell Programming and Scripting

check parameters

hi all, i write a script in ksh but i have a problem I want to check the number of parameters while ] do echo "Inserisci la macchina che vuoi restorare o scrivi fine per terminare lo script, puoi restorare" $a "client: " read CLIENT echo $CLIENT the variable... (1 Reply)
Discussion started by: FrancescoIt
1 Replies

4. Shell Programming and Scripting

How to check on parameters

Hi I have a script that reads a directory name, and then prints all de items in that directory. Now that works, but I want to check on the parameters to be sure that the user has write just 1 parameter, and if not, than the script closes. (7 Replies)
Discussion started by: hss
7 Replies

5. Shell Programming and Scripting

How to check that passed parameters all have the same extension?

$ ls monkey.txt banana.csv tree.txt $ myscript monkey.txt tree.txt All extensions ARE alike. $ myscript *txt All extensions ARE alike. $ myscript monkey.txt banana.csv All extensions are NOT alike. $ myscript * All extensions are NOT alike. My brain has given up; what's the simplest... (11 Replies)
Discussion started by: cs03dmj
11 Replies

6. UNIX for Dummies Questions & Answers

check parameters

Hi, I have a script that takes two parameters. I would check the script to run if these parameters are to null or come with the correct data. If those parameters are to null, display a message, so if they come with two facts that are incorrect. As you might see that? Thx (1 Reply)
Discussion started by: pepeli30
1 Replies

7. Shell Programming and Scripting

Call single function multiple times diff set of parameters

Okay, not sure if it can be done, I would think it could be done and I'm just having a hard time with it. fun_close_wait(){ ipVar="${1} ${2}" portVar=`cat "${5}" | cut -d' ' -f 1` for ip in $ipVar do for port in $portVar do netstatVar=`netstat -n | grep... (4 Replies)
Discussion started by: cbo0485
4 Replies

8. UNIX for Dummies Questions & Answers

Parameters to check while differentiating two servers

Hi All, I have two solaris servers. Please tell me what all parameters i can check to find out the difference between two servers. how to differentiate based on H/W,S/W etc like i have two servers spdwa013 $ uname -an SunOS spdwa013 5.8 Generic_117350-61 sun4u sparc SUNW,Sun-Fire-480R ... (1 Reply)
Discussion started by: usha rao
1 Replies

9. Shell Programming and Scripting

awk parameters check

Is there a way to preform check if the parameters that was send with the command awk -f `file_name.awk` `input_file` before even it gets to the BEGIN section (i have tested a try to check in the BEGIN it doesn't let ,you must make it on the section that after the BEGIN) and then decide if the... (1 Reply)
Discussion started by: tal
1 Replies

10. Shell Programming and Scripting

[bash] Check if variable is set or blank

Hello guys. In my script, i have the following code: echo "The tarfile contains these directorys" tar -tf file.tar > tarlist.txt cat tarlist | awk -F/ '{print $1 "/" $2}' | nl echo "Enter path of the directory you want to extract or just press enter to extract everything: " read path... (1 Reply)
Discussion started by: noratx
1 Replies
Login or Register to Ask a Question