I have this bash script getting a Boolean parameter, based on the answer here
I created this simple condition, for some reason I am getting false results
#!/bin/bash
copyFile() {
echo "copyFile #Parameter:$1"
if [ "$1" ]; then
echo "Parameter is true"
else
echo "Parameter is false"
fi
}
copyFile true
copyFile false
execution result:
[admin@local_ip_tmp]$ ./test.sh
copyFile #Parameter:true
Parameter is true
copyFile #Parameter:false
Parameter is true
The last result should have "Parameter is false" I am not sure what is going on.
What am doing wrong?
false
is just a string and it is not emptyif [ -n "$1" ]
(if$1
is not empty). What are you after?if [ "$1" = "true" ]
orif [ "$1" != "true" ]
orif [ "$1" = "false" ]
or …[
and]
.if (( $1 )); then ...
will treat0
as falsey, and all positive integers as truthy....
with actual content of thethen
block, it needs no changes, as long as your$1
is0
for false and1
for true.