'Combining 2 while loops together'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 'Combining 2 while loops together'
# 1  
Old 01-30-2009
'Combining 2 while loops together'

I need help with 'combining 2 while loops' together.
The 1st while loop reads an input and checks to see if the input is a blank.
But at the same time, I want to check if the input (if its not blank) is a number or a word. As I am using the case, I cannot use any if loops.

Really appreciate anyone who can help me. Thank you very much.

#!/bin/bash

read Name in
while [ -z "${Name}" ] #Ensure that there are no blank inputs
do
echo "You cannot input a blank name. Enter a valid name"
read Name
done

read Check in
while [ `echo $Check | grep "[^A-Z]"` ] #Check that user types in character from A-Z only
do
echo "Invalid input. Please enter characters from A-Z";
read Check;
done
# 2  
Old 01-30-2009
What should it do if its a number or word? That affects how one might check and where.

Also, why do you do this:
Code:
read Something in

That tries to read into two variables, 'Something' and 'in', but 'in' never gets used. You can leave it off, the statement will work without it.

Also, please use code tags for code. Quote my post to see how.
# 3  
Old 01-30-2009
Quote:
Originally Posted by Corona688
What should it do if its a number or word? That affects how one might check and where.

Also, why do you do this:
Code:
read Something in

That tries to read into two variables, 'Something' and 'in', but 'in' never gets used. You can leave it off, the statement will work without it.

Also, please use code tags for code. Quote my post to see how.
Sorry, I'm new to this forum. Will use the code tag in future.

The input should be a word, not a number. If the input is a number, it will keep looping and echoing an invalid statement until the input is a word.
The problem I am facing is, the first loop is working, I cannot input a blank, but I can still input numbers.

I am thinking of something like this
Code:
read Name
while [ -z "${Name}" |`echo $Name | grep  "[^A-Z]"`]
do
echo "You cannot input a blank or numeric name."
read Name
done

Yes, and I realised that the 'in' does not affect anything. Will remove it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Help with loops?

I'm trying to understand better the while and until loops, can someone help me with this example? #!/bin/bash # Listing the planets. for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto do echo $planet # Each planet on a separate line. done echo; echo for... (3 Replies)
Discussion started by: jose2802
3 Replies

2. Shell Programming and Scripting

Too many for loops

My apologies for the long post.... I have a structure problem with my ksh script on linux. right now it does what i want (which i know is the point) but its really really ugly and i know there must be a simpler way to achieve my task. The data is a list of usernames. This list will help... (2 Replies)
Discussion started by: maverick72
2 Replies

3. Shell Programming and Scripting

Two Loops

Hi please help. I have a file with two columns. I want to insert the value of the first column and second column in different sections of a line. code: for line in `cat $1`; for x in `cat $1 |awk '{print $2}'`; do do echo "print $line and then print $x" done done It works... (8 Replies)
Discussion started by: cinderella
8 Replies

4. Shell Programming and Scripting

loops

Hi All I have some directories on our server which are containing .csv files. i need to print value of cell "B2" from those csv files. Please advise. I have tried head command as example: head -2 */Book_Collection_Report_1_-_Collection_Requests_trials.csv | sed -n "3p" | awk -F","... (4 Replies)
Discussion started by: yash1978
4 Replies

5. Shell Programming and Scripting

Loops

Hi All, I want to execute a script the number of times a user enters. Please can you advise on hor can I do the same. Many Thanks, Shazin (4 Replies)
Discussion started by: Shazin
4 Replies

6. UNIX for Dummies Questions & Answers

Help with While Loops

I am traversing down a list, and I am not quite sure how to tell the loop to break when it's done going through the file. #!/bin/sh while : do read list <&3 echo $list done is the code. The file "list" is simply 5 4 3 2 1 any advice on how to break the loop after the file is... (1 Reply)
Discussion started by: MaestroRage
1 Replies

7. UNIX for Dummies Questions & Answers

two loops

Hi, how can I use "for" to have two loops : this is my script : for i in (A B C) do for j in (a b c) do echo $i$j done done #End I want to print out Aa Ab Ac .... But I have error message : syntax error at line 1 : `(' unexpected Many thanks before. How should I use "for" ?? (2 Replies)
Discussion started by: big123456
2 Replies

8. UNIX for Dummies Questions & Answers

While Loops

I'm trying to create a loop that will prompt the user for 15 values, not forcing them to enter all 15. If the user enters through one or more of the prompts the null value needs to be converted to 0, otherwise set the parameter = to the value entered: ex. Please enter file no #1: 17920 ... (4 Replies)
Discussion started by: vdc
4 Replies

9. Shell Programming and Scripting

Loops within loops

I am running on HPUX using ksh. I have a script that uses a loop within a loop, for some reason the script seems to hang on a particuliar record. The record is fine and hits the condition in Blue. If I kill the 1st loop process the script continues on with no problem. Begin code> <Some... (8 Replies)
Discussion started by: bthomas
8 Replies

10. Shell Programming and Scripting

no more loops

Hello Guys I need some help with my script I'm not very good at this, hope you can point me in the right direction. My idea, to write a file with some router commands and use it on a script for me to run on a demand basis, ( or added to the cron, if I get it to work). The below script works,... (5 Replies)
Discussion started by: tony3101
5 Replies
Login or Register to Ask a Question