Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-08-2012
Registered User
 

Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Resolve Environment Variable

I am tyring to resolve an environment variable that is part of a string I selected from our database.
Simply put, I want cd to this folder before checking if a file exists.

The variable $in_loc has the value '$PS_HOME/int/VSP' where $PS_HOME is the environment variable.

I am using cd $in_loc in my ksh script, however it doesn't work. I get the error './getfile.sh[29]: $PS_HOME/int/VSP: not found'.

can anyone help me out. I have tried several things including readlink, realpath, and various forms of the cd command, but can't seem to get it to work...
Sponsored Links
    #2  
Old 02-08-2012
ahamed101's Avatar
root is god!!!
 

Join Date: Sep 2008
Location: India
Posts: 1,478
Thanks: 33
Thanked 382 Times in 377 Posts
Can you paste the code snippet around this?... exact code snippet...

--ahamed
Sponsored Links
    #3  
Old 02-08-2012
Registered User
 

Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Resolve Environment Variable

Here is my code...


Code:
#!/bin/ksh
echo "------------------------------"
echo "Scripting ls command..."
echo "------------------------------"
 
SQLPLUS_CMD=$ORACLE_HOME/bin/sqlplus
login="userid/pwd@dev"
 
in_loc=`$SQLPLUS_CMD -s $login <<EOF 
set pagesize 0 feedback off verify off heading off echo off
select file_location from ps_is_ftp_rc where prcsname = 'HR1062' and seqnum = 1;
exit;
EOF`
 
filepath="cd "$in_loc
 
echo "filepath = $filepath"
echo "in_loc value: $in_loc"
 
cd $in_loc
 
echo `pwd`
 
my_file="testfile"
 
fname=`ls -t $my_file | head -1`
 
echo " "
echo "last edited filename: $fname"


Last edited by Franklin52; 02-09-2012 at 03:06 AM.. Reason: Please use code tags for code and data samples, thank you
    #4  
Old 02-08-2012
ahamed101's Avatar
root is god!!!
 

Join Date: Sep 2008
Location: India
Posts: 1,478
Thanks: 33
Thanked 382 Times in 377 Posts
So the data retrieved from the DB is "$PS_HOME/int/VSP"? Is $PS_HOME defined anywhere?

Please paste the output of bash -x /your/script

--ahamed
Sponsored Links
    #5  
Old 02-08-2012
Registered User
 

Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
The results are listed below, however I removed sensitive info. $PS_HOME is defined and I can run the command 'cd $PS_HOME/int/VSP' at the command line and get the diesired results. It just doesn't work in my script...



Code:
+ echo ------------------------------
------------------------------
+ echo 'Scripting ls command...'
Scripting ls command...
+ echo ------------------------------
------------------------------
+ SQLPLUS_CMD=<oracle_home>/bin/sqlplus
+ login=<userid>/<pwd>@<db>
++ <oracle_home>/bin/sqlplus -s <userid>/<pwd>@db
+ in_loc='$PS_HOME/int/VSP'
+ filepath='cd $PS_HOME/int/VSP'
+ echo 'filepath = cd $PS_HOME/int/VSP'
filepath = cd $PS_HOME/int/VSP
+ echo 'in_loc value: $PS_HOME/int/VSP'
in_loc value: $PS_HOME/int/VSP
+ cd '$PS_HOME/int/VSP'
getfile.sh: line 25: cd: $PS_HOME/int/VSP: No such file or directory
++ pwd
+ echo <pwd> 
++ cd '$PS_HOME/int/VSP'
getfile.sh: line 29: cd: $PS_HOME/int/VSP: No such file or directory
+ echo
++ pwd
+ echo <pwd>

+ my_file=testfile
++ ls -t testfile
++ head -1
testfile: No such file or directory
+ fname=
+ echo ' '
+ echo 'last edited filename: '
last edited filename:
+ echo ' '
+ echo 'Found string at position:  '
Found string at position:


Last edited by Franklin52; 02-09-2012 at 03:07 AM.. Reason: code tags
Sponsored Links
    #6  
Old 02-08-2012
ahamed101's Avatar
root is god!!!
 

Join Date: Sep 2008
Location: India
Posts: 1,478
Thanks: 33
Thanked 382 Times in 377 Posts
Try this change in your script...

Code:
actual_dir=$( eval echo $in_loc )
cd $actual_dir

--ahamed
Sponsored Links
    #7  
Old 02-08-2012
Registered User
 

Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
That works... Thank you very much...
Sponsored Links
Reply

Tags
environment variables, ksh

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Expand an environment variable in sed, when the variable contains a slash Ilja Shell Programming and Scripting 2 11-10-2010 09:58 AM
Add Environment variable in C/C++ Kattoor Programming 2 04-30-2009 06:00 AM
set environment variable? smartgupta Solaris 8 09-15-2008 01:01 PM
Resolve a Variable serm Shell Programming and Scripting 2 05-12-2005 12:27 AM
Environment Variable thumsup9 UNIX for Dummies Questions & Answers 2 02-03-2005 06:45 PM



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