LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-09-2008, 05:34 AM   #1
basildon
LQ Newbie
 
Registered: Sep 2008
Posts: 12

Rep: Reputation: 0
Array problem in Bash


Hi

Could anyone explain what I'm doing wrong in the example below. I'm trying to read characters into an array one at a time, but with no success.

Thanks



#!/bin/bash

echo "Enter the filename xx "
read answer
z=$(ls -l $answer);
int=0;

while [ $int -lt 10 ]
do
val=${z:$int:1} ### reads value 1 character at a time
FOO[$int]=$val ### puts value in array
int=$(($int + 1))
echo $val ### This echos value to screen
echo ${FOO[${int}]} ### This doesn't just gives blanks
done
 
Old 09-09-2008, 05:45 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Basically you increment the int variable before echoing the value: you assign the first value then echo the second which is still null, you assign the second and echo the third and so on.
 
Old 09-09-2008, 05:56 AM   #3
basildon
LQ Newbie
 
Registered: Sep 2008
Posts: 12

Original Poster
Rep: Reputation: 0
Many thanks
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with bash array usage jhansonxi Programming 8 07-09-2008 07:33 PM
array in bash ramesh_manu Linux - Newbie 1 02-24-2007 11:19 AM
Array Help in BASH! ?*%$ johnnybhoy67 Linux - Software 2 02-22-2006 10:39 AM
Problem displaying Bash array values. shinobi59 Programming 3 01-17-2006 05:45 PM
MAJOR problem ... bash script array HELP !!!!! michael_util Slackware 1 02-13-2004 06:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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