LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   AWK for logins -p (https://www.linuxquestions.org/questions/linux-newbie-8/awk-for-logins-p-4175434307/)

ln00biel 10-27-2012 01:32 AM

AWK for logins -p
 
By doing logins -p, I can see a list of users with blank passwords e.g

user1 111 other 1
user2 112 other 1
user3 113 other 1

I am trying to print "yes" when there are users with blank password and "no" if there arent any users with blank passwords. I had used
Code:

logins -p | awk -F"" '{if($!="")print "yes"; else print "no"}'
and it returns me:
yes
yes
yes

So how do I make output it such that it prints one "yes" only? Thanks.

Heraton 10-27-2012 02:29 AM

Try this...
 
Hello!

Should work this way:
Code:

if [ "$(logins -p | wc -l)" -ne 0 ]; then echo yes; else echo no; fi
Note: I could not test it, as logins is not available in my distro.

Regards, Heraton

ln00biel 10-27-2012 03:45 AM

Quote:

Originally Posted by Heraton (Post 4815977)
Hello!

Should work this way:
Code:

if [ "$(logins -p | wc -l)" -ne 0 ]; then echo yes; else echo no; fi
Note: I could not test it, as logins is not available in my distro.

Regards, Heraton

Thanks Heraton. The code keeps giving me an output of "no" when it is supposed to be "yes". May I just ask if
Code:

wc -l
returns an integer? I want to store
Code:

logins -p | wc -l
as a variable but it returns a blank when I echo it. I am writing this in a bash script.

chrism01 10-28-2012 08:36 PM

The wc cmd can return various numbers, all integer http://linux.die.net/man/1/wc


All times are GMT -5. The time now is 12:03 PM.