Associated Array in bash

 
Thread Tools Search this Thread
Operating Systems OS X (Apple) Associated Array in bash
# 1  
Old 11-01-2012
Associated Array in bash

I am using code as below to save data in associated array on Mac OS 10.8.
Code:
#!/bin/sh

storyboardExt=".storyboard"
stringsExt=".strings"
newStringsExt=".strings.new"
oldStringsExt=".strings.old"
localeDirExt=".lproj"
baseLprojDir="Base${localeDirExt}"

echo "${baseLprojDir}"

# Find Base.lproj path
baseLprojPath=`find . -name "${baseLprojDir}" -print`
echo "baseLprojPath= ${baseLprojPath}"

#Find storyboard file full path inside project folder
for storyboardPath in `find "${baseLprojPath}" -name "*${storyboardExt}" -print`
do
    echo "storyboardPath= ${storyboardPath}"

    # Get Base strings file path
    baseStoryboardStringsPath=$(echo "$storyboardPath" | sed "s/$storyboardExt/$stringsExt/")
    echo "baseStoryboardStringsPath= ${baseStoryboardStringsPath}"

    storyboardStringsFile=$(basename "$baseStoryboardStringsPath" "${stringsExt}")
    echo "storyboardStringsFile= ${storyboardStringsFile}"

    baseStringsArray["${storyboardStringsFile}"]="${baseStoryboardStringsPath}"
    echo ${baseStringsArray[$storyboardStringsFile]}
done

for baseStringsPath in "${baseStringsArray[@]}"
do
    echo "baseStringsArray= $baseStringsPath"
done

I got result as below, I am not sure why associated array only have one element, can you help me?
Code:
Base.lproj
baseLprojPath= ./SingleStoryboardLocalize/Base.lproj
storyboardPath= ./SingleStoryboardLocalize/Base.lproj/MainStoryboard_iPad.storyboard
baseStoryboardStringsPath= ./SingleStoryboardLocalize/Base.lproj/MainStoryboard_iPad.strings
storyboardStringsFile= MainStoryboard_iPad
./SingleStoryboardLocalize/Base.lproj/MainStoryboard_iPad.strings
storyboardPath= ./SingleStoryboardLocalize/Base.lproj/MainStoryboard_iPhone.storyboard
baseStoryboardStringsPath= ./SingleStoryboardLocalize/Base.lproj/MainStoryboard_iPhone.strings
storyboardStringsFile= MainStoryboard_iPhone
./SingleStoryboardLocalize/Base.lproj/MainStoryboard_iPhone.strings
baseStringsArray= ./SingleStoryboardLocalize/Base.lproj/MainStoryboard_iPhone.strings

One more question, why can't I use "InfoPlist.strings" as associated array index? I have no way to solve it so that I used "InfoPlist" as index...
# 2  
Old 11-01-2012
OSX 10.8 uses bash 3 which does not support associative arrays.. The shebang is not right either as sh is not the same as bash..
OSX does have ksh (ksh93u) which does support associative arrays, so perhaps you could use that instead...
# 3  
Old 11-01-2012
Quote:
Originally Posted by Scrutinizer
OSX 10.8 uses bash 3 which does not support associative arrays.. The shebang is not right either as sh is not the same as bash..
OSX does have ksh (ksh93u) which does support associative arrays, so perhaps you could use that instead...
I changed to use ksh, but I got the same result, do you know why?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash for loop array

Hi there, A bit new to bash and am having an issue with a for loop. I look for filenames in a specified directory and pull the date string from each meeting a certain criteria, and then would like to make a directory for each date found, like this: search 20180101.gz 20180102.gz 20180103.gz... (5 Replies)
Discussion started by: mwheeler12
5 Replies

2. Shell Programming and Scripting

Bash Array connectin to another Array

Hello, i have a script that i need account_number to match a name. for exsample : ACCOUNT_ID=(IatHG8DC7mZbdymSoOr11w KbnlG2j-KRQ0-1_Xk356s8) and i run a loop curl requst with this the issue is that i want to know on which account were talking about so bash will know this : ... (4 Replies)
Discussion started by: batchenr
4 Replies

3. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

4. Shell Programming and Scripting

Array Issue In Bash

Hi, I have the following code which is giving error mentioned below. Please can you support on this. Also suggest how can we access all the items against single vasservicename in circlename array,i.e, vasservicename say MTSTV will be available to all circles which are mentioned in the array... (2 Replies)
Discussion started by: siramitsharma
2 Replies

5. UNIX for Dummies Questions & Answers

Help with bash array

Hi, I would like to remove audio tracks from several video clips contained in a single directory using the command files=(*.mpg); ffmpeg -i "${files}" -vcodec copy -an na/output.mpg I want each processed file outputted to the sub-directory na retaining the original file name, how do I do that?... (4 Replies)
Discussion started by: jeppe83
4 Replies

6. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

7. Shell Programming and Scripting

Bash array manipulation

seeking assistance on comparing two arrays using bash: array1=(disk1, disk2, disk3, disk5, disk7, vol1, vol2, vol3, vol4, vol5) array2=(disk2, disk5 vol2, vol4 ) 1) if two arrays have same elements; EXIT else populate array3 & array4 with elements that are different between array1 & array2 as:... (3 Replies)
Discussion started by: solaix14
3 Replies

8. Shell Programming and Scripting

Array in Bash!

sorry I am new to the bash scripting since I was used to c shell programming. I am writting a script in bash for which I have to create an array in bash. I was able to do parse one variable through command line by doing": Lets say the below code is part of temp.bash and is called like: temp.bash... (5 Replies)
Discussion started by: dixits
5 Replies

9. Shell Programming and Scripting

bash array problem

hi friends., i have two files yy.dat and mm.dat containing 110 elements in each if i read them into variables it is just showing only 4 elements instead of 110 elements My script is like this ################################## /bin/bash declare -a yy=(`cat yy.dat`) echo "No of values in... (1 Reply)
Discussion started by: yagnesh
1 Replies

10. Shell Programming and Scripting

bash array

I'm trying to move files based on a certain day..my question is on array's, what's wrong with the way I'm stepping through the array? what i have gives me syntax errors..any ideas...so far I have #! /bin/bash sun_d=`cal |grep -v |awk '{printf " " $1}' ` echo "$sun_d" ls -l... (1 Reply)
Discussion started by: gubten
1 Replies
Login or Register to Ask a Question