LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-04-2013, 03:04 PM   #1
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Rep: Reputation: Disabled
BASH Help - Set multiple directories in variable


Hi All,
I have a script as below
Code:
# cat /tmp/test.sh 
#!/bin/bash
Source=('/boot' '/etc')
tar -cpPjvf /tmp/dummy.tar.bz2 "$Source"
But it is backing up only /boot but not /etc. So please help me how can I set multiple source path to single variable i.e to backup every folder whatever showed in "Source=".

Thanks in adavance for your kind help.
 
Old 06-04-2013, 03:20 PM   #2
spazticclown
Member
 
Registered: Sep 2010
Distribution: Fedora, Android, CentOS
Posts: 91
Blog Entries: 2

Rep: Reputation: 21
If you toss in an echo $Source after the Source= then you will see why it is backing up only /boot and not /etc.

Try using double or single quotes around all of the path(s) and dumping the parentheses.

You can test the output by declaring Source in different ways and then echo Source to see what you get.
 
Old 06-04-2013, 03:28 PM   #3
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by spazticclown View Post
Try using double or single quotes around all of the path(s) and dumping the parentheses.
Thanks for the reply. Since I am newbie in shell scripting can you please show me how?
 
Old 06-04-2013, 03:31 PM   #4
mandyapenguin
Member
 
Registered: Nov 2011
Location: India
Distribution: RedHat, Cent OS, Fedora, Debian, Ubuntu
Posts: 106

Rep: Reputation: Disabled
Quote:
Originally Posted by cli View Post
Thanks for the reply. Since I am newbie in shell scripting can you please show me how?
Code:
$ Source=('/boot' '/etc')
$ echo $Source
/boot
$ echo ${Source[@]}
/boot /etc
So try
Code:
tar -cpPjvf /tmp/dummy.tar.bz2 "${Source[@]}"
 
1 members found this post helpful.
Old 06-04-2013, 03:40 PM   #5
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by mandyapenguin View Post
tar -cpPjvf /tmp/dummy.tar.bz2 "${Source[@]}"
Thank you very much mandyapenguin. Now it is backing up both directories.
 
Old 06-04-2013, 04:51 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
As you've discovered, this is using an array, rather than a single, scalar, variable.

And that's what arrays are there for. Lists of things, and especially filenames, are very difficult to handle safely with scalar variables. So good work on using the correct tool!

Note that the first, [0], element of an array is basically the same as a scalar variable of the same name, which is why $source only prints the first directory.
 
1 members found this post helpful.
Old 06-04-2013, 07:53 PM   #7
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,168
Blog Entries: 10

Rep: Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979
Quote:
Originally Posted by David the H. View Post
As you've discovered, this is using an array, rather than a single, scalar, variable.

And that's what arrays are there for.
Any "Got ya"s on using arrays?
 
Old 06-05-2013, 05:00 AM   #8
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
As you've discovered, this is using an array, rather than a single, scalar, variable.
Thank you very much David the H.
You introduced a very good BASH tutorial site.
Code:
# cat array.sh 
#!/bin/bash
SourceA[0]="/test1"
SourceA[1]="/test 2"
SourceA[2]="/Dummy data"
for i in "${!SourceA[@]}"; do
	ls -ld "${SourceA[i]}"; done

echo

SourceB=("/test1" "/test 2" "/Dummy data")
for i in "${!SourceB[@]}"; do
	ls -ld "${SourceB[i]}"; done
Output
Code:
# bash array.sh 
drwxr-xr-x 2 root root 4096 Jun  5 13:56 /test1
drwxr-xr-x 2 root root 4096 Jun  5 13:56 /test 2
drwxr-xr-x 2 root root 4096 Jun  5 14:02 /Dummy data

drwxr-xr-x 2 root root 4096 Jun  5 13:56 /test1
drwxr-xr-x 2 root root 4096 Jun  5 13:56 /test 2
drwxr-xr-x 2 root root 4096 Jun  5 14:02 /Dummy data
There is no one error, even the directory name is having a white space in the middle. This type of array is very good to handle the files which are having white space in their names. Thanks a lot.
 
