LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack Search this Thread
Old 01-30-2005, 04:43 PM   #1
ChoKamir
LQ Newbie
 
Registered: Jan 2005
Distribution: Debian Sarge
Posts: 6

Rep: Reputation: 0
Bash problem, no executing of string


Hello

I made the following script:
Code:
#Sets the denominator
IFS=":"
#Reads the file line by line seperating the columns
while read user password uid gid info home bash; do
     #Checks if the user's home is a real home eg is in /home/
     if echo "$home" | grep /home/. ; then
          echo $user
     fi
done < /etc/passwd
It works fine except for the fact that it prints the content of the $home variables to the screen. Whenever i remove the echo command bash tries to execute the content of the $home variable which will result in an incorrect execution of the grep command.
Now my question is there a way to check if the content of the $home variable starts with /home/ without printing anything to the screen. The only thing i want to see appear are errors or the list of users having a real home.

Thanks

ChoKamir
 
Old 01-30-2005, 05:50 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Try this...
Code:
#!/bin/bash

IFS=":"
#Reads the file line by line seperating the columns
while read user password uid gid info home bash; do
     #Checks if the user's home is a real home eg is in /home/
     if [ `echo $home | grep -e "/home/."` ] ; then
        echo $user
     fi
done < /etc/passwd
or this...
Code:
cat /etc/passwd | grep -e "/home/." | awk -F: '{print$1}'
 
Old 01-30-2005, 11:17 PM   #3
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Awk itself can do it:

Code:
awk -F: '$6 ~ /\/home/ {print $1}' /etc/passwd
HTH.
 
Old 01-31-2005, 04:50 AM   #4
ChoKamir
LQ Newbie
 
Registered: Jan 2005
Distribution: Debian Sarge
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks a lot guys it works.

ChoKamir
 
  


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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String in Bash Shell nguyenzoro Linux - Newbie 4 11-19-2005 04:49 PM
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
Trivial bash question - executing $COMMAND atom Programming 4 06-05-2005 04:17 AM
bash + string manipulation dave bean Programming 7 02-16-2005 11:16 AM
BASH string expansions Barbarian Programming 2 11-21-2001 10:40 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration