LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-23-2009, 12:43 PM   #1
dnvikram
LQ Newbie
 
Registered: Dec 2005
Posts: 7

Rep: Reputation: 0
Shell Script skipping a loop


I have a function in a script of mine which is supposed to read a list of email addresses from a file called,mail.lst and display them as a menu and prompt the user to select a desired email address by choosing the corresponding number beside it in the menu it shows.The problem is,somehow the script is skipping this and then proceeds to the next statement.Hence,its not displaying that list of email addresses in the file as a menu to select one of them.I am pasting the function here and also the desired output:

FUNCTION:
##########################################
ck_mail_list()
{
i=1
cat Mail/mail.lst | while read LINE
do
if [ `expr "$LINE" : '#'` -ge 1 ]; then
continue
else
address[$i]="$LINE"
i=`expr $i + 1`
fi
done
print -n "Please choose one of the number in front of the address in the following, where\n"
print -n "you want the log file to be sent to. \n"
print -n " \n"
print -n "0) Eneter you own email address;\n"
j=1
while [ $j -lt $i ]
do
print -n "$j) ${address[$j]}; \n"
j=`expr $j + 1`
done
read num_chosen
if [ $num_chosen = 0 ]; then
print -n "Please enter the email address that you want the log file will be sent to: \n"
read email_addr
else
max_choices=`expr $j - 1`
if [ ${num_chosen} -gt ${max_choices} ]; then
print -n "You have picked a wrong choice, we are going to exit. \n"
exit
fi
email_addr=${address[$num_chosen]}
fi
}
#############################################



DESIRED OUTPUT:
Please choose one of the number in front of the address in the following, where

you want the log file to be sent to.

0) Eneter you own email address;

1) appdba@xyz.com;

2) app2@xyz.com;

3) app3@xyz.com;

4) app4@xyz.com;

5) app5@xyz.com;

4

Please enter the change reference code for this change:
####################################################

The above should be what I want to see.But,instead It displays the below:
Please choose one of the number in front of the address in the following, where

you want the log file to be sent to.

0) Enter you own email address;
#####################################################

Any help would be appreciated.If anyone of you could point out as what is the mistake in the script or if I am missing something.

Thanks in advance.
 
Old 01-23-2009, 01:11 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 1,421

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Code:
 cat Mail/mail.lst | while read LINE
This is a common problem, when you use a pipe (the "|") the loop gets executed in a new process so setting variables inside the loop doesn't affect variables outside. Instead you should write
Code:
while read LINE
do
    ...
done < Mail/mail.lst
A few other simplifications:
Code:
if [ `expr "$LINE" : '#'` -ge 1 ]; then
# same as
if expr "$LINE" : '#'; then
Code:
i=`expr $i + 1`
#same as
let i+=1
#or
((i+=1))
Code:
[ $num_chosen = 0 ]
#should be
[ $num_chosen -eq 0 ]
 
Old 01-23-2009, 02:29 PM   #3
dnvikram
LQ Newbie
 
Registered: Dec 2005
Posts: 7

Original Poster
Rep: Reputation: 0
Re:Script skipping loop

Thanks a lot My Fellow Member!
That small modification fixed the problem.Thanks for taking the time out and looking into it.Appreciate it.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop in Shell Script delamatrix Programming 4 07-24-2008 05:20 PM
optional exit from loop, shell script advanced Programming 2 07-17-2008 03:30 AM
shell script loop though text file:-1 line at a time. knockout_artist Linux - Newbie 2 05-04-2008 06:58 PM
shell script - while loop with multiple conditions ronsha Programming 13 12-10-2005 04:08 PM
Problem with simple shell script for loop abefroman Programming 2 10-25-2005 08:26 PM


All times are GMT -5. The time now is 03:57 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration