LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2013, 06:40 AM   #1
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Rep: Reputation: 18
awk test if variable is null or empty


so,

i have on SERVER
/files/backup1
/files/backup2


ssh root@SERVER "ls -l /files/backup1 2>/dev/null|awk '{if(length(\$5)==0){print \"X\"}else{print \$5}}'"

-->i'm getting file size

ssh root@SERVER "ls -l /files/backup2 2>/dev/null|awk '{if(length(\$5)==0){print \"X\"}else{print \%5}}'"

--> i get no result ( was suppose to prin X


Is something I'm missing...
 
Old 05-30-2013, 08:18 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
if( (0 + length(\$5)) == 0)

I doubt the backslash is needed. Also there is a % where I think you meant a $. I don't like inbound SSH as root.

Last edited by linosaurusroot; 05-30-2013 at 08:20 AM.
 
Old 05-30-2013, 08:39 AM   #3
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
sorry so:

/files/backup2 <-- is missing

in this case
ssh root@SERVER "ls -l /files/backup2 2>/dev/null|awk '{if((0+length(\$5)==0)){print \"X\"}else{print \$5}}'"

"ls -l" should return no value, in this case awk should print "X", but i'm getting nothing...
 
Old 05-30-2013, 09:31 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You need to think about how awk works ... ie. you pass data to it and it works on the lines of data. Therefore, you pass zero lines to it the awk never starts (kinda), hence no output.

Simple example to test is create an empty file and use:
Code:
awk '{print "hello"}' empty_file
As you will see, 'hello' is never printed as there is no data to work with ... this is the same as what you are doing with no output from ls through the pipe to awk.
 
Old 05-30-2013, 09:38 AM   #5
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
got it, but in this case how do i check the input line fro awk is NULL or has vo value ?
 
Old 05-30-2013, 09:45 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Maybe you could explain the purpose of what you are doing as this approach seems flawed and parsing ls wouldn't seem to be the way to go?
 
Old 05-30-2013, 03:50 PM   #7
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
i have a perl script:

and i'm trying to make an array of files size , and if file is found to append to array F_size and if not found to put something like 'NULL'.

so the idea is to loop through file and if found the file get the size if not fond... something elese
 
Old 05-31-2013, 03:03 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
hmmm ... colour me still a little confused.

1. Is the perl script run locally or on the servers?

2. I am no perl guru, but am pretty positive it would be able to handle the absence of files and also have functions to retrieve the file size.
 
Old 05-31-2013, 03:11 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you may check the existence of those file in awk, sending filename(s) by stdin over ssh
 
Old 05-31-2013, 04:09 AM   #10
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by fritz001 View Post
i have a perl script:

and i'm trying to make an array of files size , and if file is found to append to array F_size and if not found to put something like 'NULL'.

so the idea is to loop through file and if found the file get the size if not fond... something elese

Sounds like you want a hash ... test the files with stat() and use the Perl value "undef" when a file is missing.
 
Old 05-31-2013, 04:50 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Agree with above: do it all in Perl, using a hash and stat() and 'undef'.
http://perldoc.perl.org/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripting Help--Check empty string condition (not null, but empty!) sungchoiok Linux - Newbie 4 01-01-2012 03:46 PM
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 12:44 AM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
Overflow on /dev/null, please empty the bit bucket. little_ball Slackware 17 01-15-2004 07:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration