I have been trying to acheive the following task for a while now, but failed.. Need help, experts please help!
This is what I am trying to do:
- I am writing to a flat file the name of the source to be copied and the destination path as to where it is to be copied to.
Sample flat file:
Abc.scr $DESTN_PATH/test
Def.scr $DESTN_PATH/new
- I am reading the source name from this file from a shell script and replacing in the cp command. For example in the file above filename=abc.scr destn= $DESTN_PATH/test
So in shell script I am executing: cp $filename $destn
The issue I am facing here is that in the cp command the value of the environment variable $DESTN_PATH is not replaced, hence the file is copied correctly.
there can be many scenarios where something is going wrong
export the DESTN_PATH variable. (it shouldn't contain / at last of the path, otherwise you will two // in your output expression)
make sure the assignment doesn't contain any space on both side of '='
- I am writing to a flat file the name of the source to be copied and the destination path as to where it is to be copied to.
Sample flat file:
Abc.scr $DESTN_PATH/test
Def.scr $DESTN_PATH/new
- I am reading the source name from this file from a shell script and replacing in the cp command. For example in the file above filename=abc.scr destn= $DESTN_PATH/test
So in shell script I am executing: cp $filename $destn
The issue I am facing here is that in the cp command the value of the environment variable $DESTN_PATH is not replaced, hence the file is copied correctly.
First off, if this:
is what you really have in your script then it will not work because of the superfluous space between "=" and "$". If it was just a copy error consider this point to be moot.
Without regard to this, if i have understood you correctly:
You have a file like this:
and you read it something like this:
and you expect the "$DEST" to be expanded by the shell? Well, this won't work, because of the evaluation process of the shell. When it evaluates "$TGT" all the variable evaluation is done and to further evaluate the resulting "$DEST/..." would require the variable substitution process to restart. I have described this in detail in this thread.
The solution is the same as in the other thread and for the same reasons: use eval.
Still, this is a unsatisfying solution, because it is inherently problematic. You might want to create a more robust solution like this:
The first non-comment line of your input file shall be of the form "DEST=/some/path". Multiple lines of this form are allowed and change the value of "$DEST" until the next occurrence of such a line. This is parsed by your script and used to set the value of DEST:
Sample of input file:
The expected output from such an input would be like this:
Here is a sketch of such a script (replace "<spc>" and "<tab>" with the respective literal characters):
Bakunin, thank you for a great answer, but this solution isnt something I want to use, the reason being that this would necessitate to know all the environment variable names that are going to be used. Any new variable added would require an update to the main script..
I will try out this option at work tomorrow, but what would really help me is something which would replace any environment variable with its value without the check and replace in the script.
the reason being that this would necessitate to know all the environment variable names that are going to be used. Any new variable added would require an update to the main script..
To be honest i don't quite get it. Actually this is the case in your proposed solution, not in mine: just because i denominate it "DEST" in the file as well as in the variables name doesn't mean they have to be equal: try it out and rename the variable "DEST" throughout the script and you will see that it still works as expected.
Quote:
but what would really help me is something which would replace any environment variable with its value without the check and replace in the script.
I have already told you how this works: use "eval" like i explained at length in the link i gave. But be warned: this works ONLY, if the variable you use in the configuration file is predefined in the script with the exact name to match. In fact this behavior - every change to the environment will make a script change necessary - is true for your solution, because you define the value to which the variable should expand only in the script, not in the configuration file. As you can have many configuration files, but only need one script to run them, i think my solution will be more flexible than yours, but anyway, do as you like.
But maybe i got your requirement wrong (if so: please explain in detail what you want to do - the thing with "any environment variable" is obviously not part of the "i write a script to copy files" requirement, because it wouldn't be necessary for that), so please post your script and the intended input file alonw with its expected output or effect. This might help to understand better what you want to do.
Hi,
I am working on PGP encryption. I am getting public keys from some file.
One of the key has dollar sign in it "$" Example: "abc$123"
echo 'passphrase='$passphrase --> Giving correct value abc$123
But if I use $passphrase in PGP command getting Invalid passphrase error.
If I... (10 Replies)
Hello there,
first of all, don't hesitate to notify me if I misbehave. I'm a newbe in the use of this site, and in unix too (in english as well :-)
Well, if someone hat a little time to waste, could he explain to me this strange behavior.
I'm using ksh under AIX.
cdeset=$(su - db2inst1 -c... (8 Replies)
Hi All,
I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
RedHat 5
KSH
I am creating an array, and then using case to go through and count for specific words. Then the count gets stored as an expression.
string='ftp rcp rsh telnet ftp ftp'
set -A myarray $string
FTPCOUNT="0"
for command in ${myarray}
do
case $command in
ftp) FTPCOUNT=`expr... (2 Replies)
Hi,
I am using git bash terminal window to do git operations. I have set the prompt string variable PS1 in the ~/.bashrc file as follows:
export PS1=" "
This is intended to show me the current git branch's name which is active as part of the prompt string. But, the problem is when I do a git... (2 Replies)
Hello,
First post for Newbie as I am stumped. I need to get certain elements for a specific PID from the ps command. I am attempting to pass the value for the PID I want to retrieve the information for as a variable. When the following is run without using a variable, setting a specific PID,... (3 Replies)
hello everyone,
I have file with de-limited values in random order shown below, where C1V0 represents column1 value1,
C2V0 represents column2 value 2, and so on.
Column0 is a constant value which i didn't represent in the sample input format.
Column1 is a numeric column.
Column2 is a... (0 Replies)
Hi,,
Let example cmd: $$config/all
Here I want to replace or subsitute blank space and also with any other character in place of "$" sign...and also want to replace backslash (/) with forward (\)......in expect script
please could any one help on this.....thank you (2 Replies)
Hello All,
I don't write scripts very often, and in this case I am stumped, although it may be a bug in the version of bash I have to use (it's not my system).
I want to extract a specific string snippet from a block of text (coming from a log file) that is dependent on a bunch of other... (1 Reply)
cat test.ksh
#!/usr/bin/ksh
VAR="Dear Friends \n How are you? \n Have a nice day \n"
export VAR
echo "Inside test.ksh";
./test.pl
cat test.pl
#!/usr/bin/perl
print "Inside test.pl \n";
print "$VAR";
Output:
./test.ksh
Inside test.ksh
Inside test.plWhat I want to achieve is, I... (1 Reply)