LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-08-2015, 10:06 PM   #1
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Rep: Reputation: 47
Opposite to expected response in awk


I am using following awk code on a file called dirlist. However, it is giving reverse response- printing out directories if they are there when asked about not there and vice versa:

Code:
$ cat dirlist
/root
/boot
/sys
/a_dir
/b_dir

# TRYING TO PRINT IF DIRECTORY IS PRESENT: 
$ cat dirlist | awk ' { if ( system(" [ -d " $1 " ] ")  ) print $1 } '
/a_dir
/b_dir

# TRYING TO PRINT IF DIRECTORY IS ABSENT: 
$ cat dirlist | awk ' { if ( system(" [ ! -d " $1 " ] ")  ) print $1 } '
/root
/boot
/sys
Where is the problem. Thanks for your insight.
 
Old 12-08-2015, 11:33 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
system() returns the command's exit status. "[ -d a_dir ]" has an exit status of 0 when a_dir exists, and 1 when it doesn't exist.

Now when the if condition is nonzero, awk considers it as true. So, a nonexisting directory returns 1, which is true.
 
Old 12-09-2015, 01:45 AM   #3
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Original Poster
Rep: Reputation: 47
Perfect explanation.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk computed regex not working as expected grail Programming 12 06-01-2011 11:16 AM
[SOLVED] awk does not print the value of the variable as I expected, please help jozelo Linux - Newbie 4 04-04-2011 01:17 PM
printf \b not working as expected in AWK Rockydell Programming 5 11-13-2009 07:28 AM
Limiting awk to desired response ZimMonkey Linux - Newbie 5 06-28-2009 09:27 PM
what's the opposite of operator!() jhorvath Programming 5 11-02-2003 08:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:06 AM.

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