Register
It is currently Fri Oct 24, 2014 10:53 am

echo-ing arrays eratic


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Mon Jul 20, 2009 9:25 pm   

Joined: Mon Jul 20, 2009 4:42 pm
Posts: 2
Hello fellow Bashers. I am new to scripting, but am eager to learn.

I have the following code:

Code:
#! /bin/bash

i=1
ls | while read line
do
    arrayA[ $i ]="$line"
    echo "arrayA = ${arrayA[$i]}"
    (( i++ ))
done

echo ${arrayA[3]}



This just (for testing purposes now) echos the file listing in the current directory.

The first problem I have is that the echo in the do while loop works fine, but the last line echos a blank line. What am I missing?

My second issue is how do I modify the text in each array? I was trying to use:
Code:
arrayB[$i]=$line | sed s/foo//
but to no avail.


Top
 Profile  
 PostPosted: Tue Jul 21, 2009 12:59 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
First of all we can start with modifying text in the variable.

Easiest way is to put in that way obviously. This never works as planned thou so you can use execution in variables to solve it.
Code:
#!/bin/bash
string="This is a string"
output=$(echo $string |sed -e "s/is/as/")
echo $output


The other part I'm not really sure why it shouldn't work.
But if you really want to do that I'd suggest something like this
Code:
#!/bin/bash
path=$1 # This just takes input parameter 1 and assigns it to $path
x=0
for i in $(ls $path --color=no); do
   arrayA[$x]=$i
   (( x++ ))
done

echo ${arrayA[3]}


Instead of ls you can also use absolute paths, /home/sajko/* will work the same as doing ls /home/sajko.
Also why I add --color=no is because color coded output will screw with the result in the for loop, it happens to me way to often (even thou no one else seems to have seen it before).

Hope this gives you some clues :)

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Tue Jul 21, 2009 3:56 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
weird things happen with bash shells when you use a construct like so;
Code:
command | while read var
do
  stuff
done


I can't say I can fully explain (:~ it but basically do not use that type of structure if you can avoid it.
The 'for' loop the Fredrik suggests is safer :)


Top
 Profile  
 PostPosted: Tue Jul 21, 2009 7:01 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Well actually it isn't... Wierd things happens when whitespace gets involved in for loops :P.
A file listing like this
Code:
-rw-r--r--  1 sajko users   88 2009-07-18 15:58 tmp 1.php
-rw-r--r--  1 sajko users 1014 2009-04-23 23:05 tmp.sh
-rw-r--r--  1 sajko users  531 2009-07-15 08:41 tmp.txt
-rw-r--r--  1 sajko users  266 2009-04-15 15:22 tst.sh

would make a for loop go abit crazy.

This can be remedied by not using long listing. In which case it would look like this
Code:
tmp 1.php 
tmp.sh 
tmp.txt 
tst.sh


This would present another issue thou. Since there's a space in the file name this would make itterations look like this
Code:
tmp
1.php 
tmp.sh 
...

where neither 1.php or tmp exists (or even worse, they do exist).

The solution to this problem is the IFS environment variable.
Code:
IFS="
"

Will change the itteration separator to a new line instead of any whitespace character (newline, space or tab) making it usable when a space character is in the file name, or when you parse columned files.

There are several other things that gets abit wacky but you'll figure them out while you get along with your bash programming.
Recommended thou is that you learn to handle the bash commands for stream editing, programs like sed, cut, awk, replace, tr and so on :)

ps. Using the absolute path (as described in my previous post) instead of running a command to populate your for loop condition does not present the above problem. The shell automagically escapes characters that could pose a problem as part of the expanding of the *. Also you'll get the full path and then file name which ls won't give you. You can use "basename" to remove the absolute path ds.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Tue Jul 21, 2009 5:27 pm   

Joined: Mon Jul 20, 2009 4:42 pm
Posts: 2
Wow, thanks guys for your help. I am unable to apply this today, hopefully tomorrow.

B-)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP