Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to pass the output of an awk command as a variable in a bash script, but not having much success so any help is appreciated.

homedirectory=$(awk /stephen/ /etc/passwd | awk -F/home/ '{print $2}' | awk -F/downloads '{print $1}')

echo '$homedirectory'

I want to extract the home directory of user stephen from the /etc/passwd, so the result I want is 0001

/etc/passwd

statd:x:102:65534::/var/lib/nfs:/bin/false

bind:x:103:106::/var/cache/bind:/bin/false

sshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin

mysql:x:105:107:MySQL Server,,,:/var/lib/mysql:/bin/false

messagebus:x:106:108::/var/run/dbus:/bin/false

stephen:x:1039:1031::/home/0001/downloads:/bin/false
share|improve this question
2  
Do you really need 3 separate awk invocations there? What exactly are you trying to do? It sure looks like you are trying to make things far more complicated then it needs to be. –  Zoredache Apr 23 '12 at 20:13
3  
You'd be better off explaining what you're trying to actually do. –  MikeyB Apr 23 '12 at 20:13

3 Answers 3

up vote 3 down vote accepted

Don't look at /etc/passwd. echo ~username, in your case echo ~stephen will do what you want.

From your /etc/passwd result would be

$ echo ~stephen
/home/0001
$ basename `echo ~stephen`
0001

Edit: This was before the edit applied proper formatting to your /etc/passwd contents. I didn't catch the homedir was /home/0001/downloads. What you're asking for is something that will not always have a consistent result unless you always expect to have the inspected user's homedir be in /home. There are a million ways to do this, most which will be somewhat wrong, until you provide information on your expected constraints.

In any case, apply your filtering to the output of echo ~username. You could use a variety of utilities after that, like basename, awk, sed, cut, etc.

share|improve this answer
1  
+1, this is a far more appropriate approach and it should work on all POSIX systems, including Macs (which use etc/passwd only in single-user mode). –  Skyhawk Apr 23 '12 at 20:30
    
The result of basename echo ~stephen is downloads. I could use this I suppose echo ~stephen | awk -F/home/ '{print $2}' | awk -F/downloads '{print $1}' but I am still stuck with how to use the output as a variable. Actually that works, thanks –  Stephen Apr 23 '12 at 20:35
    
Actually, using backticks is pretty 1995 at this point. I'd suggest trying the $(/usr/bin/foo argv) syntax first, as it's more readable. –  Magellan Apr 23 '12 at 23:56

Your three awks worked (although as others have said, it could have been done a lot better).

The final problem was that echo '$homedirectory' should have been echo "$homedirectory" since the shell looks inside double quotes but not single quotes.

share|improve this answer

Scrap what you have and use:

getent passwd stephen | cut -d: -f6

Or even

cd ~stephen && pwd
share|improve this answer
    
Not every Unix system has the getent command. It would make more sense to use what is universally available. –  Skyhawk Apr 23 '12 at 20:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.