LinuxQuestions.org
Review your favorite Linux distribution.
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-24-2008, 12:48 PM   #1
delamatrix
Member
 
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")

tfile="prosolv-$tdydte.txt"

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 ]
do
echo "Please Scan your item:"
read theitem2
echo $theitem2 >> ~/misc/$tfile
done

Thanks in advance

-Delamatrix
 
Old 07-24-2008, 01:09 PM   #2
pixellany
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**:
Code:
if [ $t −lt 5 ]
then
  return 0 # true
else
  return 1 # false
fi
In place of the test ([...]), substitute your test, and in place of the return statements, you would substitute the desired commands.

**Available at http://tldp.org, but I would start with Bash Guide for Beginners (same site)
 
Old 07-24-2008, 01:44 PM   #3
delamatrix
Member
 
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]"

-Delamatrix
 
Old 07-24-2008, 03:52 PM   #4
jcookeman
Member
 
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:

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

until [ $CONTINUE != y ]
do
     handle_item
     read -p "Would you like to scan another item? [y/n] " CONTINUE
done
 
Old 07-24-2008, 06:20 PM   #5
delamatrix
Member
 
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.

-Delamatrix
 
  


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
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

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

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