Old 06-06-2013, 01:42 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by Habitual View Post
Any "Got ya"s on using arrays?
Not many that I can think of. Off the top of my head the biggest things to keep in mind are the differences between "*" and "@", and the importance of quoting the expansion at all times (just as with all parameters). And you always have to remember that indexes start at 0.

Associative arrays operate slightly differently from regular arrays too, in that the index field is reconfigured as a simple text field, instead of having an arithmetic context.

I imagine the other big area of difficulty is probably just getting the array correctly set in the first place. You still have to deal with correctly handling delimiters and such at that point. The plus side is, of course, that you only have to do it once, and after that it's smooth sailing.

Other than that, nothing really comes to mind right now. They're a bit more complex than regular variables, naturally, but for the most part they act just the same.
 
1 members found this post helpful.
Old 06-06-2013, 05:16 PM   #10
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,168
Blog Entries: 10

Rep: Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979
Quote:
Originally Posted by David the H. View Post
for the most part they act just the same.
Thank you.

double quotes or singles for
Source=('/boot' '/etc') ?

I used single quotes today without issue, but I lean towards double-quotes in my assignments usually.
 
1 members found this post helpful.
Old 06-07-2013, 03:27 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Code:
t=( a b )
echo $t{[0]}
a

echo ${t[1]}
b

v1=33
t=( v1 b )
echo ${t[0]}
v1
from which we can conclude that interpolation is not an issue and for array values with no spaces, it automatically gets it 'right' ie puts them in separate elements.
For vals with embedded spaces, I always go with single-quotes, just for safety/paranoia.
(Also, single-quotes are one-key; double-quotes require a shift-key as well )
 
1 members found this post helpful.
Old 06-07-2013, 08:51 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
@Habitual

Actually, you don't need quotes at all in that particular case. When setting an array, each individual "word" inside the parentheses will be given its own entry. You only have to make sure that multi-word strings are grouped properly, just as with any other command.

It really helps to think about the shell command line in terms of tokens ("word" being a synonym for these unbreakable text strings). That's its main job after all, to build a list of tokens that define the command and its arguments, and then to execute it. The shell just needs to know what to treat as an individual token, and the two types of quotation marks* are the main way to do this. They escape the whitespace and other reserved characters between them to create contiguous strings. If there are no reserved characters in a string, quoting it is unnecessary**.

Check out these links for a better understanding of how a line is parsed.
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/BashParser


* I take it you already know the general difference between single and double quotes.

** I sometimes like to include them anyway for consistency and readability, especially when working with mixed groups of entries, but the shell doesn't care either way.
 
1 members found this post helpful.
Old 06-07-2013, 09:02 AM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by chrism01 View Post
from which we can conclude that interpolation is not an issue and for array values with no spaces, it automatically gets it 'right' ie puts them in separate elements.
For vals with embedded spaces, I always go with single-quotes, just for safety/paranoia.
The only difference between double and single quotes is that doubles continue to allow the interpretation of [$`\!] characters (! only when history expansion is enabled), so that substitutions and escaping can still occur. Otherwise it doesn't matter which you use. And as I just mentioned, neither is needed if there are no reserved characters in the string.

Quote:
(Also, single-quotes are one-key; double-quotes require a shift-key as well )
On my Japanese keyboard the double quote is shift+2, and the single quote is shift+7, so the former is actually easier for me. I don't have to stretch my fingers as far.
 
1 members found this post helpful.
Old 06-08-2013, 12:50 AM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
I just knew there'd be someone with 2-key requirement... sigh
 
  


Reply


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] set variable in bash mohitvad Linux - Newbie 4 09-08-2012 04:34 PM
Bash Question(for loop): How to Zip multiple files under multiple directories Znrall Linux - General 2 08-01-2012 02:52 PM
Bash quesion - scp on multiple hosts to multiple directories in each one mierea.ciprian Programming 2 06-22-2012 01:27 PM
Multiple variable for loop (bash) Phier Linux - Newbie 2 11-23-2009 12:13 PM
create multiple directories in bash? Count Zero Programming 19 07-06-2008 09:34 AM


All times are GMT -5. The time now is 12:41 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration