LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 05-23-2007, 09:28 PM   #1
sachinh
Member
 
Registered: Jul 2004
Location: india
Distribution: RH
Posts: 189

Rep: Reputation: 30
Simple Bash Script


Hi,

How can we get a user's home directory for every iteration of "i" in the below script?

Script:-

for i in `cat UserList`
do
HomeDir= {This variable should get the value of the respective User's Home Directory in every iteration of "i" }
echo $HomeDir
done

Hope I'm clear.
Thanx in advance.
 
Old 05-23-2007, 09:40 PM   #2
sachinh
Member
 
Registered: Jul 2004
Location: india
Distribution: RH
Posts: 189

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by sachinh
Hi,

How can we get a user's home directory for every iteration of "i" in the below script?

Script:-

for i in `cat UserList`
do
HomeDir= {This variable should get the value of the respective User's Home Directory in every iteration of "i" }
echo $HomeDir
done

Hope I'm clear.
Thanx in advance.


Can it be done using Environment Variable $HOME ...But how ??
 
Old 05-23-2007, 09:44 PM   #3
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
You would want to parse /etc/passwd but this seems like a really inefficient way to do it. Still:

Code:
for i in `cat UserList`
do
    grep "$i" /etc/passwd | awk -F: '{print $6}'
done
 
Old 05-23-2007, 09:47 PM   #4
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
Note: this really depends on what you are trying to do and why. The above would be more helpful if it included the username with each homedir... or whatever.

And the entire thing can be reduced to one awk-command if you are looking for all users (even all between a range of user IDs).

The benefit of using one awk command is that the file is read once (not once per name like above) and you spawn less resources to get the job done.

Last edited by frob23; 05-23-2007 at 09:48 PM.
 
Old 05-23-2007, 09:47 PM   #5
sachinh
Member
 
Registered: Jul 2004
Location: india
Distribution: RH
Posts: 189

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by frob23
You would want to parse /etc/passwd but this seems like a really inefficient way to do it. Still:

Code:
for i in `cat UserList`
do
    grep "$i" /etc/passwd | awk -F: '{print $6}'
done

Thanks for the reply . And yes I'm afraid even I think thats a long way approach , as there is ready variable available on Unix thats $HOME..... can we use it in any way in above script??
 
Old 05-23-2007, 09:54 PM   #6
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
No we can't because it is only available to the user when they are logged in. And we are not logging in as each user (which would waste even more system resources).
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple bash script help zok Linux - General 4 12-15-2006 11:56 AM
Need simple bash script help rickenbacherus Linux - General 1 11-01-2006 02:44 AM
Simple bash script lp449 Linux - Networking 3 08-02-2005 04:25 PM
Simple bash script Soulful93 Programming 1 06-04-2005 07:26 PM
Simple BASH script fio Linux - General 5 01-18-2003 03:26 PM

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

All times are GMT -5. The time now is 03:16 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