How to check whether a variable is empty or contains some value?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check whether a variable is empty or contains some value?
# 1  
Old 07-02-2013
How to check whether a variable is empty or contains some value?

hi,

i want to check whether a a variable contains some value or is empty in a shell script. so if the variable contains some value i want to do some job and if the variable doesnt contain any value then i need to skip that job.

here is a sample script

read_filenames.sh contains

Code:
FILENAME=$1

FILE="s1.txt"

if [ -n $FILENAME ]; then

      FILE=$FILENAME

fi

echo "FILE = {$FILE}"


so, when i run the script without passing any argument, variable "FILE" should contain "s1.txt". so the output should be

Code:
$ sh read_filenames.sh

FILE = {s1.txt}

but when i pass an argument to the script, then the variable "FILE" should contain the argument value. so the output should be

Code:
$ sh read_filenames.sh p1.txt

FILE = {p1.txt}

currently i am using the above code, but if i dnt pass any argument to the script. the output is

Code:
FILE = {}

i read somewhere that

Code:
if [ -n $FILENAME ]; then

checks whether the variable is empty or not. but i dnt think that its doing that job. can anyone tell how can i do the above.
# 2  
Old 07-02-2013
Try:

Code:
FILENAME="s1.txt"

FILE=${1:-$FILENAME}

printf 'FILE = {%s}\n' "$FILE"

This User Gave Thanks to radoulov For This Post:
# 3  
Old 07-02-2013
Code:
FILE=${1:-s1.txt}
echo $FILE

This User Gave Thanks to balajesuri For This Post:
# 4  
Old 07-03-2013
ya it works fine.. Can you explain me.
Code:
FILE=${1:-$FILENAME}

or

Code:
FILE=${1:-s1.txt}

# 5  
Old 07-03-2013
If $1 is empty, then ${1:-s1.txt} expands to "s1.txt". It's like telling, if $1 is empty use the default value "s1.txt" instead.

$FILENAME is nothing but "s1.txt". So, both the approaches mean the same thing.
These 2 Users Gave Thanks to balajesuri For This Post:
# 6  
Old 07-03-2013
In your original script snippet, replace -n (non-zero check) with -z (zero check), and it will fly. man bash:
Quote:
-z string
True if the length of string is zero.
string
-n string
True if the length of string is non-zero.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Stupid question to check if variable is empty

Hi, I am simply trying to check if the variable is empty in the code below but it isn;t working. Anything that I might have missed here #Check if values in job card are not empty title=`cat $filename | grep "TITLE:" | cut -d ":" -f3` if ] then echo "10:Title Empty" ":Fail">> $rptfile... (13 Replies)
Discussion started by: nua7
13 Replies

2. Shell Programming and Scripting

Cant check empty string

Hello So i have that script collection, in which i have a single script to create a configuration file. In there, i have multiple occourences of something like this: prj_title=$(tui-read "What is the TITLE? ($prj_name):") ] && prj_title="${prj_name/_/ }" They all work as expected, if... (5 Replies)
Discussion started by: sea
5 Replies

3. Shell Programming and Scripting

Check if the string is empty

I am reading from a file and executing the jobs with/without parameters as the job requires. File job1 R job2 job3 Y 123 if then <job>.ksh else <job>.ksh $params fi This works fine if the line read from the file has parameters it executes like job1.ksh R But for... (2 Replies)
Discussion started by: nw2unx123
2 Replies

4. Homework & Coursework Questions

Check whether a Directory is empty or not

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1.pls tell me the command for checking whether a given directory is empty or not . 2. can i check what is the... (1 Reply)
Discussion started by: upvan111
1 Replies

5. Shell Programming and Scripting

Check whether a Directory is empty or not

1.pls tell me the command for checking whether a given directory is empty or not . 2. can i check what is the last copied item in a directory . and i yes , i want to move that last copied item in another directory . pls help me with shell code for these two tasks thanks (1 Reply)
Discussion started by: upvan111
1 Replies

6. Shell Programming and Scripting

How to check a variable for empty and a newline

I have a variable with a new line. I want to check this variable for empty or a new line Can anyone advise (4 Replies)
Discussion started by: Muthuraj K
4 Replies

7. Shell Programming and Scripting

How to check the variable is empty with spacing

How to check the variable is empty or not? aaa=" " how to check aaa variable is empty or just spacing? If only spacing inside.. it will asume it is empty. some are 6 spacing, or 8 spacing.. as long as variable is empty with spacing.. anyone can help me? (2 Replies)
Discussion started by: ryanW
2 Replies

8. Shell Programming and Scripting

check whether the directory is empty or not

I have the list of users in user.log, under each user folder there is sub1 folder is there. i want to check whether sub1 is empty or not, if it is empty i have to skip that user user folder and iterate next user folders. i have the sample code,its not giving not proper results. while read line... (8 Replies)
Discussion started by: KiranKumarKarre
8 Replies

9. Shell Programming and Scripting

How to check if two variable are empty strings at once? (bash)

I need to check if $1 or $2 are empty before continuing but I don't know if bash has any logic of the sort. This is what I'm looking for - except that "and" doesn't seem to work. if and ;then ... Thank you! :D (4 Replies)
Discussion started by: ph0enix
4 Replies

10. UNIX for Dummies Questions & Answers

How to check if a file is empty?

Hi Masters..... I have problem !!! I need to check number of records in a file and if it is zero or file is empty i need to do some task. if ; then echo "File s empty" else echo "Not empty" fi so how to check this condition. I used wc -l < filename.txt => 1 for zero records same result... (1 Reply)
Discussion started by: shreekrishnagd
1 Replies
Login or Register to Ask a Question