Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-01-2015, 11:36 AM
|
#1
|
Member
Registered: Sep 2006
Posts: 57
Rep:
|
get path name from array -> always wants to do math
With the following code
Code:
DIRS[0]=home/mb
DIRS[1]=etc
for i in ${DIRS[@]}
do
echo ${DIRS[$i]}
done
I always get a "division by zero" error because of the "/" in DIRS[0].
It works if I write
but as soon i use the $i to access the value the bash misinterprets the "/" and wants to do math.
|
|
|
07-01-2015, 11:57 AM
|
#2
|
Senior Member
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,197
|
this is because the command '${DIRS[$i]}' expects a number inside the brackets as its an array subscript what you want is
Code:
DIRS[0]=home/mb
DIRS[1]=etc
for i in ${DIRS[@]}
do
echo $i
done
|
|
|
07-02-2015, 03:22 AM
|
#3
|
Member
Registered: Sep 2006
Posts: 57
Original Poster
Rep:
|
Now I have this
Code:
#!/bin/bash
TARGET=/media/mb/Elements/DAR20
DATE=`date -I`
DAROPTIONS='-m 0 -z -s 50000M -D -Z "*.gz" -Z "*.bz2" -Z "*.zip" -Z "*.png" -Z "*.7z" -v'
# LOCAL
DIRS[0]=home/mb
EXCLUDES[0]='-P tmp -P VirtualBox\ VMs -P Downloads -P .cache -P .local/share/Trash'
DIRS[1]=etc
EXCLUDES[1]=
for i in ${DIRS[@]}
do
FILE=$TARGET/"$i"/${DATE}_${y}
mkdir -p $TARGET/"$i"
if [ ! -f "$FILE" ]
then
dar -R /"$i" -c "$FILE" $DAROPTIONS ${EXCLUDES[$i]}
else
PREV=`ls $TARGET/"$i"/*.dar | head -n 1`
dar -R /"$i" -c "$FILE" -A ${PREV%%.*} $DAROPTIONS ${EXCLUDES[$i]}
fi
done
And still get division by zero on line 20.
|
|
|
07-02-2015, 07:38 AM
|
#4
|
Senior Member
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,197
|
you have done exactly the same as before, you can NOT use a string as a subscript it MUST be nunber.
|
|
|
07-02-2015, 07:31 PM
|
#5
|
Senior Member
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,197
|
As I see you have started a new thread with this same script but have fixed the subscript problem I assume you consider this part of the problem solved, so please mark this thread as solved its also good manners to say thanks and post the solution to your problem for the sake of any one else searching the forum with a similar problem.
|
|
|
07-03-2015, 02:09 AM
|
#6
|
Member
Registered: Sep 2006
Posts: 57
Original Poster
Rep:
|
Sorry about that. You are right.
I solved it like this
Code:
for ((i=0; i<${#DIRS[@]}; i++))
do
FILE=$TARGET/"${DIRS[$i]}"/${DATE}_${y}
mkdir -p $TARGET/"${DIRS[$i]}"
if [ ! -f "$FILE" ]
then
dar -R /"${DIRS[$i]}" "${EXCLUDES[$i]}" -c "$FILE" $DAROPTIONS
else
PREV=`ls $TARGET/"${DIRS[$i]}"/*.dar | head -n 1`
dar -R /"${DIRS[$i]}" "${EXCLUDES[$i]}" -c "$FILE" -A ${PREV%%.*} $DAROPTIONS
fi
done
|
|
|
All times are GMT -5. The time now is 03:05 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|