arrays and substitutions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting arrays and substitutions
# 1  
Old 01-06-2011
arrays and substitutions

I am working on a bash script and ran around this issue. here's the code :

Code:
#!/bin/bash

string="\"bin\" \"barn\" \"bin, barn /\""
array=($string)
echo -e "\nMethod 1\narray[2] is ---> ${array[2]}"
echo -e "array=($string)"

array=("bin" "barn" "bin, barn /")
echo -e "\nMethod 2\narray[2] is ---> ${array[2]}"
echo -e 'array=("bin" "barn" "bin, barn /")'"\n"
exit

I would suspect that the output of array[2] should be identical for both methods but they are not. Here is the output :
Quote:
Method 1
array[2] is ---> "bin,
array=("bin" "barn" "bin, barn /")

Method 2
array[2] is ---> bin, barn /
array=("bin" "barn" "bin, barn /")
Thanks for your feedback.
# 2  
Old 01-06-2011
It's to do with the order the shell performs expansions.

word splitting is done after variable expansion.

You could eval the line to get a 2nd bite of the cherry
Code:
eval array=($string)

Or change IFS (I like this solution because it looks a lot neater and avoids possible hyjacks):
Code:
string="bin|barn|bin, barn"
IFS="|" array=($string)

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 01-07-2011
Here is another approach to arrays
Code:
declare -a testarry
testarry=("11"  "22"  "33" "44")
limit="`echo ${#testarry[@]}`" 
for ((i=0;i<=$limit;i++))
do 
echo ${testarry[$i]}
done

output:
Code:
11
22
33
44

OR as in your case simply :
Code:
declare -a testarry
testarry=("bin"  "barn"  "bin" "barn")
limit="`echo ${#testarry[@]}`"  
echo ${testarry[2]}

Keep in mind array count starts from 0.

hope it helps.
# 4  
Old 01-07-2011
Quote:
Originally Posted by Chubler_XL
[..]
Or change IFS (I like this solution because it looks a lot neater and avoids possible hyjacks):
Code:
string="bin|barn|bin, barn"
IFS="|" array=($string)

It is usually a good idea to save IFS before and reset it to its old value afterwards.
Code:
oldIFS=$IFS
IFS=... var=...... # some variable assignment with the other IFS
IFS=$oldIFS


Last edited by Scrutinizer; 01-07-2011 at 03:16 AM..
# 5  
Old 01-07-2011
Thanks, the IFS approach works perfectly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Speeding up substitutions

Hi all, I have a lookup table from which I am looking up values (from col1) and replacing them by corresponding values (from col2) in another file. lookup file a,b c,d So just replace a by b, and replace c by d. mainfile a,fvvgeggsegg,dvs a,fgeggefddddddddddg... (7 Replies)
Discussion started by: senhia83
7 Replies

2. UNIX for Dummies Questions & Answers

Multiple substitutions in one expression using sed

Hi, I'm trying to get multiple substitutions in one expression using sed: echo "-foo-_-bar--foo-_bar_-_foo_bar_-foo_-_bar_-" | sed -e "s//-/g" So, as you can see I'm trying to replace all instances of _-, -_, -- with - (dash) I have provided bad example. The question is how to use multiple... (6 Replies)
Discussion started by: useretail
6 Replies

3. Shell Programming and Scripting

How can I write nested command substitutions?

Hello How can write the nested command substitutions? echo `expr substr $x 1 expr ${#x} - 1` the above code is not working! Thanks in advance Regards Chetanz (5 Replies)
Discussion started by: Chetanz
5 Replies

4. Shell Programming and Scripting

Two substitutions in one echo

PHOST1=temp i=1 I want to display the value of PHOST1 by making use of variable i inplace of 1 something like this echo "$PHOST$i" # -> This doesn't seem to work. Please provide me the correct syntax. I tried many different ways echo ${PHOST${i}} echo ${PHOST Nothing seems... (6 Replies)
Discussion started by: blazer789
6 Replies

5. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

6. Shell Programming and Scripting

Multiple variable substitutions

Is there anyway to accomplish this? (ksh) FILES_TO_PROCESS='NAME1 NAME2' SOURCE_NAME1=/tmp/myfile TARGET_NAME1=/somewhere/else # other file names for i in $FILES_TO_PROCESS do file1=SOURCE_$i file2=TARGET_$i echo cp ${$file1} ${$file2} <-- how do get this to work. done (2 Replies)
Discussion started by: koondog
2 Replies

7. Shell Programming and Scripting

Perl - nested substitutions

How can I nest substitutions ? My solution just seems cheap ... sample data Cisco Catalyst Operating System Software, Version 235.5(18) Cisco Catalyst Operating System Software, Version 17.6(7) Cisco Catalyst Operating System Software, Version 19.6(7) Cisco Catalyst Operating System... (1 Reply)
Discussion started by: popeye
1 Replies

8. Shell Programming and Scripting

Using Sed to perform multiple substitutions?

Hello I have the following output which is returned with the Month in text format instead of numerical. The output I receive is performed by using Rational Synergy CM software commands from the Unix command line and piping Unix commands on the end. bash-3.00$ ccm query -n... (4 Replies)
Discussion started by: Glyn_Mo
4 Replies

9. Shell Programming and Scripting

Return Number of Substitutions made by SED?

Hi guys, Is there any way this can be done, or return whether any substitutions have been made? thanks for any input. skinnygav (using Bash shell) (2 Replies)
Discussion started by: skinnygav
2 Replies

10. UNIX for Dummies Questions & Answers

String substitutions in ASCII files -

We need to scramble data in a number of ASCII files. Some of these files are extremely large (1.2 GB). By scrambling, I mean that we need to substitute certain strings, which number around 400, with scrambled strings. An example has been given below If "London" occurs in the file, then it... (2 Replies)
Discussion started by: SanjivNagraj
2 Replies
Login or Register to Ask a Question