Multiple variable substitutions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple variable substitutions
# 1  
Old 10-20-2010
Multiple variable substitutions

Is there anyway to accomplish this?
(ksh)

Code:
FILES_TO_PROCESS='NAME1 NAME2'

SOURCE_NAME1=/tmp/myfile
TARGET_NAME1=/somewhere/else
# other file names

for i in $FILES_TO_PROCESS
do
file1=SOURCE_$i
file2=TARGET_$i
echo cp ${$file1} ${$file2}  <-- how do get this to work.
done

# 2  
Old 10-20-2010
Is it what you are trying to achieve ?

Code:
SRC=/tmp/src
DEST=/tmp/dest
for i in file1 file2
do
echo "cp $SRC/$i $DEST/$i"
done

or
Code:
... 
eval file1=\$SOURCE_$i
eval file2=\$TARGET_$i
...
eval echo "cp $file1 $file2"

eval will force the expansion of $var but not the one which is \ protected
so the finale commande after eval should look like
echo "cp $SOURCE_NAME1 $TARGET_NAME1"

Note that this would suppose that you have already setup the 2 other variables
Code:
SOURCE_NAME2=/whateverdir/whaterfile
TARGET_NAME2=/whatevernewdir/whatevernewfile

... which does not currently appear in your code

You could just copy dir
Code:
cp -p /sourcedir/* /targetdir

If you want to move them and rename them at the same time, please give us some clue about how you want them to be renamed

Last edited by ctsgnb; 10-20-2010 at 02:38 PM..
# 3  
Old 10-20-2010
Note the trailing slash to ensure it is a dir.

Code:
(
cd /tmp/src
cp NAME1 NAME2 /tmp/dest/
)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Speeding up substitutions

Hi all, I have a lookup table from which I am looking up values (from col1) and replacing them by corresponding values (from col2) in another file. lookup file a,b c,d So just replace a by b, and replace c by d. mainfile a,fvvgeggsegg,dvs a,fgeggefddddddddddg... (7 Replies)
Discussion started by: senhia83
7 Replies

2. UNIX for Dummies Questions & Answers

Multiple substitutions in one expression using sed

Hi, I'm trying to get multiple substitutions in one expression using sed: echo "-foo-_-bar--foo-_bar_-_foo_bar_-foo_-_bar_-" | sed -e "s//-/g" So, as you can see I'm trying to replace all instances of _-, -_, -- with - (dash) I have provided bad example. The question is how to use multiple... (6 Replies)
Discussion started by: useretail
6 Replies

3. Shell Programming and Scripting

How can I write nested command substitutions?

Hello How can write the nested command substitutions? echo `expr substr $x 1 expr ${#x} - 1` the above code is not working! Thanks in advance Regards Chetanz (5 Replies)
Discussion started by: Chetanz
5 Replies

4. Shell Programming and Scripting

Multiple Substitutions across Multiple Files

Hey everyone! I am determining the best method to do what the subject of this thread says. I only have pieces to the puzzle right now. Namely this: grep -rl "expression" . | xargs open (I should mention that the intention is to grep through many files containing the "expression" and... (2 Replies)
Discussion started by: Alexander4444
2 Replies

5. Shell Programming and Scripting

Two substitutions in one echo

PHOST1=temp i=1 I want to display the value of PHOST1 by making use of variable i inplace of 1 something like this echo "$PHOST$i" # -> This doesn't seem to work. Please provide me the correct syntax. I tried many different ways echo ${PHOST${i}} echo ${PHOST Nothing seems... (6 Replies)
Discussion started by: blazer789
6 Replies

6. Shell Programming and Scripting

arrays and substitutions

I am working on a bash script and ran around this issue. here's the code : #!/bin/bash string="\"bin\" \"barn\" \"bin, barn /\"" array=($string) echo -e "\nMethod 1\narray is ---> ${array}" echo -e "array=($string)" array=("bin" "barn" "bin, barn /") echo -e "\nMethod 2\narray is... (4 Replies)
Discussion started by: titou_dude
4 Replies

7. Shell Programming and Scripting

Perl - nested substitutions

How can I nest substitutions ? My solution just seems cheap ... sample data Cisco Catalyst Operating System Software, Version 235.5(18) Cisco Catalyst Operating System Software, Version 17.6(7) Cisco Catalyst Operating System Software, Version 19.6(7) Cisco Catalyst Operating System... (1 Reply)
Discussion started by: popeye
1 Replies

8. Shell Programming and Scripting

Using Sed to perform multiple substitutions?

Hello I have the following output which is returned with the Month in text format instead of numerical. The output I receive is performed by using Rational Synergy CM software commands from the Unix command line and piping Unix commands on the end. bash-3.00$ ccm query -n... (4 Replies)
Discussion started by: Glyn_Mo
4 Replies

9. Shell Programming and Scripting

Logfile parsing with variable, multiple criterias among multiple lines

Hi all I've been working on a bash script parsing through debug/trace files and extracting all lines that relate to some search string. So far, it works pretty well. However, I am challenged by one requirement that is still open. What I want to do: 1) parse through a file and identify all... (3 Replies)
Discussion started by: reminder
3 Replies

10. UNIX for Dummies Questions & Answers

String substitutions in ASCII files -

We need to scramble data in a number of ASCII files. Some of these files are extremely large (1.2 GB). By scrambling, I mean that we need to substitute certain strings, which number around 400, with scrambled strings. An example has been given below If "London" occurs in the file, then it... (2 Replies)
Discussion started by: SanjivNagraj
2 Replies
Login or Register to Ask a Question