Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-14-2007
Registered User
 

Join Date: Dec 2007
Posts: 41
How can I parse a record found in /etc/passwd into variables?

I am working with the Oracle 10.2.0.3 job scheduler on Solaris 10, and unfortunately, the scheduler executes scripts in such a way that several default shell environment variables are not defined. For example, $HOME, $USER, and $LOGNAME are missing.

How can I parse the appropriate record in /etc/passwd into the above variables? For example, when the output from "grep oracle /etc/passwd" is:
oracle:x:100:1978::/usr/oracle:/bin/ksh
I need to parse the information so that I can manually export the following variables:
export HOME=/usr/oracle
export USER=oracle
export LOGNAME=oracle
I'm looking for something generic that will enable me to search for any record in /etc/passwd and parse it properly.

Thanks,

J
Sponsored Links
  #2 (permalink)  
Old 12-14-2007
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 759
Quote:
Originally Posted by shew01 View Post
I am working with the Oracle 10.2.0.3 job scheduler on Solaris 10, and unfortunately, the scheduler executes scripts in such a way that several default shell environment variables are not defined. For example, $HOME, $USER, and $LOGNAME are missing.

How can I parse the appropriate record in /etc/passwd into the above variables? For example, when the output from "grep oracle /etc/passwd" is:
oracle:x:100:1978::/usr/oracle:/bin/ksh
I need to parse the information so that I can manually export the following variables:
export HOME=/usr/oracle
export USER=oracle
export LOGNAME=oracle
I'm looking for something generic that will enable me to search for any record in /etc/passwd and parse it properly.

Thanks,

J

Code:

export HOME=$(nawk -F":" '/^oracle/{print $(NF-1)}' /etc/passwd)
export USER=$(nawk -F":" '/^oracle/{print $1}' /etc/passwd)
export LOGNAME=$(nawk -F":" '/^oracle/{print $1}' /etc/passwd)

  #3 (permalink)  
Old 12-14-2007
porter porter is offline Forum Advisor  
Registered User
 

Join Date: Jan 2007
Posts: 2,965
If you know the user name then the only thing you need to look up is the home directory, in ksh this can be evaluated with ~username, eg eval ~\$USER.
  #4 (permalink)  
Old 12-14-2007
Read Only
 

Join Date: Nov 2007
Posts: 165
This should work:


Code:

grep "^oracle:" /etc/passwd | read s
USER=`echo $s | cut -d: -f1`
LOGNAME=$USER
HOME=`echo $s | cut -d: -f6`

  #5 (permalink)  
Old 12-15-2007
Registered User
 

Join Date: Dec 2007
Posts: 41
Quote:
Originally Posted by porter View Post
If you know the user name then the only thing you need to look up is the home directory, in ksh this can be evaluated with ~username, eg eval ~\$USER.
That's the problem. When running under the Oracle Scheduler, the "normal" shell environment variables are not fully populated. Actually, only two shell variables are populated, and they are not among the list I mentioned above. I'm having to "backfill" what is missing.
  #6 (permalink)  
Old 12-15-2007
Registered User
 

Join Date: Dec 2007
Posts: 41
Quote:
Originally Posted by prowla View Post
This should work:


Code:

grep "^oracle:" /etc/passwd | read s
USER=`echo $s | cut -d: -f1`
LOGNAME=$USER
HOME=`echo $s | cut -d: -f6`

This looks interesting. I'll check it out.
  #7 (permalink)  
Old 12-17-2007
Registered User
 

Join Date: Dec 2007
Posts: 41
Quote:
Originally Posted by prowla View Post
This should work:


Code:

grep "^oracle:" /etc/passwd | read s
USER=`echo $s | cut -d: -f1`
LOGNAME=$USER
HOME=`echo $s | cut -d: -f6`

Thanks! This works great!
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to parse a string into variables aquimby Shell Programming and Scripting 9 06-12-2009 05:57 PM
How to: Parse text string into variables using Korn shell shew01 Shell Programming and Scripting 7 05-23-2008 11:01 AM
splitting a record and adding a record to a file rsolap Shell Programming and Scripting 1 08-13-2007 02:58 PM
How to parse config variables from external file to shell script pradsh Shell Programming and Scripting 2 07-09-2007 03:21 PM
How do I parse passwords into passwd command. gilberteu Shell Programming and Scripting 0 03-28-2006 12:26 PM



All times are GMT -4. The time now is 08:08 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0