variable lookup problem in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable lookup problem in shell script
# 1  
Old 06-02-2009
variable lookup problem in shell script

Hi
I have one properties file containing as

Code:
$INSTALL_BASEPATH/mssages/commonmessages_default.properties
$INSTALL_BASEPATH/resource/configurationBundle.properties

and $INSTALL_BASEPATH is set in .bash_profile

but from shell script when I read this file
and use in copy statement then it is not working

cp ${URL[j]} $distributionName/$dirstructure

where ${URL[j]}-------->file path read from properties file $distributionName/$dirstructure--------->destination directory


error---------->cp: cannot stat $INSTALL_BASEPATH/mssages/commonmessages_default.properties: No such file or directory


I know here possibly $INSTALL_BASEPATH value is not replace but how I can do lookup this value in shell script..........



Thanks in advance

Last edited by vgersh99; 06-02-2009 at 12:05 PM.. Reason: do not 'SUPER size' your posts - use 'code tags' appropriately.
# 2  
Old 06-02-2009
Quote:
Originally Posted by mnmonu
I have one properties file containing as

$INSTALL_BASEPATH/mssages/commonmessages_default.properties
$INSTALL_BASEPATH/resource/configurationBundle.properties

and $INSTALL_BASEPATH is set in .bash_profile
but from shell script when I read this file
and use in copy statement then it is not working

Have you exported the variable? If not, child processes will not see it.

In .bash_profile:

Code:
export INSTALL_BASEPATH

[QUOTE]
# 3  
Old 06-02-2009
[quote=cfajohnson;302321899]

Have you exported the variable? If not, child processes will not see it.

In .bash_profile:

Code:
export INSTALL_BASEPATH

Hi cfajohnson

Yes I have already entried in .bash_profile as
export INSTALL_BASEPATH=/vol2/RAORV_HOME

and I check it if I print the value from shell script it works well in cp command it's not working



This is my shell script
When I run it it gives the error in cp ${URL[j]} $distributionName/$dirstructure line




Code:
tempURL=`cat PropertiesFileURL_unix.txt|tr '\n' ' '|tr -d '\r'`
urlCount=(`echo "$tempURL" | sed 's/[^ ]//g' | wc -c` ) 
urlCount=`expr $urlCount - 1`
echo "urlCount------------->"$urlCount
URL=( $tempURL )
distributionName=`echo $INSTALL_BASEPATH|awk -F "/" '{print $NF}'`
ratingdirName=`echo $INSTALL_RATPATH|awk -F "/" '{print $NF}'`

for(( j =0; j< $urlCount ; j++ ))
do
    echo -e "{URL[$j]}------------->"${URL[j]}
    serverName=`echo ${URL[$j]}|awk -F "/" '{print $2}'|tr -d ' ' `
    echo "serverName---------------->"$serverName
    
    if [ $serverName == "abc-5.0" ] 
    then
    dirstructure=`echo ${URL[$j]} | awk -F"/" 'BEGIN{ OFS="/" } {$1="";$NF=""; print $0 }'`
    `mkdir -p $distributionName$dirstructure`
    echo ${URL[j]}
    cp ${URL[j]} $distributionName/$dirstructure
    
    elif [ $serverName == "abcra-5.0" ] 
    then
    dirstructure=`echo ${URL[$j]} | awk -F"/" 'BEGIN{ OFS="/" } {$1="";$NF=""; print $0 }'`
    `mkdir -p $distributionName/$dirstructure`
    
    elif [ $serverName == "abctl-5.0" ]
    then
    dirstructure=`echo ${URL[$j]} | awk -F"/" 'BEGIN{ OFS="/" } {$1="";$NF=""; print $0 }'`
    `mkdir -p $distributionName/$dirstructure`
    elif [ $serverName == "abcbill-5.0" ]
    then
    dirstructure=`echo ${URL[$j]} | awk -F"/" 'BEGIN{ OFS="/" } {$1="";$NF=""; print $0 }'`
    `mkdir -p $ratingdirName/$dirstructure`
    else
    echo "Unknown"
    fi
done



Please help me.......................






Last edited by vgersh99; 06-02-2009 at 12:41 PM.. Reason: code tags PLEASE!
# 4  
Old 06-02-2009
mnmonu,

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 5  
Old 06-02-2009
Quote:
Originally Posted by mnmonu
Quote:
Originally Posted by cfajohnson

Have you exported the variable? If not, child processes will not see it.

In .bash_profile:

Code:
export INSTALL_BASEPATH

Yes I have already entried in .bash_profile as
export INSTALL_BASEPATH=/vol2/RAORV_HOME

and I check it if I print the value from shell script it works well in cp command it's not working

When you post code, please wrap it in [code] tags.
Quote:
Code:
tempURL=`cat PropertiesFileURL_unix.txt|tr '\n' ' '|tr -d '\r'`
urlCount=(`echo "$tempURL" | sed 's/[^ ]//g' | wc -c` ) 
urlCount=`expr $urlCount - 1`


You have six external commands in three lines. You need no more than 2. (You never need expr; the shell can do integer arithmetic.)

It's not clear exactly what you are counting, so it's hard to recommend any code.
Quote:
Code:
echo "urlCount------------->"$urlCount
URL=( $tempURL )


Ah!

I think everything up to this point can be replaced with:

Code:
oIFS=$IFS
IFS='
'
URL=( $(tr -d '\r' < PropertiesFileURL_unix.txt) )
IFS=$oOFS
urlCount=${#URL[@]}

See? One external command!
Quote:
Code:
distributionName=`echo $INSTALL_BASEPATH|awk -F "/" '{print $NF}'`
ratingdirName=`echo $INSTALL_RATPATH|awk -F "/" '{print $NF}'`


You don't need awk:

Code:
distributionName=${INSTALL_BASEPATH##*/}
ratingdirName=${INSTALL_RATPATH##*/}

Quote:
Code:
for(( j =0; j< $urlCount ; j++ ))
do
    echo -e "{URL[$j]}------------->"${URL[j]}
    serverName=`echo ${URL[$j]}|awk -F "/" '{print $2}'|tr -d ' ' `
    echo "serverName---------------->"$serverName
    
    if [ $serverName == "abc-5.0" ] 
    then
    dirstructure=`echo ${URL[$j]} | awk -F"/" 'BEGIN{ OFS="/" } {$1="";$NF=""; print $0 }'`


You don't need awk:

Code:
dirstructure=${URL[$j]#*/}
dirstructure=/${dirstructure%/*}/

Quote:
Code:
    `mkdir -p $distributionName$dirstructure`


Why do you have that in backticks?

And are you sure that $distributionName ends with a slash?
Quote:
Code:
    echo ${URL[j]}
    cp ${URL[j]} $distributionName/$dirstructure


Does $distributionName/$dirstructure exist?
Quote:
Code:
    
    elif [ $serverName == "abcra-5.0" ] 
    then
    dirstructure=`echo ${URL[$j]} | awk -F"/" 'BEGIN{ OFS="/" } {$1="";$NF=""; print $0 }'`
    `mkdir -p $distributionName/$dirstructure`
    
    elif [ $serverName == "abctl-5.0" ]
    then
    dirstructure=`echo ${URL[$j]} | awk -F"/" 'BEGIN{ OFS="/" } {$1="";$NF=""; print $0 }'`
    `mkdir -p $distributionName/$dirstructure`
    elif [ $serverName == "abcbill-5.0" ]
    then
    dirstructure=`echo ${URL[$j]} | awk -F"/" 'BEGIN{ OFS="/" } {$1="";$NF=""; print $0 }'`
    `mkdir -p $ratingdirName/$dirstructure`
    else
    echo "Unknown"
    fi
done

# 6  
Old 06-02-2009
Please help

Hi

For testing purpose I have written another simple script till I am facing the problem Please help me


HTML Code:
Now my parameter file test.txt contain
$INSTALL_BASEPATH/uninstall.sh


Code:
and my test script contain following (test.sh)
dir=`cat test.txt|tr -d '\n'`
echo "dir============"$dir
mkdir -p resources
cp $dir resources




I have checked well that uninstall.sh is exist in path $INSTALL_BASEPATH

and when I run the following
HTML Code:
[raorv@qcldb2 test]$ ./test.sh 
dir============$INSTALL_BASEPATH/uninstall.sh
cp: cannot stat `$INSTALL_BASEPATH/uninstall.sh': No such file or directory
but if I do cat $INSTALL_BASEPATH/uninstall.sh it shows the actual file content from shell script also


So where I am doing the wrong.$INSTALL_BASEPATH path already exported in .bash_profile


Plz help
# 7  
Old 06-02-2009
Quote:
Originally Posted by mnmonu
Hi

For testing purpose I have written another simple script till I am facing the problem Please help me

This is not HTML; why are you using HTML tags?
Quote:
HTML Code:
Now my parameter file test.txt contain
$INSTALL_BASEPATH/uninstall.sh
Code:
and my test script contain following (test.sh)


The previous line is not code; why is it inside code tags?
Quote:
Code:
dir=`cat test.txt|tr -d '\n'`


Why are you using cat? It's an unnecessary external command.

Code:
dir=$( tr -d '\n' < test.txt )

But, since there's only one line in the file, you don't need any external command:

Code:
read dir < test.txt

Why are you calling the variable dir when the file contains a filename, not a directory?
Quote:
Code:
echo "dir============"$dir
mkdir -p resources


You should check that mkdir was successful.

Code:
if ! mkdir resources
then
  printf "could not create directory %s\n" "resources" >$2
  exit 1
fi

Quote:
cp $dir resources[/CODE]


I have checked well that uninstall.sh is exist in path $INSTALL_BASEPATH

You should check it in the script.

Code:
if ! [ -e "$dir" ]
then
  printf "%s\n" "$dir does not exist" >&2
  exit 1
fi

Quote:
and when I run the following
HTML Code:
[raorv@qcldb2 test]$ ./test.sh 
dir============$INSTALL_BASEPATH/uninstall.sh
cp: cannot stat `$INSTALL_BASEPATH/uninstall.sh': No such file or directory
but if I do cat $INSTALL_BASEPATH/uninstall.sh it shows the actual file content from shell script also


So where I am doing the wrong.$INSTALL_BASEPATH path already exported in .bash_profile

In this script, your problem is that $dir will contain a literal '$INSTALL_BASEPATH/uninstall.sh'. The variable is not expanded.

Fix that with:

Code:
eval "dir=$dir"

You probably had the same problem in the original script, but you didn't tell us what was in the file, so we couldn't help you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

Help with Shell Script: User Lookup

Hi everyone, Let me start by stating this question is for homework help (not "help, my boss needs this ASAP") I have spent the last few days re-visiting this script, and cannot figure out where I am going wrong (something simple I'm sure). I am to build a script that searches for a user... (1 Reply)
Discussion started by: jjc032681
1 Replies

3. Shell Programming and Scripting

Problem with lookup in awk

I need to add new ID to the file with old ID (column 7), I collected old ID / new ID pairs in a lookup file and I am trying to use awk to do the job, but something is not clicking. My input file ABC| 107|1440589221| -118.117167| 33.986333|10| 497476|1 ABC| 125|1440591215| -118.181000| ... (4 Replies)
Discussion started by: migurus
4 Replies

4. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

5. Shell Programming and Scripting

Problem getting the content of a file in a shell script variable

Hi, I have a text file that has a long multi-line db2 CTE query. Now I want to store all the contents of this file (i.e. the entire query) in a shell script variable. I am trying to achieve it by this: query = `cat /Folder/SomeFile.txt` But when I echo the contents of this file by saying echo... (4 Replies)
Discussion started by: DushyantG
4 Replies

6. Shell Programming and Scripting

Sed variable from lookup table

I have a file with the following format --TABLEA_START-- field1=data1;field2=data2;field3=data3 --TABLEA_END-- --TABLEB_START-- field1=data1;field2=data2;field3=data3 --TABLEB_END-- --TABLEA_START-- field1=data1;field2=data2;field3=data3 ... (0 Replies)
Discussion started by: milo7
0 Replies

7. Shell Programming and Scripting

have a problem in using a variable in kourne shell script

when I write the kourne shell script on a solaris machaine like this: #!/bin/ksh myps =50 echo $myps and when I run this script with typing "myfile" , it shows this message: myfile: myps: not found I think I write a correct syntax but why a program show error like that. (3 Replies)
Discussion started by: thsecmaniac
3 Replies

8. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

9. UNIX for Dummies Questions & Answers

Problem in incrementin a variable in UNIX shell script

I have a simple query If we have BATCH= 081675 and incremnting it by one as BATCH=`expr ${BATCH} + 000001`; We can't get BATCH = 081676 but gets BATCH = 81676 Can anyone tell why i am getting this value and not the rquired one and how i could get the required one? (1 Reply)
Discussion started by: communicator
1 Replies

10. Shell Programming and Scripting

problem in getting the path of environment variable set in bashrc in my shell script

hi all i have joined new to the group. i have set an variable in my bashrc file. .bashrc PROGHOME=/home/braf/braf/prog export PROGHOME but while using it in my shell script its path is not taken and i had to explicitly give the export command to set the path. in my script... (8 Replies)
Discussion started by: krithika
8 Replies
Login or Register to Ask a Question