is_dir_empty(){ for file in "$1" do if [ "$file" != "$1" ]; then return 0 fi done echo "return 1" return 1 } file="/home/tmp/*.sh" if is_dir_empty "$file"; then echo "empty" else echo "not empty" fi
it outputs
return 1 not empty
so is_dir_empty returned 1 but if condition evaluated to false somehow.... why?
IF statements and their syntax. IF statements are used in making decisions in bash scripting. When an IF statement is introduced in a bash script, it is intended to check for certain conditions before running different commands in the script.
When creating complex or multi-conditional tests, that's when to use these Boolean operators. That is to say, if you're writing a bash script where two or more tests will have to return "True", then you have to use &&.
Bash IF. Bash IF statement is used for conditional branching in the sequential flow of execution of statements.
Types of if condition in shell script 1. Simple if statement In this type of statement, only the if condition is used, which essentially means that the... 2. If-Else condition This type of statement is used when the program needs to check one condition and perform a task if... 3. If elif else fi ...
Because shell scripts follow the Unix convention of expecting utilities to return zero for success and non-zero for failure, so boolean conditions are inverted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With