bash-function with array acting bizarre, bug?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash-function with array acting bizarre, bug?
# 1  
Old 01-27-2009
bash-function with array acting bizarre, bug?

Hello,

basically what this script is supposed to do is showing a list of hosts that is given a number, that you will be able to choose from a list.
A check is made to verify that the chosen number is within the array and this is where things go bad and I don't know why, bizarre.

I've spent quite some time trying to figure out why this function doesn't give the correct output (see below the script).

Code:
HOSTLIST="test1 test2 test3"

List_Hosts ()
{
  num=1
  hosts=
  HI_HOSTS=
  hostlist=(NULL $( echo $HOSTLIST ))
  hostnumbers=${#hostlist[@]}
  for (( i=1; i<${hostnumbers}; i++ )); do
    hosts=$( echo "$hosts($num)-${hostlist[$i]} " )
    let num=num+1
  done
  echo -e "\n$hosts\n"
  echo -n "Enter number(s) (comma-separated - 1,2,3): "
  read input
  if [ -n "$( echo "$input" | tr , " " | tr -d '[0-9]' | awk '{print $1}' )" ]; then
    echo -e "\nNone numerical value \"$input\" entered, please try again."
    List_Hosts
  else
    for hostnum in $( echo "$input" | tr , " " ) ; do
      if [ "$hostnum" -eq 0 -o "$hostnum" -gt $[$hostnumbers-1] ]; then
        echo "Number \"$hostnum\" doesn't match existing values, please try again."
        List_Hosts
      fi
      HI_HOSTS=$( echo "$HI_HOSTS${hostlist[$hostnum]} " )
    done
  fi
}
List_Hosts
echo $HI_HOSTS

output
# ./script.sh
(1)-test1 (2)-test2 (3)-test3

Enter number(s) (comma-separated - 1,2,3): 4
Number "4" doesn't match existing values, please try again.

(1)-test1 (2)-test2 (3)-test3

Enter number(s) (comma-separated - 1,2,3): 5
Number "5" doesn't match existing values, please try again.

(1)-test1 (2)-test2 (3)-test3

Enter number(s) (comma-separated - 1,2,3): 1,2
test1 test2 test2 test2

for each unmatched number it will add the last entry again. Running it without typing an unmatched number it will give the correct output.
# 2  
Old 01-27-2009
I think you need to drop this down a line:
Code:
     HI_HOSTS=$( echo "$HI_HOSTS${hostlist[$hostnum]} " )

so its after your for loop. Thing is, that loop doesn't do what you think it should do. You should run through the loop and if ANY of the hosts don't match, report an error and try again. OTHERWISE, you should add HI_HOSTS.
# 3  
Old 01-28-2009
thanks for the reply.

i will need some sort of for loop to get the input of numbers
connected with the strings in the array. if I do this loop after
a pre-check it still gives me the same problem? Smilie

Code:
    for hostnum in $( echo "$input" | tr , " " ) ; do
      if [ "$hostnum" -eq 0 -o "$hostnum" -gt $[$hostnumbers-1] ]; then
        echo "Number \"$hostnum\" doesn't match existing values, please try again."
        List_Hosts
      fi
    done
    for hostnum in $( echo "$input" | tr , " " ) ; do
      HI_HOSTS=$( echo "$HI_HOSTS${hostlist[$hostnum]} " )
    done

# 4  
Old 01-28-2009
Okay, I see. Your error-recovery is to recurse. BAD IDEA. When the code detects a bad input, it should re-start a loop, NOT re-call the function. In re-calling the function, you do not terminate the loop, so after the re-call completes, the first call of the function continues right there and adds to HI_HOSTS again.

Welcome to the pitfalls of global variables and recursion.

It should be like this:
Code:
# pseudo code
valid-input=false
while ! valid-input
  prompt
  get input
  for each hostnum
     if hostnum is invalid
        invalid-hosnum=TRUE
        echo "hostnum doesn't match"
     else
        hosts=$hosts + $hostlist[ hostnum ]
   if invalid-hostnum 
      echo Please try again
   else
      valid-input=TRUE
return $hosts

# 5  
Old 01-29-2009
thanks otheus for showing me the way.

i've changed the error checking the way you pointed out and now it works as expected.
I never knew about this problem with loops and calling functions...
...you learn something every day Smilie
# 6  
Old 01-29-2009
Quote:
Originally Posted by gand
thanks otheus for showing me the way.
*blush* Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

Small bug in the Quick Editor function in postbit

Hey, There was a small bug in the Quick Editor function in postbit, but I fixed it (basically a double quote was missing from an element id): <div id="post_message_$post" class="neo-message-area">$post</div> Was <div id="post_message_$post class="neo-message-area">$post</div> Should... (1 Reply)
Discussion started by: Neo
1 Replies

2. Shell Programming and Scripting

Bash script acting funny when run from Crontab

Hello, first time here. I have a script that seems to ignore the if statement when run from the cron. I am using Ubuntu 12.10 #!/bin/bash DOWN=/usr/sbin/dcon UP="pon dsl-provider" LOG=/var/log/dsl-reconnect.log RECV=`ifconfig 2>&1|grep ppp0|cut -d , -f 5|cut -d " " -f 1` if ] then... (1 Reply)
Discussion started by: mkoster
1 Replies

3. Programming

Tweaked getpass() function gives an untraceable bug

I have customized the getpass() as follows: char* my_getpass(const char* str) { struct termios oflags, nflags; static char passwd; /* disabling echo */ tcgetattr(fileno(stdin), &oflags); nflags = oflags; nflags.c_lflag &= ~ECHO; nflags.c_lflag |= ECHONL; ... (3 Replies)
Discussion started by: royalibrahim
3 Replies

4. Shell Programming and Scripting

Sort function UNIX bug ???

Hello there i have a funny behiavor of the sort fonction, i try it out on different Solaris machine and i have the same issue. So i would like to see if there is a rationel explanation here is some data in a file:test.txt ,Test,RSD,RSD_Asset ,Test,RSD,RSD_Credit ,Test,RSD,RSD_Liab... (3 Replies)
Discussion started by: kykyboss
3 Replies

5. Shell Programming and Scripting

BASH weird acting: unquoted parameter accepted as quoted one !

In one session I have strange behavior of the bash-shell: SDX-Q> echo ">$ss<" #this is just to present the $ss var: > lll kkk < SDX-Q> # more obviose: SDX-Q> od -cb <<<"$ss" 0000000 l l l k k k \n 040 040 040 154 154 154 040... (3 Replies)
Discussion started by: alex_5161
3 Replies

6. Shell Programming and Scripting

Bug in Function Call

Can anybody tell me where is the bug in this below mentioned function call. #The String Search File myString="${LOCATION}/config/stringFile.txt" # Functional Usage function usage() { if ; then echo "************************************************************" ... (5 Replies)
Discussion started by: baraghun
5 Replies

7. Shell Programming and Scripting

[bash] passing array to function

Hi, I'm trying to write a function that reassigns an array to another local array but the method used in reassigning the array reformats the contents of the array which is what I am trying to prevent. The method used to load a file into an array works as expected and the entire array is... (4 Replies)
Discussion started by: ASGR
4 Replies

8. Shell Programming and Scripting

Array in loop is acting up

Hello! I have a question about loops and arrays. I'm trying to go through this: for aa in 01 02 03 OrigNum=$(grep ${Orig} Ba3In2F12.prepos | wc -l) OrigNum=$((${OrigNum} - 1)) echo ${OrigNum} etc It gets stuck on the second line. The error reads: ./asdf: line 30:... (5 Replies)
Discussion started by: RisingSun
5 Replies

9. Shell Programming and Scripting

[BASH - KSH] Passing array to a function

Passing a array to a function, a basic feature in modern language, seems to be only possible in KSH. Not in BASH. Depite all my efforts I couldn't come to a solution. See the following examples: It works perfectly in KSH: #!/usr/bin/ksh function print_array { # assign array by indirect... (3 Replies)
Discussion started by: ripat
3 Replies

10. Shell Programming and Scripting

Function Bug in script - need help

My script is erroring with: testtapemgr.sh: FTP_RETURNS: not found I cannot see what I am doing wrong..when it calls that function from the Volume returns function and says taht FTP_RETURNS is not found and exits out of the script. What am I not seeing here? #### Return Volume Function ... (4 Replies)
Discussion started by: gzs553
4 Replies
Login or Register to Ask a Question