Combining multiple variables into new variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combining multiple variables into new variable
# 1  
Old 05-20-2011
Combining multiple variables into new variable

Hello, I am a new joiner to the forum, and have what i hope is a simple question, however I can't seem to find the answer so maybe it is not available within bash scripting.

I intend to use the below script to archive files from multiple directories at once by using a loop, and a variable (n) which increases by one each loop.

The problem is that when i connect two variables, if i define a variable with that output, it is not called, only the text is output.
Hopefully you can see what im trying to do and it will explain itself;


Code:
#!/bin/bash

n=1
a=/archive
dir_1=/home/gohara/test/sd1
dir_2=/home/gohara/test/sd2
dir_3=/home/gohara/test/sd3
filemask_1=TAV*.???
filemask_2=TAV*.???
filemask_3=TAV*.???
fileage_1=30
fileage_2=30
fileage_3=30

while [ $n -le 3 ]

do

#If dir_n is NOT NULL
if [ -n $dir_$n ]; then

find $dir_$n/$filemask_$n -type f -mtime +fileage_$n -exec mv {} $dir_$n$a \;

#Repeat task for 3 directories listed above
n=`expr $n + 1`

fi
done

# 2  
Old 05-20-2011
Quote:
Originally Posted by dring
Hello, I am a new joiner to the forum,
Always handy to know a carpenter Smilie
Code:
#!/bin/bash

n=1
a=/archive
dir_1=/home/gohara/test/sd1
dir_2=/home/gohara/test/sd2
dir_3=/home/gohara/test/sd3
filemask_1='TAV\*.???' # avoid interpolation here
filemask_2='TAV\*.???' # And include an escape
filemask_3='TAV\*.???'# for the call to find
fileage_1=30
fileage_2=30
fileage_3=30

while [ $n -le 3 ]
do
    #If dir_n is NOT NULL
    if [ -n ${dir_$n} ]; then
       find ${dir_$n}/${filemask_$n} -type f -mtime +fileage_$n -exec mv {} ${dir_$n}$a \;

       #Repeat task for 3 directories listed above
       n=`expr $n + 1`
    fi
done

You can surround the variable name with braces to isolate it from surrounding text.
# 3  
Old 05-20-2011
You have to use when you want to build a variable name using another variable. like dir_1 using variable n.

Code:
cgi@tioman> (/home/cgi) $ dir_1="abc"
cgi@tioman> (/home/cgi) $ n=1
cgi@tioman> (/home/cgi) $ echo $dir_$n
1

Here you won't get the value, because it tries $dir_ as one variable which is empty and $n as another variable which is 1, but below method works.
Code:
cgi@tioman> (/home/cgi) $ eval echo '$'dir_$n
abc
cgi@tioman> (/home/cgi) $

---------- Post updated at 01:04 PM ---------- Previous update was at 01:00 PM ----------

Quote:
Originally Posted by Skrynesaver
Always handy to know a carpenter Smilie
Code:
#!/bin/bash

n=1
a=/archive
dir_1=/home/gohara/test/sd1
dir_2=/home/gohara/test/sd2
dir_3=/home/gohara/test/sd3
filemask_1='TAV\*.???' # avoid interpolation here
filemask_2='TAV\*.???' # And include an escape
filemask_3='TAV\*.???'# for the call to find
fileage_1=30
fileage_2=30
fileage_3=30

while [ $n -le 3 ]
do
    #If dir_n is NOT NULL
    if [ -n ${dir_$n} ]; then
       find ${dir_$n}/${filemask_$n} -type f -mtime +fileage_$n -exec mv {} ${dir_$n}$a \;

       #Repeat task for 3 directories listed above
       n=`expr $n + 1`
    fi
done

You can surround the variable name with braces to isolate it from surrounding text.
Code:
cgi@tioman> (/home/cgi) $ echo ${dir_$n}
-bash: ${dir_$n}: bad substitution
cgi@tioman> (/home/cgi) $
cgi@tioman> (/home/cgi) $

It gave me bad subsitution error, I don't this the value inside the {} will go for interpretation, where your $n will change for its value.
This User Gave Thanks to kumaran_5555 For This Post:
# 4  
Old 05-20-2011
Welcome to the forum!

As far as I know, bash doesn't allow such thing as ${dir_$n}.
I'd use array instead:
Code:
dir[$a]=someValue;
echo ${dir[$a]}

Array indices can be strings, too.

To loop through the array (in an arbitrary order!), you'd do:
Code:
for d in ${dir[@]} ; do 
  #whatever with $d
done

The variable ${dir[@]} returns the whole array. (same thing ${dir[*]})

Last edited by mirni; 05-20-2011 at 06:22 AM..
This User Gave Thanks to mirni For This Post:
# 5  
Old 05-22-2011
MySQL

Thanks heaps kumaran_5555 and mirni.

I've only just tested what you mentioned which workeda treat!
Now I have to try and utilise it multiple times for multiple variables throughout the script.
I havent yet attempted the array but I might give it a go this week.

Once done I'll post a copy of the finished script as i havent been able to find one similar in the forums.

Thanks again,
Danny.
# 6  
Old 05-22-2011
Back to Post #1. If you really want to do it this way, then you need to use a Shell "eval" statement wherever you are changing the name of an environment variable on the fly. This further involves escaping certain $ symbols to protect them from the Shell first pass.

