List file with variables expanded


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List file with variables expanded
# 1  
Old 10-12-2010
List file with variables expanded

Hello,

I have this problem.

I have script file, e.g.

#!/usr/bin/ksh
echo $MY_DIR
ls -lt $MY_DIR

I want to list the script but with MY_DIR variable expanded. E.g.

MY_DIR=/abc/xyz (in shell MY_DIR is set)

So I want to list the script and see:

#!/usr/bin/ksh
echo /abc/xyz
ls -lt /abc/xyz

I tried both:
a) while read line; do echo $line; done < script.sh
b) awk '{print $0}' script.sh

But both just list the script without substitutin $MY_DIR with its real value.

How to do this?
# 2  
Old 10-12-2010
if you give the absolute path of MY_DIR inside the script and then echo.... it should work fine....
# 3  
Old 10-12-2010
Try:
Code:
while IFS='' read -r line ;do eval echo "$line"; done < script.sh

# 4  
Old 10-12-2010
I tried eval. The problem is that scrip I'm listing is sql and there are things like: "/*" "(c)" which gives:

=> if line of script starts with /* (begin of sql comment) it gives list of directories in root
=> fi line of script starts with * it gives list of directories in current directory
=> for copyright marks like "(c)" error is given and while loop breaks.

can errors be suppressed somehow in eval?
# 5  
Old 10-12-2010
Quote:
Originally Posted by r1omen
Hello,

I have this problem.

I have script file, e.g.


MY_DIR=/abc/xyz (in shell MY_DIR is set)

How to do this?

use export command as below.

Code:
export MY_DIR=/abc/xyz  #(in shell MY_DIR is set)

then do the script normally as you write it in before.
# 6  
Old 10-12-2010
How does this work out then:
Code:
while IFS= read -r line ;do eval printf '"%s\n"' "\"$line\""; done < script.sh


Last edited by Scrutinizer; 10-12-2010 at 08:47 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 10-12-2010
Works!

I don't know what this printf does but will soon find out.

Thank You Scrutinizer!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove last '1' in list of variables

Hi folks, I have a list of variables as follows: CDBTEST1 messdba1 sat11cru1 s12tgts1 sa12ss1 I need to remove the last '1' so I can use the remaining variables in a for loop: CDBTEST messdba sat11cru s12tgts sa12ss Something like this: (3 Replies)
Discussion started by: jonnyd
3 Replies

2. UNIX for Beginners Questions & Answers

Wild char getting expanded in echo

Hi All, I am having a issue in a script. I am trying to execute a select * from a scirpt to a database and printing the the sql string. The * in the sqltring is printing all the files in the directory. How to handle it ? .. .. sql="select * from emp" execute ($sql) echo $sql Here my... (6 Replies)
Discussion started by: arunkumar_mca
6 Replies

3. UNIX for Beginners Questions & Answers

How the home alias (~) is it expanded?

echo $HOME return : /home/user1echo ~ return : /home/user1My_path=~/bin/"some dir1/some dir2" kate "$My_path/some_file"Kate open a file "~/~/bin/some dir1/some dir2/some_file" which does not exists. I was expecting kate to open : "~/bin/some dir1/some dir2/some_file" Any help is welcome. ... (8 Replies)
Discussion started by: jcdole
8 Replies

4. Solaris

zpool hasn't expanded

Hi Guys, I have a raidz zpool that consists of four disks. 2x2TB, 1x1TB and 1x0.75TB. Originally it was only 1x1TB, 3x0.75TB, and I had around 1.7TB of storage capacity. I've just switched out two of the 0.75TB disks for the 2x2TB ones. I did this one at a time and now the resilvering is... (2 Replies)
Discussion started by: rudigarude
2 Replies

5. Red Hat

List of all environment variables?

Hey all, I am simply trying to find a listing of all of the default BASH environment variables in RHEL 5.4. Namely, I need to find what the path variable is for libraries since one of my applications doesn't see a module that it needs to run. So far I've seen $LD_PRELOAD, $LD_LIBRARY_PATH and... (4 Replies)
Discussion started by: msarro
4 Replies

6. Solaris

war file not expanded (tomcat solaris)

hi .. my example.war file is not expanded in the webapps folder.. could u guide me why the file is not expanded when i restart tomcat... (1 Reply)
Discussion started by: senkerth
1 Replies

7. UNIX for Advanced & Expert Users

Redirect contained in variable not expanded as expected

Hi all, I have a selection of files that contain several commands that write to a file and are started as background processes similar to below. command_to_run_simulator -simulator_arguments > log_file 2>&1 & command_to_run_simulator -simulator_arguments > log_file 2>&1 &... (2 Replies)
Discussion started by: noose
2 Replies

8. Shell Programming and Scripting

list file content as individual variables

Hello, I've created a couple of files within a list using the command "ls -ltr | tail -2 > list" These files are the newest files placed within a directory. From the "list" file, I need to place the filenames as a variable. In which the newest file will be called "new_ctrl" the older file... (4 Replies)
Discussion started by: petersf
4 Replies

9. Programming

Macros expanded output of c files

Hi, I want to just expand the macros in a set of c files from a filelist which are in different directories. I don't want the header files included in the c file to be expanded. I have a perl script which does gcc -E <infile> -I <path to search for header files> -imacros <infile> But in... (1 Reply)
Discussion started by: spsenthil
1 Replies
Login or Register to Ask a Question