LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash infinite loop problem? (http://www.linuxquestions.org/questions/linux-newbie-8/bash-infinite-loop-problem-795293/)

stp001 03-14-2010 05:38 AM

Bash infinite loop problem?
 
Hi guys ,
I am writing a bash that's quite simple ,having zenity dialog boxes.
I seem to have a problem with the infinite loop.
as soon as it reaches the loops , the script exits without entering the loop.
I couldn't find concrete examples of inner infinite loops in Bash.

here's the loop:

for param in $ans
do
while [ 1 ]
do

test_pram= $(zenity --entry --text "Please \ enter $param for the webpage")
if [ $? -eq 0 ] || [ -n "$test_pram" ] ; then
break
fi
done
done

Please help
Thanks

GrapefruiTgirl 03-14-2010 05:43 AM

Hiya! Welcome to LQ.

First things first: Please put code into code tags, so it is more readable, and so that any formatting that might be present, will be preserved. Here's how to use code tags: http://www.phpbb.com/community/faq.php?mode=bbcode#f2r1

Now.... Unless that's a typo, there is a SPACE between the test_pram= and the $(zenity.. thing, which will mean that the variable will always be empty, and your zenity ccommand, if it works, will just execute and return nothing useful to the script.

If there's more than that wrong, I don't see it at this moment -- I'm tired, and going to sleep soon ;)

Cheers,
Sasha

PS -- instead of the while true; do that you are using, how about something more like:
Code:

while [ ! "$test_pram" ]; do
which will repeat the loop until $test_pram has some contents?

jamescondron 03-14-2010 05:45 AM

Well ideally sub 'while[1]' for 'while true', but what is in $ans? And why does that need an infinite loop? It'll only ever loop over the first element in $ans.

stp001 03-14-2010 05:56 AM

Thanks
 
Thanks for your help I'll try to figure out

jamescondron 03-14-2010 09:14 AM

If you give us your whole script we can give you a much better answer

stp001 03-15-2010 05:35 AM

The main idea of this script is that you collect a string of values from a zenity checklist and then use the zenity entry
dialogbox
to type these parameters that you latter on embed in an Html page.I want the entry dialogbox to stay there unless you type something and click OK.Any advance help or references on how to best use dialog boxes will be appreciated.The problem is that there are no modal dialog boxes in zenity which makes the task difficult.

Code:

#!/bin/bash

#set -x
IFS_OLD= $IFS

answers=$(zenity --list  --text "please check all parameters for the Web page" --checklist  --column "Pick" --column "Parameters"  TRUE "title" TRUE "H1" TRUE "para1" TRUE "para2"  TRUE "imageURL" \
 --separator=":") ; echo "$answers"

IFS=':'
arrAns=""
inc=0

for param in  $answers
#while [[ $inc -le ${#parray[@]} ]]
  do

  while true
      do
        test_param= $(zenity --entry --text "Please enter $param  for the webpage")
             
          if [ $? -eq 0 ]  &&  [ "$test_param" != " " ] ; then
          arrAns["$inc"]= $test_param
          inc= $(( inc++ ))
          break
          fi     
    done       
done
       
#echo ${#arrAns[@]}
IFS=OLD_IFS

 zenity --info --text "In the next screen,please select the file to add as preformatted text into your webpage"



myFile= $(zenity --file-selection --title="Select a File")
IFS=$IFS_OLD

echo "<html>

 <head>

<title>"${arrAns[1]}"</title>

</head>

<body bgcolor=$bgColor>

<font color=$fgColor>

<h1>"${arrAns[2]}"</h1>

<p>"${arrAns[3]}"</p>

<p><img src="${arrAns[3]}" /></p>

<p> `cat "$myFile"` </p>

</font>

</body>

  </html>" > ~/public_html/$filename



zenity --info --text "Congrats !! Your Webpage has been created\nType this URL into your browser to confirm:\nhttp://mysite.com/~`$(whoami)`/$filename"
#EOF


jamescondron 03-15-2010 06:56 AM

Well, my take on the loop in question, or more or less. When I execute yours the loop is all very odd.
Code:

#!/usr/bin/env bash

OLD_IFS=$IFS
IFS=":"

answers=$(zenity --list  --text "please check all parameters for the Web page" --checklist  --column "Pick" --column "Parameters"  TRUE "title" TRUE "H1" TRUE "para1" TRUE "para2"  TRUE "imageURL"  --separator=":")

arr_ans=""
inc=0

for param in ${answers[@]}; do
    test_param=""
    # Here, as per 'while test_param == "" or test_param == "some entry that results in err etc"
    # By setting t_p above we can happily use a while loop here without any issue.
    test_param=$(zenity --entry --text "Please enter $param for the webpage")
    arr_ans=( ${arr_ans[@]-} $( echo "$test_param" ) )
done

# And so on



All times are GMT -5. The time now is 07:28 AM.