Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script that creates users in Linux as well as Oracle, when I create multiple users I use the following:

      cat /path/path/file.txt | while read USER PASS ORAUSER ROLE 

However I had the misfortune of leaving the role blank and it did some horrible things to my users access. I was wondering how I can check if the text has an empty variable.

share|improve this question

Ways to check if a variable is empty:

# bash
if [[ -z $var ]]; then
        echo var is empty

# POSIX sh
if [ -z "$var" ]; then
        echo var is empty
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.

share|improve this answer
That worked, I added the colons to the text file and used your example to come up with this: – Jose Ruiz Jul 25 '12 at 12:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.