On a design point it would be much easier to read a flat file of records with each records containing the three parameters. The script would then work for a virtually unlimited number of records.
# 7  
Old 05-25-2011
Hey guys,

Thanks again for your help above, much appreciated.
I'm now trying to use the eval statement as advised above before attempting an array.

Can anyone tell me what i'm doing wrong? I sort of understand the method behind the eval command but haven't been able to use it successfully with the below script...
It should be self explanatory what im trying to acheive, any help would be appreciated. Thanks!
Code:
#!/bin/bash

a=/archive
n=1
home=/home/gohara/test

dir_1=/sd1
dir_2=/sd2
dir_3=/sd3

filemask_1=TAV*.???
filemask_2=TAV*.???
filemask_3=TAV*.???

fileage_1=30
fileage_2=30
fileage_3=30

deleteage_1=365
deleteage_2=365
deleteage_3=365


while [ $n -le 3 ]

do

#eval echo '$'dir_$n

eval find $home$dir_$n/$filemask_$n -type f -mtime +$fileage_$n -exec ls {} $home$dir_$n$a \;

n=`expr $n + 1`

done
echo "Script Complete"


Last edited by Franklin52; 05-25-2011 at 07:00 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help using combining variables with sed command (RHEL 7)

Here is the whole script, very simple, but I am just learning ROK_NO=$1 RPT=/tmp/test sed -E '/^SELECT/ s/(.{23}).{8}/\1'"$ROK_NO"' /' $RPT echo $RPT When I run this I get $ bash rok.sh 2388085 : No such file or directory /tmp/test When I type the command in console, it works... (3 Replies)
Discussion started by: isey78
3 Replies

2. UNIX for Beginners Questions & Answers

Combining multiple greps

I'm trying to learn about regular expressions. Let's say I want to list all the files in /usr/bin beginning with "p", ending with "x", and containing an "a". I know this works:ls | grep ^p | grep x$ | grep abut I'm thinking there must be a way to do it without typing grep three times. Some of my... (9 Replies)
Discussion started by: Xubuntu56
9 Replies

3. Shell Programming and Scripting

Combining a declared variable with a temporary variable

Hi folks! Kind of a noob question... from an OLD AIX/HPUX Admin. I am writing a script to ease use of a command; an extended aliasing if you will. What I want to do is set several variables (OPT1, OPT2, etc) with command arguments, such as --help, --list-all, etc. Later in the script, I... (5 Replies)
Discussion started by: clee
5 Replies

4. UNIX for Beginners Questions & Answers

Combining multiple files into one

Hello Everyone, I have 4 different files (one column in each) that I'm trying to combine into 1 file with four columns. Having issues trying to get the columns to format properly. I have tried the following: paste file1 file2 file3 file4 | column -s $'\t' -t > results.txt paste file1 file2... (1 Reply)
Discussion started by: malk71
1 Replies

5. Shell Programming and Scripting

Combining multiple files

I have 2 files. each having 3 coloums 1st field date as 20130322 2nd field time as 05:55 3rd field numberic value File 2 has entries missing for some date time. FILE1 20130322 05:35 2219 20130322 05:40 1809 20130322 05:45 1617 20130322 05:50 ... (2 Replies)
Discussion started by: sandeepkmehra
2 Replies

6. Shell Programming and Scripting

Combining multiple files into one with the same name/different extension

I've been trying to find information in regard to creating a script that will generate HTML files. I currently have a series of files that contain code I need to surround with a <textarea> tag for easy viewing. I have about a thousand files that contain code, one file that contains the HTML code up... (10 Replies)
Discussion started by: 12o
10 Replies

7. Shell Programming and Scripting

Problem combining two variables into one

Hello, I have a problem combining two variables into one. I did the following: in my env variables i had set PATH_DESTINATION_1=/root/path_one PATH_DESTINATION_2=/root/path_two #!/usr/bin/ksh count=1 count_path=2 while do (3 Replies)
Discussion started by: Eraser
3 Replies

8. Shell Programming and Scripting

Combining two variables in ksh

I can't believe I can't figure this out... given this code: CARS_DATA_LIST=`cat /tmp/file1 | awk '{print $1}' ` FMSA_DATA_LIST=`cat /tmp/file2 | awk '{print $1}' ` The value of each of the above variables is: CARS = a b c d e f g FMSA = a b c q r s I want to declare a third... (8 Replies)
Discussion started by: Shoeless_Mike
8 Replies

9. Shell Programming and Scripting

Combining multiple commands

Hi Guys, I am looking to optimze these 5 SSH lines to a single SSH to get my machine to not hang! lol! cat hosts.lst | xargs -n1 -t -i echo 'home/util/timeout 6 0 ssh -q {} top -b > util/{}.top &' >> r_query_info cat hosts.lst | xargs -n1 -t -i echo 'home/util/timeout 6 0 ssh -q {} uname -r... (5 Replies)
Discussion started by: wick3dsunny
5 Replies

10. Shell Programming and Scripting

Combining multiple lines

I am fairly new to scripting. But I have been able to extract and format all of my information required into one file. My issue is that one character is on a separate line. I need to be able to add the character to the previous line. ex. abcdefghi 1 bcdefghij 3 cdefghijk 4 need to... (4 Replies)
Discussion started by: DUST
4 Replies
Login or Register to Ask a Question