LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-04-2015, 04:41 AM   #1
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
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
 
Old 09-04-2015, 05:33 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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
 
1 members found this post helpful.
Old 09-04-2015, 10:05 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
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.
 
1 members found this post helpful.
Old 09-04-2015, 10:15 AM   #4
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
...
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
 
Old 09-04-2015, 10:37 AM   #5
polaris96
Member
 
Registered: Jan 2015
Distribution: Slackware, LFS, OpenIndiana, debian wheezy
Posts: 55

Rep: Reputation: Disabled
Arrow

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

Last edited by polaris96; 09-04-2015 at 10:39 AM. Reason: final suggestion
 
  


Reply

Tags
bash, variables


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Populate array and ignore lines with certain characters jtn703 Programming 2 11-19-2012 08:14 PM
Modify script loading array with ranges of lines in awk cgcamal Programming 7 03-18-2011 11:14 PM
Bash Script Array index value Kedelfor Programming 10 04-29-2009 04:37 AM
Another Bash iterate lines to array... jchambers Programming 9 08-22-2007 02:06 PM
Can't get lines of a file with a Bash script.. barisdemiray Programming 2 08-11-2004 12:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration