Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-24-2008, 12:48 PM   #1
Registered: Nov 2004
Posts: 43

Rep: Reputation: 15
Loop in Shell Script

I'm trying to write a simple shell script that

1.) prompt for input
2.) write the input to a file
3.) then ask the user if he/she wants to input more data
4.) If user selects 'y', then continue to prompt for input
5.) If user selects 'no', then exit script

Below is what I got so far:

tdydte=$(date +"%b-%d-%y")


echo "Please Scan Item:"
read theitem
echo $theitem >> ~/misc/$tfile

echo "Would you like to scan another item [y/n]?"
read yourch2

until [ $yourch2 = n ]
echo "Please Scan your item:"
read theitem2
echo $theitem2 >> ~/misc/$tfile

Thanks in advance

Old 07-24-2008, 01:09 PM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I'm not immediately seeing what your question is. Does the existing code work? If not, what does not work correctly?

Eventually, it looks like you need the if / else construct. Here is an example, copied from the Advanced Bash Scripting Guide**:
if [ $t −lt 5 ]
  return 0 # true
  return 1 # false
In place of the test ([...]), substitute your test, and in place of the return statements, you would substitute the desired commands.

**Available at, but I would start with Bash Guide for Beginners (same site)
Old 07-24-2008, 01:44 PM   #3
Registered: Nov 2004
Posts: 43

Original Poster
Rep: Reputation: 15
The script works up to the point where the user is prompted to scan an item after they choose 'y'. After that, it just keeps looping at "Please Scan your item:". Instead of looping back to "Would you like to scan another item? [y/n]"

Old 07-24-2008, 03:52 PM   #4
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
Of course it won't work that way. You have a loop construct that doesn't contain your prompt for user input on that particular question. So, it will never return to that question. With a little clever use of flow control and functions you can do this easily:

#!/bin/env bash
FILE="~/misc/prosolv-`date +%b-%d-%y`"
     read -p "Please Scan Item: " ITEM
     echo $ITEM >> $FILE

until [ $CONTINUE != y ]
     read -p "Would you like to scan another item? [y/n] " CONTINUE
Old 07-24-2008, 06:20 PM   #5
Registered: Nov 2004
Posts: 43

Original Poster
Rep: Reputation: 15
Thanks a lot for all the input guys! I originally was going to try to use functions, but couldn't figure it out. The script works like a charm now.



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
optional exit from loop, shell script RudraB Programming 2 07-17-2008 04:30 AM
shell script loop though text file:-1 line at a time. knockout_artist Linux - Newbie 2 05-04-2008 07:58 PM
howto execute for loop on remote mach using shell script dsids Linux - Newbie 1 08-22-2006 04:07 AM
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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:54 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration