LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: how to distribute an array into lines in a script (https://www.linuxquestions.org/questions/programming-9/bash-how-to-distribute-an-array-into-lines-in-a-script-4175552596/)

markush 09-04-2015 04:41 AM

Bash: how to distribute an array into lines in a script
 
Hi all,

well, the subjet isn't very meaningfull, is it ;-)

I'm writing a backup-script and want to have a separate configuration script.

As yet my Script has the following lines:
Code:

...
    --include /etc \
    --include /root \
    --include /home/$BACKUPUSER \
    --include /usr/local/ \
    --include /var/spool/cron/crontabs \
 ...

But I want to put the pathes into a separate configuration file looking like this:
Code:

FILES="/root /etc /home/$BACKUPUSER /usr/local /var/spool/cron/crontabs"
I know that I can put $FILES into an array this way
Code:

myarray=($FILES)
But I don't have an idea how to put the variables in the array into my --include lines as I don't yet know how much variables there will be.
Code:

--include $1 \
--include $2
...

would therefore not be a proper solution.

Thanks in advance

Markus

HMW 09-04-2015 05:33 AM

This seems overly complicated, but I might be missing the point somewhere. Can't you just loop the array?

Code:

$ echo "${myarr[@]}"
foo bar baz


Code:

$ for item in "${myarr[@]}"; do echo "--include $item \\"; done
--include foo \
--include bar \
--include baz \


Best regards,
HMW

grail 09-04-2015 10:05 AM

hmmm ... I think part of the problem here is you have not really provided enough information about what backup program you are using (something like rsync??).

So my first question back would be, can your backup program that uses --include switch not accept multiple paths for a single switch?

If you are going define as an array, I would just do it straight off instead of first placing it in a space separated string.

Lastly, you can also use HMW's method to build your command line and then execute that.

markush 09-04-2015 10:15 AM

Quote:

Originally Posted by grail (Post 5415781)
...
So my first question back would be, can your backup program that uses --include switch not accept multiple paths for a single switch?
...

Well, that was the question I needed. I didn't read the whole manpage. I'm using duplicity and indeed besides --include there is also an option --include-filelist which solves my problem.

Markus

polaris96 09-04-2015 10:37 AM

You're hammering nails with a wrench. I'm not teasing. This is how we learn. But give this a try:

Make a file in /root called "backup.txt" or some such, instead of hard coding an array.

Code:

touch /root/backup.txt
nano /root/backup.txt

enter the paths of all your "include" files/directories, there

Then, as was mentioned, use a FOR..IN to pack them into the command.

Code:

#!/bin/bash
for I in `cat /root/backup.txt`
do
cp -a $I /$DEST
#Or substitute with your command of choice
done

This is better because it's WAY easier to update a text file than a hard coded array. It also shows a good bit about how UNIX works. Extracting info from a text file is called parsing and it's one of the biggest jobs for an old school admin. It gives you a lot of power to control your system.

In this vein we could go a little deeper and write an awk script to parse the data

Code:

/#.*/  {
          # Ignore any comments
          next
        }
/^$/    {
          #ignore blank lines
          next
        }
/.*/    {
        #Act on anything else
        system("cp -a $1 /HARD_CODED_PATH")
        next
        }

the script above will allow you to write comments (#) and skip lines in the backup.txt file. Nice for leaving yourself notes. Save it as backup.awk.

call it like this:
Code:

awk -f backup.awk backup.txt
Just a few options :) HMW was absolutely right about how to code an array. You're much better off with a config file, though.


Finally. This is great training, but if backup is your desire, please check out rsync (man rsync). It's a backup dream come true and very worth learning.

cheers


All times are GMT -5. The time now is 08:00 AM.