LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-16-2009, 10:09 AM   #1
cylon
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Rep: Reputation: 0
Help understanding a bash script


I need help decifering this short script. There's no big thing I'm trying to accomplish, and I didn't write it, but I need to understand what its doing through each step. I'm not really a script writer, so I thought you all might be able to help me.

for D in $(cat /etc/passwd | awk -F : '{print $6}' | sort -u) ; do
ls -d "$D" >/dev/null 2>&1 || echo "$D" 1>&2 ;
done

My general understanding is that the output of the passwd file is being compared against another file and the results sorted to show only unique results. A directory listing is being run against the results of the $D variable and the output is being discarded.

I'm sure I've misinterpreted something, or just plain missed something all together. I am trying to get a better understanding of what's happening.
 
Old 09-16-2009, 10:15 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
It generates a list of home directories that do not exist.

EDIT:

In more detail the only purpose of the ls command is to test whether each home directory exists; if it does not then ls returns a non-zero exit status and the following logical OR (||) test is passed and the name of the missing directory is printed on stderr (which has file descriptor 2).

Could be done a little more elegantly and robustly.

Last edited by catkin; 09-16-2009 at 10:20 AM.
 
Old 09-16-2009, 10:18 AM   #3
nidsche
LQ Newbie
 
Registered: Jun 2008
Location: Germany
Posts: 21

Rep: Reputation: 16
Quote:
Originally Posted by cylon View Post
for D in $(cat /etc/passwd | awk -F : '{print $6}' | sort -u) ; do
ls -d "$D" >/dev/null 2>&1 || echo "$D" 1>&2 ;
done
If you try the script with
for D in $(cat /etc/passwd | awk -F : '{print $6}' | sort -u) ; do
echo $D
done

you will see that the for loop is giving the /etc/passwd to AWK. AWK prints column number 6 which has the Home Directory included.

now look at the ls command. This will try to list the entries in column number 6 as directories and throw the ouptput away. if this fails the echo command is in charge and it will output the name .

The conclusion is that the script shows non existing home directories

Regard Norbert

EDIT: remove typos
 
Old 09-16-2009, 10:47 AM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
The ls symdrome strikes back I'd rather use the -d builtin. You can save yourself one pipe as well.

Code:
for D in $(awk -F : '{print $6}' /etc/passwd | sort -u); do [ -d "$D" ] || echo "$D"; done
 
Old 09-16-2009, 11:04 AM   #5
cylon
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks for feedback

Thanks for the feedback. That helps a lot.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Understanding `ls /home |sed '/$user/ d' in Bash akiladila Linux - Newbie 2 03-01-2008 02:54 PM
bash script, understanding variables antis Programming 5 11-15-2007 05:27 AM
Help understanding a script Gins Programming 11 04-12-2007 03:13 AM
Need help understanding script....? jmr0311 Mandriva 3 08-08-2004 08:42 AM
Startup script, help understanding it..? jmr0311 Linux - Software 1 08-07-2004 06:59 PM


All times are GMT -5. The time now is 03:10 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration