Trouble reading content of file from a variable

Hi ,

i have a parameter which has path of a file. Now i need to have another parameter with the content of that file. I tried the belwo script , can any one please help.
I dont want to use cat command to read. Can we do it with out using cat command.

 while read line
do
DML_FILE_NAME_ORIG1=`echo $line|awk 'BEGIN { FS = "|" } ; {print $1}'`
 
echo "DML_FILE_NAME_ORIG1=$DML_FILE_NAME_ORIG1"
 
DML_FILE_NAME_NEW_MODIFIED=${AI_DML}/testharness_modified.dml
cp $DML_FILE_NAME_ORIG1 ${AI_DML}/testharness_modified.dml
sed -i '/record/r ${AI_BIN}/modift_test_harness.txt' ${AI_DML}/testharness_modified.dml
 
echo "Contents of DML_FILE_NAME_NEW_MODIFIED"
echo `cat $DML_FILE_NAME_NEW_MODIFIED`
done<$AI_BIN/input.txt
> cat AI_BIN/input.txt
$AI_DML/a.dml
 
> cat $AI_DML/a.dml
include '~$STI_TOOLS_DML/data_quality.dml';
metadata type =
/* DML Generated for SQL: SELECT * FROM cdm_5.FT_ARNGE_CRED_RISK_MTGT_FACT
 * On: Thu Mar 29 06:40:06 2012
 */
record
  decimal("\x01") mth_rpt_perd_dim_id /*INTEGER NOT NULL*/;
  decimal("\x01") core_arnge_dim_id /*INTEGER NOT NULL*/;
end
 
> cat ${AI_BIN}/modift_test_harness.txt
string("\x01") key1 = NULL("");
string("\x01") key2 = NULL("");
 

The real question is, why do you want to dump the entire content of a file into a variable? Can you just use the file as-is?

I don't see where AI_DML gets set, but I presume it is an environmental variable.

You can remove the complex echo | awk to be just:-

DML_FILE_NAME_ORIG1="${line%%|*}"

.... or perhaps change the loop:-

OIFS="$IFS"
IFS="|"
while read DML_FILE_NAME_ORIG1 rest
do
   echo "DML_FILE_NAME_ORIG1=$DML_FILE_NAME_ORIG1"
   DML_FILE_NAME_NEW_MODIFIED=${AI_DML}/testharness_modified.dml
   cp $DML_FILE_NAME_ORIG1 ${AI_DML}/testharness_modified.dml
   sed -i '/record/r ${AI_BIN}/modift_test_harness.txt' ${AI_DML}/testharness_modified.dml
 
   echo "Contents of DML_FILE_NAME_NEW_MODIFIED"
   cat $DML_FILE_NAME_NEW_MODIFIED
  
done < $AI_BIN/input.txt
IFS="$OIFS"

You will notice I have removed the echo `cat and just left the cat

What are you actually trying to achieve here?

Robin

1 Like

Thanks for the reply....

I need to dump into variable only because in further steps i need to pass variable to ETL-graph and then it will execute using this.

Environmental issues are not there since i tried to run the script using hard coded value and it ran perfectly. Using variable only the problem comes.

cp command is not working ...... is there any other way around?

---------- Post updated at 12:13 AM ---------- Previous update was at 12:09 AM ----------

Hi Robin,

I just need the content of the file where the file path is in parameter to another parameter where it will be also having another file path with different name.

cp is not working is there any way around?

Phani

Not sure I understand your specification

Please describe in more detail and provide examples.

cp has proven to be working a zillion times. What is its behaviour in your case that displeases you? Run it with the -x option set and show us the execution log.

I mean

a.ksh
a=/ai/a.txt
b=/ai/b.txt
cp $a $b
echo `cat $b`
-----------------
cat /ai/a.txt
and
cat /ai/b.txt both should give the same result set.

This is wrong:

echo `cat $b`

The `cat $b` is going to flatten out all whitespace.

It's not the cp that does it, it's the ``.

It's completely redundant, anyway. Just cat $b would accomplish the same thing without the mangling.

It's this kind of thing which makes me suspect you don't need cat or cp at all, here, if we understood your requirements better.

1 Like

Please use code tags as required by forum rules!

From what you show here, cat /ai/a.txt and cat /ai/b.txt ARE giving the same result unless there has been an error msg indicating that cp failed.

1 Like

Hi when i tried

cp $a $b 

it is giving below error:

DML_FILE_NAME_ORIG1=$INF_COMRCLDATAMART_DML/ft_arnge_cred_risk_mtgt_fact.dml
cp: cannot stat `$INF_COMRCLDATAMART_DML/ft_arnge_cred_risk_mtgt_fact.dml': No such file or directory

but when i tried

cp $INF_COMRCLDATAMART_DML/ft_arnge_cred_risk_mtgt_fact.dml $b

it is working fine and the contents are being copied correctly.

So, are you saying that you have $a set as DML_FILE_NAME_ORIG1=$INF_COMRCLDATAMART_DML/ft_arnge_cred_risk_mtgt_fact.dml then?

This will not work. You are asking the shell to work with a file that isn't one. Which is the file you actually want to work with? I presume it's the one referred to by DML_FILE_NAME_ORIG1 , i.e. $INF_COMRCLDATAMART_DML/ft_arnge_cred_risk_mtgt_fact.dml

This far down the chain of variables, the $ will just be a character. You need to get that interpreted before you call the cp command.

I would suggest that your logic is over complicated. Break it down to find a simpler way to refer to the file you need. If not, you will end up with a mess of:-

  • I want to use cp $a $b
  • $a refers to DML.....
  • $DML.... refers to $INF.... and a file.

It's just too messy.

Are the records in $AI_BIN/input.txt also variable names or variable assignments that you want to use? Can you share the format of that file so we can see what are trying to process.

Try to slim the code down to the minimal structure and just have a one record input file to your loop, then after pasting it into the thread along with the content of $AI_BIN/input.txt , can you run the script with ksh -x yourscript to illustrate where it is going. It might be obvious to you then, but if not I'm sure we can discuss it further.

Robin