Help With Loop in Case Statement script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help With Loop in Case Statement script
# 1  
Old 04-22-2011
Help With Loop in Case Statement script

I am writing a bash script that asks the user for input and I need it to repeat until the user selects quit.. I dont know how to write the loop for it I searched all over but i still do not get it.. if anyone could help with this it would be greatly apprciated here is my script so far:

Code:
#! /bin/bash

if [[ $@ == "username" ]]
then
	echo "Hello, $@!"
	echo "The current directory is $PWD"
	echo "The home directory is $HOME"
else
	echo "Hello Stranger"
fi

echo "Would you like to:"
echo "1) See your name"
echo "2) See your current directory"
echo "3) See your home directory"
echo "4) Quit"
read case;

case $case in 
1) echo "username";;
2) echo "$PWD";;
3) echo "$HOME";;
4) echo "Quit";;

esac 

rep="$case" 

#THIS IS WHAT I DONT GET: it just runs this command like 5 times but does not let me choose a option 

while (( $rep <=1 )) 
do 
echo "Would you like to:"
echo "1) See your name"
echo "2) See your current directory"
echo "3) See your home directory"
echo "4) Quit"
rep=$(( rep+1))
done

I really need help figuring this out

Last edited by pludi; 04-22-2011 at 03:13 AM..
# 2  
Old 04-22-2011
Try this:

Code:
while true
do
    echo "Would you like to:"
    echo "1) See your name"
    echo "2) See your current directory"
    echo "3) See your home directory"
    echo "4) Quit"
    echo -n ">> "
    read case

    case "$case" in
        1) echo "username";;
        2) echo "$PWD";;
        3) echo "$HOME";;
        4) break;;
    esac
done

echo "Quit"

This User Gave Thanks to hergp For This Post:
# 3  
Old 04-22-2011
hergp Thank you so much man this really helped me out Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

2. Windows & DOS: Issues & Discussions

Loop statement within a batch script

I have to create a couple of files within a directory structure. Filename stays the same, but at the end of each file, the number appears. Example: File needs to be created within directory c:\temp File Name will stay the same (testfile), but extension will increase, starting at 01 - example... (1 Reply)
Discussion started by: HenkTrumpie
1 Replies

3. Shell Programming and Scripting

Case statement in UNIX shell script

have written the below code to check whether the string received from user is a file name or dir using case statement, but its going into default case*). #!/bin/sh #Get a string from user and check whether its a existing filename or not rm str2 rm str3 echo "enter a file \c" read fil... (8 Replies)
Discussion started by: Mohan0509
8 Replies

4. Shell Programming and Scripting

Case Statement for Login/Logoff Script

I'm currently trying to write a login script. I have already written a logoff script that uses an if/else statement. #!/bin/bash TIMED=$(date +%H) if ' then echo Have a Great Day $USER else echo Have a Great Night $USER fi But I'd like to write one that give me the option of... (1 Reply)
Discussion started by: bbowers
1 Replies

5. Homework & Coursework Questions

Problem with executing a possible if or case statement script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Create a phonebook program. It should use functions to perform the required tasks. It should be menu-based,... (1 Reply)
Discussion started by: Rgasin02
1 Replies

6. Shell Programming and Scripting

Case statement

Hello, The standard case statement :- case "$1" in "IE0263") commands;; "IE0264") commands;; esac is it possible to have :- case "$1" in "IE0263" OR "IE0878") commands;; "IE0264") commands;; esac Thanks (4 Replies)
Discussion started by: jmahal
4 Replies

7. Shell Programming and Scripting

Help with IF statement with loop Shell Script

Hello I am very new to shell and I bought some books and trying to learn it. I started trying to write a script that will take a number and count it down to 1 with commas in between. This number can only be one argument. If lower than one or higher than one argument it sends an error message. ... (4 Replies)
Discussion started by: zero3ree
4 Replies

8. Shell Programming and Scripting

shell script case statement

In a case statement like below : case $rental in "car") echo "For $rental Rs.20 per k/m";; "van") echo "For $rental Rs.10 per k/m";; "jeep") echo "For $rental Rs.5 per k/m";; "bicycle") echo "For $rental 20 paisa per k/m";; *) echo "Sorry, I can not gat a $rental for you";;... (4 Replies)
Discussion started by: sriram003
4 Replies

9. Shell Programming and Scripting

what is problem with this small shell script.. case statement related

Hi All, this small script is written to recognize user input character.. it is in small case .. upeer case or is a number... but when i input first capital letter say A.. it always gives small character.... what is the problem. #!/bin/bash echo "Enter the character" read a case $a in )... (2 Replies)
Discussion started by: johnray31
2 Replies

10. Shell Programming and Scripting

Shell script automation using case statement

Hi, I'm trying to write a shell script that has a menu and then dependant on the selection, will automate some samba file transfer. The problem is when I run the code without the case statement it runs fine. but when I put the case statement in the only way I can get the code to run is to... (6 Replies)
Discussion started by: ianf
6 Replies
Login or Register to Ask a Question