Sponsored Content
Top Forums Shell Programming and Scripting Combining multiple variables into new variable Post 302523848 by kumaran_5555 on Friday 20th of May 2011 03:34:02 AM
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:
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

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