Combining multiple variables into new variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combining multiple variables into new variable
# 8  
Old 05-25-2011
Code:
eval find '$'home'$'dir_$n/'$'filemask_$n -type f -mtime +'$'fileage_$n -exec ls {} '$'home'$'dir_$n'$'a \;

This will work. You have to use eval to in $dir_$n to replace only $n but keep $dir as it is. after eval you should have statement $dir_1.
# 9  
Old 05-25-2011
That's getting really hairy. It's gonna become an ugly mess of dollar signs and quotes.

Also, using 'eval' can have security implications, when you run 'eval' on something that is not hard-wired (like output of find(1)).


I recommend you look into arrays.
# 10  
Old 05-26-2011
hey legends,
just tested the script with an array (first time for me). check it out;
Code:
#!/bin/sh

HOME=/home/gohara/test/
ARCHIVE=archive/
DIRECTORY=( "sd1/" "sd2/" "sd3/")
FILEMASK=( "TAV*.*" "CRM*.*" "FTO*.*" )
ARCHIVETIME=( "30" "60" "90" )
DELETETIME=( "365" "90" "120" )


n=${#DIRECTORY[@]} #num elements in array DIRECTORY
for (( i=0; i<n; i++ ))

do

echo -e "\n!! Moving files older than ${ARCHIVETIME[$i]} days, like ${FILEMASK[$i]} from $HOME${DIRECTORY[$i]} to $HOME${DIRECTORY[$i]}$ARCHIVE...\n"

find $HOME${DIRECTORY[$i]}${FILEMASK[$i]} -type f -mtime +${ARCHIVETIME[$i]} -exec ls {} \;

find $HOME${DIRECTORY[$i]}${FILEMASK[$i]} -type f -mtime +${ARCHIVETIME[$i]} -exec mv {} $HOME${DIRECTORY[$i]}$ARCHIVE  \;

echo -e "\n!! Deleting files older than ${DELETETIME[$i]} days from $HOME${DIRECTORY[$i]}$ARCHIVE\n"

find $HOME${DIRECTORY[$i]}$ARCHIVE${FILEMASK[$i]} -type f -mtime +${DELETETIME[$i]} -exec ls {} \;

find $HOME${DIRECTORY[$i]}$ARCHIVE${FILEMASK[$i]} -type f -mtime +${DELETETIME[$i]} -exec rm {} \;

echo -e "\n!! Archive complete for ${DIRECTORY[$i]}\n\n"

done

echo -e "\n\n!! Script Complete\n"

---------- Post updated at 01:46 PM ---------- Previous update was at 01:45 PM ----------

now i just gotta try retriving the variables from a config file, and outputting all the info to a log file!!

Last edited by Scott; 05-26-2011 at 01:36 AM.. Reason: Code tags, please...
# 11  
Old 05-26-2011
Looks good. Missing a dollar sign here
Code:
for (( i=0; i<$n; i++ ))

The two find commands can be put together, one way would be through -ls command of find(1).
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