Ways to check if a variable is empty:
# bash
if [[ -z $var ]]; then
echo var is empty
fi
# POSIX sh
if [ -z "$var" ]; then
echo var is empty
fi
printf %s\\n "${var:-var is unset or null}"
: "${var:=default value for var}"
This still doesn't solve the problem of missing or misaligned fields in your input file. If the ORAUSER
field is missing the ORAUSER
variable will get assigned the value of the ROLE
field. If there are too many fields, the last variable, ROLE
, will get all the extra fields. I would recommend switching your text file format to use a separator that is not whitespace, so that empty fields are explicitly delimited. For example, /etc/passwd
uses colons (:
).
If you haven't yet, I would also recommend reading BashFAQ 1 for some collective wisdom on properly parsing text files line-by-line / field-by-field in shell scripts and bash
specifically.