How to compare that file name is empty?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare that file name is empty?
# 1  
Old 12-06-2009
How to compare that file name is empty?

i am finding some pattern this way..

fname=`grep -w "^$index" $HOME/UnixCw/backup/Path.txt`

how to check that fname is empty i.e. if pattren doesnt found then i want to do other operations....

Last edited by AbhijitIT; 12-06-2009 at 03:22 PM..
# 2  
Old 12-06-2009
look at the -s file test operator
# 3  
Old 12-06-2009
I tried but its not working...can you please elaborate more..I am very new to unix...sorry for trouble
# 4  
Old 12-06-2009
Code:
if [ -z $fname ]
then
    # do something
fi

Read the man page for test.
# 5  
Old 12-06-2009
ignore my post about -s. I misunderstood what you were trying to do. there is no need to have a $fname variable if your just testing for a match.

Code:
if grep -q -w "^$index" $HOME/UnixCw/backup/Path.txt
then
   #do something
fi

# 6  
Old 12-07-2009
if [ -z $variable ]

This will check whether the $variable is Null.
# 7  
Old 12-07-2009
Quote:
Originally Posted by frank_rizzo
ignore my post about -s. I misunderstood what you were trying to do. there is no need to have a $fname variable if your just testing for a match.

Code:
if grep -q -w "^$index" $HOME/UnixCw/backup/Path.txt
then
   #do something
fi

You could also do:

Code:
grep -q -w "^$index" $HOME/UnixCw/backup/Path.txt

if [ ! $? = "0" ]
then
    #dowhatever
fi

If your grep finds the string the exit status will be 0 and you can branch based upon that, or the reverse.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format: input chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18 chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

if/else with an empty file...

Hi there, I'm just starting out with shell scripting and I'm trying to make a test where the script will basically run yum check-update to find out if the server has any available and if it does it e-mails me but if not it will just stop there. I have it working if there are actually... (7 Replies)
Discussion started by: Imnewtothis
7 Replies

3. Red Hat

Compare Empty Variable

Hello All, I am running the below code in my script. I want if jk is empty nothing should be appened to the file total_usage. but apparently its not happening.Kindy let me know how to do it. ################################### jk=`ps auxf |grep -w $inputline|tr -s " "|cut -d... (0 Replies)
Discussion started by: ajaincv
0 Replies

4. Shell Programming and Scripting

Compare columns and rows with template, and fill empty slots.

Hi, I'm working on a script that will take the contents of a file, that is in a row and column format, and compare it to a arrangment file. Such that if there is any or all blanks in my content file, the blank will be filled with a flag and will retain the row and column configuration. Ex. ... (2 Replies)
Discussion started by: hizzle
2 Replies

5. Shell Programming and Scripting

while file is empty

how can i use while loop ? while file is empty do.... if not empty do ..... in bash (1 Reply)
Discussion started by: Trump
1 Replies

6. UNIX for Dummies Questions & Answers

Getting same exit status for empty and non empty file

Hi All, I am checking for a empty input file to do some further action , but I am getting exit status 0 in both the cases , for empty and non empty file both. The value of $? is coming 0 in if part also and else part too. #!/bin/ksh if ]; then echo "data" # exit 0 echo "$?" else... (4 Replies)
Discussion started by: mavesum
4 Replies

7. Shell Programming and Scripting

how to see if the file is empty or not?

hi how can I determine, if a file is empty or not?I am using read line clause. The script should be like: while read line do if(file is empty) then; ...... done < $blacklist (1 Reply)
Discussion started by: tjay83
1 Replies

8. UNIX for Dummies Questions & Answers

Trying to empty file using > but the file size increasing when next append

AIX 5.3 / KSH I have a Java application which creates a log file a.log. I have a KSH script which does the following action cp a.log /directory2/b.log > a.log After this the file size goes to 0 as per "ls -l" Then next time when the application writes into this file, the file size... (4 Replies)
Discussion started by: firdousamir
4 Replies

9. Shell Programming and Scripting

File is not empty?

How do I check to make sure a file is not zero bytes? Something like: if then ????? (7 Replies)
Discussion started by: lesstjm
7 Replies

10. UNIX for Advanced & Expert Users

empty file in hp-ux

Hi, I need your help. How can I create an empty filename with a specific size, in hp-ux? Regards, Mizi (2 Replies)
Discussion started by: Mizi
2 Replies
Login or Register to Ask a Question