Jump to content
Can't remember your login details? Read more... ×
Sign in to follow this  

BASH: Adding a space into a variable in a loop

Recommended Posts

Hi All,


Fark, Friday arvo and I still haven't figured this out!


I'm working on a backup script for work and am attempting to get all the files/folders to be stored into a single variable.


Basically the end result should be a variable that can then be passed to zip (or similar) to backup all the files/folders specified. The idea being one can specify as many files/folders as one wants and they'll all be backed up into one archive, hence the loops.


Problem is I can't get the loop to add a space between each file/folder and add it to the string. First loop is fine, it's the eval line in the second loop which I can't get to work properly.


Here's the code:


# !/bin/bash

subdir1=some\ folder/file1.txt

until [ "$string" = "" ]
		eval string="$""subdir"$counter
		if [ "$string" != "" ]
				eval "subdir"$counter=$mntdir"$""subdir"$counter
				#eval string="$""subdir"$counter
				#echo $string
				let counter=counter+1
				eval string="$""subdir"$counter
		elif [ $counter = 1 ]
				#echo $subdir1

until [ $counter = 0 ]
		# This is the troublesome line here
		eval string=$string"$""subdir"$counter
		#eval string=$string"$""subdir"$counter"\ "
		let counter=counter-1
string="echo $string"
eval `echo $string`

echo ""
echo This is how it should look...
var1=$subdir1" "$subdir2" "$subdir3" "$subdir4" "$subdir5
echo $var1

Running that as is, produces output very close to what I need, just with no spaces between the file paths.


I can get it to do what I want manually (last three lines of code), but there's an unknown quantity of 'subdir's defined so it needs to go through a loop.


Uncomment the eval line in the last loop and comment out the line above it to see what I mean. That adds the space I want but produces a "file/directory not found" error. Thing is in the error you can see that there is a space after the file name, so the space is being seen but doesn't want to be added to the variable for some reason.


+100 points to anyone who can help with a solution.

Share this post

Link to post
Share on other sites

Without addressing your scripting problem, I think you're going about backups in the wrong way.


I think you should look into rsync, and / or tar. rsnapshot might even do what you need, and is really simple.

Share this post

Link to post
Share on other sites

Yeah nah that's fine. I'm already rsyncing some things, others I need to compress.


I've got a script I've built that caters for all sorts of backup methods. I just create a separate configuration file with the variables I want in it and run the script with that.


Seeing as the one script does many backups it needs to be flexible hence why I'm trying to do it this way.

Share this post

Link to post
Share on other sites

Dear me, just put all the folders you want to backup into the same string delimited with spaces. Is there any reason you want them in subdirX like that?

Share this post

Link to post
Share on other sites

While that idea would work freespace it's not exactly what I'm after. And yes there is a reason.


Thanks all for your code suggestions *cough*


Seems as though I was using the eval statement when I didn't need to.


Replaced the eval line in the 2nd loop with


string=${string}\$subdir${counter}"\ "


Edited by mark84

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this