Check if file is empty with variables, bash | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Check if file is empty with variables, bash

UNIX for Dummies Questions & Answers


Tags
bash, file, solved, variables

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-19-2013
bjoern456 bjoern456 is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 20 September 2013, 5:50 AM EDT
Posts: 11
Thanks: 12
Thanked 0 Times in 0 Posts
[Solved] Check if file is empty with variables, bash

Hello again!

I have some trouble with scripting in bash.
In the following script I read from a folder with the files


Code:
line0_Ux.xy
line1_Ux.xy
line2_Ux.xy
.
.
.

Some of the files are empty. For those I would like to print a "0" in list.
I think the problem with the code is that bash thinks "$i_Ux" is the variable, but Im not sure..


Code:
        for i in $( ls -v ); do
            echo item: $i
	    if [ ! -s line$i_Ux.xy ]; then 
	      echo -e "0" >> list
	    else
	      cat $i | awk '{a+=$2; n++;} END  {print a/n}' >> list 
        done


Thanks a lot!!
Sponsored Links
    #2  
Old 09-19-2013
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 17 April 2014, 3:54 AM EDT
Location: Switzerland - GE
Posts: 5,490
Thanks: 148
Thanked 361 Times in 338 Posts
you are missing a fi, thats all, just before the last line...


Code:
for i in $( ls -v ) 
do
    echo item: $i
    if [ ! -s line$i_Ux.xy ]; then 
        echo -e "0" >> list
    else
        cat $i | awk '{a+=$2; n++;} END  {print a/n}' >> list 
    fi
done

The Following User Says Thank You to vbe For This Useful Post:
bjoern456 (09-20-2013)
Sponsored Links
    #3  
Old 09-19-2013
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 17 April 2014, 3:54 AM EDT
Location: Switzerland - GE
Posts: 5,490
Thanks: 148
Thanked 361 Times in 338 Posts
That said looking at your script now, do you mind explaining what $( ls -v ) is supposed to give and what OS we are dealing with ( and the shell used perhaps?) because -v is not implemented on AIX or HP-UX (11.11...) and on solaris10 its a -l equivalent except that it displays also ACL stuff... but my guess would be a linux simple sort ls in which case your script is not working because $i is the name of the file! in which case line$i_Ux.xy would be lineline0_Ux.xy_Ux.xy for your first occurence which it cant find to test...
My 2 cents...

Last edited by vbe; 09-19-2013 at 11:09 AM..
The Following User Says Thank You to vbe For This Useful Post:
bjoern456 (09-20-2013)
    #4  
Old 09-19-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 17 April 2014, 7:46 AM EDT
Location: Aachen, Germany
Posts: 3,514
Thanks: 51
Thanked 819 Times in 782 Posts
(linux) man ls:
Quote:
-v natural sort of (version) numbers within text
If that is not absolutely essential, try
Code:
for i in *          
  do echo item: $i
     if [ -s line${i}_Ux.xy ]
        then awk '{a+=$2; n++;} END  {print a/n}' $i
        else echo "0"
     fi
  done > list

The Following User Says Thank You to RudiC For This Useful Post:
bjoern456 (09-20-2013)
Sponsored Links
    #5  
Old 09-20-2013
bjoern456 bjoern456 is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 20 September 2013, 5:50 AM EDT
Posts: 11
Thanks: 12
Thanked 0 Times in 0 Posts
I am using the terminal in openSuse 12.3 and ls -v lists the files in a numerical way like this: (1,2,3,..,10,11,..,100).

Thank you guys, now it works! I lost the "fi" and $i was allready the name of the file as you said.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[Solved] Bash test 2 variables to see if ones greater by n jaysunn Shell Programming and Scripting 2 03-13-2012 01:19 PM
Bash-Shell: If-Clause to check if file is empty ABE2202 Shell Programming and Scripting 5 08-03-2009 11:16 AM
check if file is empty stolz Shell Programming and Scripting 8 03-22-2009 11:15 PM
How to check if two variable are empty strings at once? (bash) ph0enix Shell Programming and Scripting 4 10-24-2008 02:18 PM
How to check if a file is empty? shreekrishnagd UNIX for Dummies Questions & Answers 1 09-09-2008 08:21 AM



All times are GMT -4. The time now is 02:27 PM.