[Solved] cp command with dollar variable in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] cp command with dollar variable in ksh
# 1  
Old 08-05-2012
Question [Solved] cp command with dollar variable in ksh

hi,

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.

Please help guys!
# 2  
Old 08-05-2012
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)
Code:
correct ex - export DESTN_PATH=/fs/dir1/dir2
wrong ex - export DESTN_PATH=/fs/dir1/dir2/

make sure the assignment doesn't contain any space on both side of '='

it should work else post your sample code here.
# 3  
Old 08-05-2012
Quote:
Originally Posted by donadarsh
export the DESTN_PATH variable. (it shouldn't contain / at last of the path, otherwise you will two // in your output expression)
That doesn't actually matter. If you can name any extant UNIX system which doesn't do the right thing when given two /'s, I'll be quite surprised.
# 4  
Old 08-05-2012
Quote:
Originally Posted by Corona688
That doesn't actually matter. If you can name any extant UNIX system which doesn't do the right thing when given two /'s, I'll be quite surprised.
If i remember I faced it sometimes back. Not sure if it was exact same but related to it.
# 5  
Old 08-05-2012
Quote:
Originally Posted by abdulhusein
- 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:

Code:
filename=abc.scr destn= $DESTN_PATH/test

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:

Code:
one.file    $DEST/new.filename
other.file   $DEST/other.newname

and you read it something like this:

Code:
DEST=/some/where
SRC=""
TGT=""

while read SRC TGT ; do
     cp $SRC $TGT
done < file

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:
Code:
# This is a possible input file
DEST=/some/where
first.file first.file.new

# now we change the DEST value
DEST=/else/where
second.file second.file.new
third.file third.file.new

The expected output from such an input would be like this:

Code:
cp first.file /some/where/first.file.new
cp second.file /else/where/second.file.new
cp third.file /else/where/third.file.new

Here is a sketch of such a script (replace "<spc>" and "<tab>" with the respective literal characters):

Code:
#!/bin/ksh

INFILE="$1"
DEST=""
SRC=""
TGT=""

# the sed is only to clean out comments, leading and trailing blanks and empty lines
sed 's/#.*$//
     s/^[<spc><tab>][<spc><tab>]*//
     s/[<spc><tab>][<spc><tab>]*$//
     /^$/d' $INFILE |\
while read SRC TGT ; do
     if [ "${SRC%=*}" = "DEST" ] ; then
          DEST="${SRC#=}"
     else
          if [ -z "$DEST" ] ; then
               print -u2 - "Error in input file, no \"DEST=\" clause."
               exit 2
          fi
          if [ ! -r "$SRC" -o -w "$DEST" ] ; then
               print -u2 - "Error: $SRC not readable or $DEST not writable."
               exit 1
          fi
          print - "cp $SRC $DEST/$TGT"     # remove the print if the result is as expected
     fi
done

exit 0


I hope this helps.

bakunin
# 6  
Old 08-05-2012
hey

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.
# 7  
Old 08-05-2012
Quote:
Originally Posted by abdulhusein
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.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dollar symbol in Shell Script Variable

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)
Discussion started by: Sreehari
10 Replies

2. Shell Programming and Scripting

Grep and dollar command

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)
Discussion started by: skitoc
8 Replies

3. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

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)
Discussion started by: danish0909
5 Replies

4. Shell Programming and Scripting

[Solved] KSH: Array/If Help

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)
Discussion started by: nitrobass24
2 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Help needed to have changing value to the command prompt string variable PS1

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)
Discussion started by: royalibrahim
2 Replies

6. Shell Programming and Scripting

Variable to command to Variable Question KSH

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)
Discussion started by: Coyote270WSM
3 Replies

7. Shell Programming and Scripting

[Solved] Help with Sort command (ksh)

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)
Discussion started by: angie1234
0 Replies

8. Shell Programming and Scripting

substitute the starting dollar sign in command with blank

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)
Discussion started by: swethakast
2 Replies

9. Shell Programming and Scripting

[Solved] Command Substitution and Variable Expansion within a Case

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)
Discussion started by: jaimielives
1 Replies

10. Shell Programming and Scripting

[SOLVED] Scope of KSH Variable in Perl?

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)
Discussion started by: dahlia84
1 Replies
Login or Register to Ask a Question