Stupid bash scripting question

Steven W. Orr steveo at syslang.net
Wed Aug 24 10:52:00 EDT 2005


On Wednesday, Aug 24th 2005 at 10:32 -0400, quoth Cole Tuininga:

=>I've received two different responses to this request - both of which
=>seem to work just fine.  My thanks to those who responded.  I'm a little
=>curious if one solution has any advantages over the other:
=>
=>Solution 1:
=>
=>user="username"
=>f=`eval "echo ~${user}"`
=>
=>
=>Solution 2:
=>
=>user="username"
=>f=`getent passwd $user|cut -f6 -d:`
=>
=>
=>It seems that either will do the job, I'm just wondering (for the
=>purpose of my own "betterment" and improved knowledge of shell scripting
=>*grin*) about advantages of either approach.  The only one I've come
=>with so far is that Solution 2 requires a separate process to run.

Good question. Using the tilde is a a bash thing that was taken from the 
Cshell.

Actually, the nr of processes may suprise you. Solution 1 is actually 
using three processes: The shell running the script, the shell represented 
by the backquotes, and the child of the backquotes which is the echo. Note 
that echo is built in but had you run something that wasn't builtin that 
would be another process.

In solution 2, you have four processes: the script itself, the 
backquotes, getent and cut.

My personal preference for improving Solution 2 (though not 
neccesarily better than 1) would be

Solution 2'
user=username		# Note that no quotes are required.
oldIFS=$IFS
IFS=:			# You are responsible for restoring this later
set -- $(getent passwd $user)	# Let bash parse out the fields
IFS=$oldIFS
f=$6



-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net



More information about the gnhlug-discuss mailing list