LinuxQuestions.org
Review your favorite Linux distribution.
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 12-12-2008, 08:23 AM   #1
grishaoks
LQ Newbie
 
Registered: Dec 2008
Posts: 7

Rep: Reputation: 0
bash - do a "while" instead of "for" on an array


Hello,

I know that if I have an array and I want to go over all the elements in the array I can do something like this:

Code:
ELEMENTS=${#ARRAY[@]}

for (( i=0;i<$ELEMENTS;i++)); do
   echo ${ARRAY[${i}]}
done
Now I have 3 arrays:

Code:
ELEMENTS1=${#arr1[@]}
ELEMENTS2=${#arr2[@]}
ELEMENTS3=${#arr3[@]}
but instead of a "for" loop, I want to use "While". I am trying to do something like this:

Code:
i=0
k=0
j=0
while [ i<$ELEMENTS1 && j<$ELEMENTS2 && k<$ELEMENTS3 ]; do
But it gives me error in the While line that there is no such file or directory. What am I doing wrong?

Thanks in advance,

Greg

The full code is below:


Code:
let tmp=1
ELEMENTS1=${#arr1[@]}
ELEMENTS2=${#arr2[@]}
ELEMENTS3=${#arr3[@]}
i=0
k=0
j=0
while [ i<$ELEMENTS1 && j<$ELEMENTS2 && k<$ELEMENTS3 ]; do
  echo -n $tmp")"
  if [[ ${arr1[${i}]} != NULL ]];
  then
    echo -n ${arr1[${i}]}
    let i++
  fi
  if [[ ${arr2[${j}]} != NULL ]];
  then
    echo -n ${arr2[${j}]}
    let j++
  fi  
  if [[ ${arr3[${k}]} != NULL ]];
  then
    echo ${arr3[${k}]}
    let k++
  fi
  let tmp++
done
 
Old 12-12-2008, 12:52 PM   #2
Unmindfulawe
Member
 
Registered: Aug 2005
Location: WI
Distribution: openSUSE 11.1/ TinyMe
Posts: 51

Rep: Reputation: 16
I this what you are trying to do?

Whenever you use the < operator you must enclose it within [[ ]] otherwise it thinks it is the command in linux to input to a specific file. Whenever you are increasing using ++ it must be enclosed in (( )).
otherwise you will receive a command not found error. I am very new to scripting myself and have found this link to be very helpful.

http://tldp.org/LDP/abs/html/

I modified your code for testing purposes. But I hope this helps.

Code:
let tmp=1
arr1=(1 2 3 4 5 6)
arr2=(A B C D E F G H I)
arr3=(L I N U X S C R I P T )

 ELEMENTS1=(1 2 3 4 5 6)
 ELEMENTS2=(1 2 3 4 5 6 7 8 9)
 ELEMENTS3=(1 2 3 4 5 6 7 8 9 10 11 12)
 i=0
 j=0
 k=0

while [[ $i < ${ELEMENTS1[${i}]} && $j < ${ELEMENTS2[${j}]} && $k < ${ELEMENTS3[${k}]} ]]; do
  echo -n $tmp")"
  if [[ ${arr1[${i}]} != NULL ]];
  	then
		echo -n ${arr1[${i}]};
		((i++))
  fi

  if [[ ${arr2[${j}]} != NULL ]];
	then
		echo -n ${arr2[${j}]}
		((j++))

  fi
	
  if [[ ${arr3[${k}]} != NULL ]];
  	then
   		echo ${arr3[${k}]}
		((k++))
  fi
((tmp++))
done
 
Old 12-13-2008, 05:29 AM   #3
grishaoks
LQ Newbie
 
Registered: Dec 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Does the code you modified work? Because it doesnt print anything on the screen so I dont think it enters the "While" loop even once...
 
Old 12-14-2008, 12:52 AM   #4
Unmindfulawe
Member
 
Registered: Aug 2005
Location: WI
Distribution: openSUSE 11.1/ TinyMe
Posts: 51

Rep: Reputation: 16
Well, when I run the script in the console this is the result of it.

1)1AL
2)2BI
3)3CN
4)4DU
5)5EX
6)6FS

So, the while loop lasts 6 times as $ELEMENTS1 has 6 numbers to iterate.
 
  


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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
bash-2.05b# Xlib: extension "XFree86-DRI" missing on display ":0.0". citrus Linux - General 8 02-22-2004 10:43 AM

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

All times are GMT -5. The time now is 02:55 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
Open Source Consulting | Domain Registration