Expansion does not work

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expansion does not work
# 1  
Old 10-11-2013
Expansion does not work

I'm trying to figure out whether some files exist. Locations of those file are stored in a plain text file called temp.txt in this way:
All environment variables ($LIB_HOME and $ORACLE_HOME) have been set using export command.
Then I do:
while read line; do [[ -f $line ]] && echo "OK ==> $line" || echo "KO ==> $line"; done < temp.txt

Output is:
KO ==> $LIB_HOME/opensource/project/mpxj.jar
KO ==> $LIB_HOME/opensource/project/jaxb-api.jar
KO ==> $LIB_HOME/opensource/project/jaxb-impl.jar
KO ==> $LIB_HOME/opensource/project/jsr173_1.0_api.jar
KO ==> $LIB_HOME/infraestructura/stdinc/STDIncidencias.jar
KO ==> $LIB_HOME/opensource/log4j/log4j-1.2.8.jar
KO ==> $LIB_HOME/infraestructura/reu/reu.jar
KO ==> $LIB_HOME/opensource/commons/commons_beanutils.jar
KO ==> $LIB_HOME/opensource/commons/commons_codec.jar
KO ==> $LIB_HOME/opensource/commons/commons_collections.jar
KO ==> $LIB_HOME/opensource/commons/commons_logging.jar
KO ==> $LIB_HOME/infraestructura/log4sm/log4sm-2.0.jar
KO ==> $ORACLE_HOME/jdbc/lib/ojdbc5.jar
OK ==> /apps/datos/intranet/lib61/was/com.ibm.ws.runtime_6.1.0.jar
KO ==> $LIB_HOME/was/j2ee.jar
Nevertheless, if I type from command line
[coge5ast02].Ataxtxc1:/shared/apps/taxtxc > [[ -f $LIB_HOME/opensource/project/mpxj.jar ]] && echo "OK" || echo "KO"

I get OK, as expected.
I don't understand why expansion of $LIB_HOME does not work when I read from file.

By the way, OS is Red Hat Enterprise Linux Server release 5.7 (x86_64), under bash shell.

Thanks a lot.
# 2  
Old 10-11-2013
while read line; do [[ -f $(echo $line) ]] ....


Last edited by sea; 10-11-2013 at 07:20 AM..
# 3  
Old 10-11-2013
[[ -f "$line" ]]

# 4  
Old 10-11-2013
Thanks both.
I already tried as you both say and it doesn't work, either. I also tried using single brackets, and nothing. It looks like it is unable to substitute $LIB_HOME by its content.
[coge5ast02].Ataxtxc1:/shared/apps/taxtxc > env | grep LIB_HOME
Even more. If I type 'set -x' to see what is really evaluated, I get:
[coge5ast02].Ataxtxc1:/shared/apps/taxtxc > while read line; do [[ -f $(echo $line) ]] && echo "OK ==> $line" || echo "KO ==> $line"; done < temp.txt
+ read line
++ echo '$LIB_HOME/opensource/project/mpxj.jar'
+ [[ -f $LIB_HOME/opensource/project/mpxj.jar ]]
+ echo 'KO ==> $LIB_HOME/opensource/project/mpxj.jar'
KO ==> $LIB_HOME/opensource/project/mpxj.jar
Using double quotes I get same output:
[coge5ast02].Ataxtxc1:/shared/apps/taxtxc > while read line; do [[ -f "$line" ]] && echo "OK ==> $line" || echo "KO ==> $line"; done < temp.txt
+ read line
+ [[ -f $LIB_HOME/opensource/project/mpxj.jar ]]
+ echo 'KO ==> $LIB_HOME/opensource/project/mpxj.jar'
KO ==> $LIB_HOME/opensource/project/mpxj.jar
# 5  
Old 10-11-2013
Please issue an :
echo $LIB_HOME

as well as an :
env | grep LIB_HOME

before running the while statement (this to make sure that the $LIB_HOME variable have a relevant value and exists in your environment
[[ -f $(eval echo $line) ]]

Last edited by ctsgnb; 10-11-2013 at 08:16 AM..
This User Gave Thanks to ctsgnb For This Post:
# 6  
Old 10-11-2013
It worked!! Thanks a lot.

Can you tell me why 'eval' is necessary and my command did not work without it? I don't understand why.

Anyway, I really appreciate your answer. It saves me a lot of work to replace each variable manually.

# 7  
Old 10-11-2013
eval force the evaluation of the variable $line before echo process it
so :
eval echo $line

becomes :
echo $LIB_HOME/opensource/whatever

so that the $LIB_HOME is evaluated by the echo command

Otherwise , $line is evaluated only once so the $LIB_HOME remains as litteral and is never evaluated.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use parameter expansion over a parameter expansion in bash.

Hello All, Could you please do help me here as I would like to perform parameter expansion in shell over a parameter expansion. Let's say I have following variable. path="/var/talend/nat/cdc" Now to get only nat I could do following. path1="${path%/*}" path1="${path1##*/}" Here... (8 Replies)
Discussion started by: RavinderSingh13
8 Replies

2. Shell Programming and Scripting

Tilde expansion

(Using Bash 4.4) When I write something like dir="~/dox" ls $dir then I get the message that the directory '~/docs' does not exist. I understand that the tilde is not expanded at the time of the above assignment because of the quotes. But why is it not expanded at the time when the ls command is... (2 Replies)
Discussion started by: Ralph
2 Replies

3. Shell Programming and Scripting

Bash expansion

Hello. I cannot write a command without using eval. Any help is welcome Note 1 : What does the function SOMETHING has no importance. Note 2 : What does the command find has no importance. It is an expansion variable problem : where to put or or or anythings else What works (FILTRE_1... (8 Replies)
Discussion started by: jcdole
8 Replies

4. IP Networking

Discussion at work, would a router work pluging a cable in wan1 and lan1?

hi all. and sorry for the random question, but this sparkled a raging flame-war at work and i want more points of view situation a router, with linux of some sort, dhcp client requesting for ip in wan1 (as usual with wan ports) dhcp server listening in lan1, and assigning ip (as usual... (9 Replies)
Discussion started by: broli
9 Replies

5. UNIX for Dummies Questions & Answers

Expansion within cp

I have a bunch of files which I need to transfer to another location... and some of these I need to skip. For e.g. let us say the files are: cust_abc.dat cust_xyz.dat cust_def.dat and I only want to move the first two. I want to do something like: cp cust_.dat <target> ... (1 Reply)
Discussion started by: jawsnnn
1 Replies

6. Shell Programming and Scripting

Need help with parameter expansion

Say you have this numeric variable that can be set by the user but you never want it to leave a certain range when it gets printed. How could you use parameter expansion such that it will never expand outside of that boundary? Thanks ---------- Post updated at 11:09 PM ---------- Previous update... (3 Replies)
Discussion started by: stevenswj
3 Replies

7. Shell Programming and Scripting

Need to print the expansion of the found string (the expansion is beween two delimiters '-' , '||'

Hi , could anyone help me out with this problem. sample.txt has this content : u001- this is used for project1 || u002- this is used for p2|| not to be printed u003- this is used for project3 || u004- this is used for p4 || u005- this is used for project5 || u006- this is used for p6... (9 Replies)
Discussion started by: Balaji PK
9 Replies

8. UNIX for Dummies Questions & Answers

sudo and expansion

Hi there, Can anyone explain me the following behavior? hfserver:~# ls -l /home/cronlogs/mysqldump* -rw-r--r-- 1 root root 10658464 2009-01-18 03:00 /home/cronlogs/mysqldump_20090118030002 -rw-r--r-- 1 root root 10651035 2009-01-19 03:00 /home/cronlogs/mysqldump_20090119030001 -rw-r--r-- 1... (4 Replies)
Discussion started by: chebarbudo
4 Replies

9. Shell Programming and Scripting

~ expansion in printf

Hi, I have a script that at one point prints to a file as follows: printf -- $2 > ~/.mydir/$1 The idea is to print to a hidden directory .mydir in my home directory. I've already sanitized the inputs and $1 is in the format path1/path2/filename and $2 is some user input. When I run this... (2 Replies)
Discussion started by: Rledley
2 Replies

10. AIX

AIX Expansion CD

Hey Anyone have a copy of the Expansion CD for the old 5.1 Version of AIX Thanks (0 Replies)
Discussion started by: almuwatta
0 Replies
Login or Register to Ask a Question