Fellas,
Am new to unix os/ and here the situation , I am trying to write multiple condition statement inside if but it throws me a error
here is my piece of code ,
if [ [ $file1 ne " " ] ] && [ [ $file2 ne " " ] ] && [[ $file3 ne " " ] ]
then
commands
fi
error : line 15 : [ : missing `]`
can someone please advise me how to fix it
Hello xeccc5z,
Welcome to forums, please use code tags for commands/codes/Inputs which you are using into your posts as per forum rules. Could you please try following.
if [[ -z $file1 && -z $file2 && -z $file3 ]]
then
commands
fi
You could go through manual entry for test
too.
Thanks,
R. Singh
RavinderSingh13
Thanks for your response and since I am new this I may missed out the rules and i will do it from now on
And my query is I am passing file names as command line arguments and my condition should alert the user if he fails to pass the parameter
am saving those parameters in a variable called file1, file2 and file3
i believe [ -z file ] will check whether file is a zero size file or not , correct me if iam wrong
xeccc5z:
RavinderSingh13
Thanks for your response and since I am new this I may missed out the rules and i will do it from now on
And my query is I am passing file names as command line arguments and my condition should alert the user if he fails to pass the parameter
am saving those parameters in a variable called file1, file2 and file3
i believe [ -z file ] will check whether file is a zero size file or not , correct me if iam wrong
Hello xeccc5z,
You welcome, you could hit THANKS button at left side of any post for any useful post to a person. Yes, for NULL values it should work.
Thanks,
R. Singh
1 Like
Ravinder,
As i said am passing the file names as command line argument and saving them in variables as file1,file2,file3. When user fails to pass anyone of the arguemnt it should display error message and quit from the script hence i would like to for if condition and checks those file1,file2,file3 values are not empty , how could i right that
If you're checking the first 3 positional parameters instead of the variables you specified before, change:
if [[ -z $file1 && -z $file2 && -z $file3 ]]
in Ravinder's suggestions to:
if [[ -z $1 && -z $2 && -z $3 ]]
but, of course, all of this assumes that you're using a shell that recognizes [[ expression ]]
syntax.
1 Like
RudiC
June 7, 2016, 11:35am
7
The -z
test checks for a string's zero length, e.g. if the file name was given. To check for a file's contents being zero or not, use the -s
test.
1 Like
Why not use bash's special parameter #
?
# Expands to the number of positional parameters in decimal.
Example:
#!/bin/bash
if [[ "$#" -ne "3" ]]; then
echo "Need 3 files, you supplied $#, please try again."
exit 1
fi
set -x
file1=$1
file2=$2
file3=$3
set +x
Test:
$ ./script
Need 3 files, you supplied 0, please try again.
$ ./script foo
Need 3 files, you supplied 1, please try again.
$ ./script foo bar
Need 3 files, you supplied 2, please try again.
$ ./script foo bar baz
+ file1=foo
+ file2=bar
+ file3=baz
+ set +x
$