LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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


Reply
  Search this Thread
Old 07-01-2015, 10:36 AM   #1
slackware_newbie
Member
 
Registered: Sep 2006
Posts: 57

Rep: Reputation: 15
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
Code:
echo $DIRS[0]
but as soon i use the $i to access the value the bash misinterprets the "/" and wants to do math.
 
Old 07-01-2015, 10:57 AM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Red face

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
 
Old 07-02-2015, 02:22 AM   #3
slackware_newbie
Member
 
Registered: Sep 2006
Posts: 57

Original Poster
Rep: Reputation: 15
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.
 
Old 07-02-2015, 06:38 AM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
you have done exactly the same as before, you can NOT use a string as a subscript it MUST be nunber.
 
Old 07-02-2015, 06:31 PM   #5
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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.
 
Old 07-03-2015, 01:09 AM   #6
slackware_newbie
Member
 
Registered: Sep 2006
Posts: 57

Original Poster
Rep: Reputation: 15
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
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl Math::Bezier returns Math::Bizarre output captainentropy Programming 3 10-09-2013 09:00 PM
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
C (math.h)not doing right math? exp() issue. knockout_artist Programming 7 11-25-2011 02:13 PM
Running a 2.6.* kernel with math emulation ( Does the math emulation work ?) dar_beh_dar Linux - Kernel 3 05-20-2009 11:43 PM
math program that I can enter math functions ... Four General 5 04-19-2006 08:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:04 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