Hmm while do loops.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Hmm while do loops.
# 1  
Old 12-07-2009
Hmm while do loops.

Hey guys, i'm trying to write a script which checks user input. If the input provided is wrong i.e. " " then I want to reprompt the user to enter it again and again etc. until it's OK.

Here's my code:

Code:
 
wp="wp"
ss="ss"
pf="pf"
echo "Welcome to the backup utility"
echo "what is your username?"
read username
if [[ $username == "" ]]
then
echo "No username provided"
while [ $username == ""]
do
echo "what is your username please?!"
read username
done
else
echo /home/$username

I can get it to reprompt the user for their name. However when I enter the name after 1 of these reprompts I get the error: line 10: [: ewan (the username I enter): unary operator expected.


Any suggestions?


Thanks


Ewan
# 2  
Old 12-07-2009
There must be a space before this closing square bracket:

Code:
while [ $username == "" ]
                       ^

# 3  
Old 12-07-2009
And double quotes around the variable.
Code:
while [ "$username" == "" ]

-or-
ksh/bash style double brackets:
Code:
while [[ $username == "" ]];

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

If and Loops

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: In this script you will take a directory as input from the user and change the end of line sequence from a Unix... (1 Reply)
Discussion started by: Pcarson
1 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. UNIX for Dummies Questions & Answers

loops with tr

Hello, I'm not sure if this is more appropriate for the 'unix for dummies' or the 'unix for experts' forum because I'm new to this forum and this is the second topic I've discussed, but if you could let me know which one was more appropriate for something like this, please do! So in tr (an... (2 Replies)
Discussion started by: juliette salexa
2 Replies

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

5. Shell Programming and Scripting

Help with the 2 for loops

#!/bin/bash IFS=$'\n' A= a c b t g j i e d B= t y u i o p counter=0 found="" for i in $(cat $A) do for j in $(cat $B) do if then found="yes" fi done if then (1 Reply)
Discussion started by: vadharah
1 Replies

6. Shell Programming and Scripting

while loops

Hi I've a file like so: Now, I want to read my file and take ex. the Media ID and the Type for each groups of Media (Media1,Media2,...,Media(n): cat /tmp/file|\ while read FILE do while $(FILE|cut -d: -f1)=Media$i do #here will be some test, ex: #if Media ID < 23 ... (4 Replies)
Discussion started by: nymus7
4 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. UNIX for Dummies Questions & Answers

hmm

I'd like to learn more about Unix, can any of you guys gimmie something or point me in the right direction? (1 Reply)
Discussion started by: Jace
1 Replies
Login or Register to Ask a Question