All checks were successful
learn org at code.softwareshinobi.com/linux.softwareshinobi.com/pipeline/head This commit looks good
3.4 KiB
3.4 KiB
Conditional Expressions
Conditionals are how your scripts make decisions: run different code based on a true/false outcome.
In Bash, [[ is your go-to for testing files, strings, and numbers.
Let's start with a basic example:
touch conditional_test.sh
Open conditional_test.sh and add:
#!/usr/bin/env bash
my_var="data"
if [[ -n "$my_var" ]]; then # If variable is not empty
echo "Variable contains data."
fi
Save, make executable, and run:
chmod +x conditional_test.sh
./conditional_test.sh
Output: Variable contains data.
Now, for your full reference:
File Tests
These check file attributes. Use "${file}" to handle spaces in names.
[[ -e "${file}" ]]- True iffileexists.[[ -f "${file}" ]]- True iffileexists and is a regular file.[[ -d "${file}" ]]- True iffileexists and is a directory.[[ -s "${file}" ]]- True iffileexists and is not empty (size > 0).[[ -r "${file}" ]]- True iffileexists and is readable.[[ -w "${file}" ]]- True iffileexists and is writable.[[ -x "${file}" ]]- True iffileexists and is executable.[[ -L "${file}" ]]- True iffileis a symbolic link (-his also used).[[ -b "${file}" ]]- True iffileexists and is a block device.[[ -c "${file}" ]]- True iffileexists and is a character device.
String Tests
Compare string properties and values.
[[ -v "${varname}" ]]- True ifvarnameis set (has a value).[[ -z "${string}" ]]- True ifstringhas zero length (empty).[[ -n "${string}" ]]- True ifstringhas non-zero length (not empty).[[ "${string1}" == "${string2}" ]]- True if strings are equal. (==allows pattern matching inside[[.)[[ "${string1}" != "${string2}" ]]- True if strings are not equal.[[ "${string1}" < "${string2}" ]]- True ifstring1sorts beforestring2lexicographically.[[ "${string1}" > "${string2}" ]]- True ifstring1sorts afterstring2lexicographically.
Numeric Comparisons
Compare integer values.
[[ ${arg1} -eq ${arg2} ]]- True ifarg1equalsarg2.[[ ${arg1} -ne ${arg2} ]]- True ifarg1not equalsarg2.[[ ${arg1} -lt ${arg2} ]]- True ifarg1less thanarg2.[[ ${arg1} -le ${arg2} ]]- True ifarg1less than or equal toarg2.[[ ${arg1} -gt ${arg2} ]]- True ifarg1greater thanarg2.[[ ${arg1} -ge ${arg2} ]]- True ifarg1greater than or equal toarg2.
Logical Operators
Combine multiple conditions.
- AND (
&&): Both conditions must be true.# Example: file exists AND is not empty if [[ -f "my_file.txt" && -s "my_file.txt" ]]; then echo "File is present and has content." fi - OR (
||): At least one condition must be true.# Example: either file or directory exists if [[ -f "another_file.txt" || -d "another_dir" ]]; then echo "Found a file or a directory." fi
Exit Status Checks
$?: The exit status of the last executed command. 0 means success. Any non-zero value indicates an error.
- Success:
command_to_run if [[ $? -eq 0 ]]; then echo "Command succeeded." fi - Failure:
command_to_run if [[ $? -ne 0 ]]; then echo "Command failed." fi