LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 06: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, 06:45 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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, 06:56 AM   #3
basildon
LQ Newbie
 
Registered: Sep 2008
Posts: 12

Original Poster
Rep: Reputation: 0
Many thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 08:33 PM
array in bash ramesh_manu Linux - Newbie 1 02-24-2007 12:19 PM
Array Help in BASH! ?*%$ johnnybhoy67 Linux - Software 2 02-22-2006 11:39 AM
Problem displaying Bash array values. shinobi59 Programming 3 01-17-2006 06:45 PM
MAJOR problem ... bash script array HELP !!!!! michael_util Slackware 1 02-13-2004 07:51 AM


All times are GMT -5. The time now is 08:16 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration