LinuxQuestions.org
Help answer threads with 0 replies.
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, 02:19 AM   #1
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 264

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, 02:56 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 03:30 AM   #3
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 264

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 12:25 PM
howto execute for loop on remote mach using shell script dsids Linux - Newbie 1 08-22-2006 03:07 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:21 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