LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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
 
Search this Thread
Old 07-17-2008, 03:19 AM   #1
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 262

Rep: Reputation: 23
optional exit from loop, shell script


here is an easier version of what i want to do

Code:
#!/bin/bash
echo "Enter file name"
echo "You can enter maximum 3 file"

for (( i=1; i<=3;i++))
until [ "$*" = "" ]
do
 echo "Enter file ${i}"
 read fil_$i
 done
//do the rest of the code
the probleam is, my input needs three input. but what i want to do is to put an optin of optional break, say instead of three, i have only one input ...so, if i put "0", the code should ixit for loop and do the rest of the things.
how can i do that?
 
Old 07-17-2008, 03:56 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Check the syntax: to put other statements between for and do is not permitted. Based on your requirements, you have to simply check if the input is equal to 0 and eventually break the loop:
Code:
#!/bin/bash

echo "Enter file name"
echo "You can enter maximum 3 file"

for (( i=1; i<=3; i++ ))
do
  read -p "Enter file ${i}: " tmpfile
  if [ "$tmpfile" = "0" ]
  then
    break
  else
    eval "file_$i=$tmpfile"
  fi
done
echo file_1 is $file_1
echo file_2 is $file_2
echo file_3 id $file_3
As you can see, in this example I used a temporary variable "tmpfile" to read the user's input. Then based on the condition you can either exit the loop using the break statement or assign the value of tmpfile to a variable.

The assigment is done using indirect reference: the variable name contains the value of another variable ($1) so you have to use the eval statement to let the shell expand the variable name and then execute the command. See the Advanced Bash Scripting Guide, here for more details.
 
Old 07-17-2008, 04:30 AM   #3
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 262

Original Poster
Rep: Reputation: 23
thnkx colucix
its just what i was looking for
 
  


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
what is the script which runs at the exit bash shell supersubu123 Linux - General 6 01-24-2007 01:25 PM
howto execute for loop on remote mach using shell script dsids Linux - Newbie 1 08-22-2006 04:07 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 03:34 PM
shell script - while loop with multiple conditions ronsha Programming 13 12-10-2005 05:08 PM
Problem with simple shell script for loop abefroman Programming 2 10-25-2005 09:26 PM


All times are GMT -5. The time now is 08:59 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration