Sponsored Content
Top Forums Shell Programming and Scripting [Solved] cp command with dollar variable in ksh Post 302682161 by bakunin on Sunday 5th of August 2012 05:02:16 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 06:09 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy