Beginner bash - basic shell script 'while' help...


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Beginner bash - basic shell script 'while' help...
# 1  
Old 10-22-2017
Beginner bash - basic shell script 'while' help...

Hi everyone, first time visitor to these forums here.

Keeping a long story short I've been attempting to learn how to code in bash. I have VERY little previous experience with coding languages besides simply copying and pasting batch scripts for Windows. So, with that in mind I've followed a few tutorials on how to use some basic commands and how to write and execute basic scrips in bash. I'm now trying to put what I've learned to the test and am attempting to write out an entire (admittedly simple and useless) script without following any kind of tutorial. Here's what I have so far:

Code:
#!/bin/bash
while true; do
        echo "What is your name?"
        read name
        echo "What is your age?"
        read age
        echo "What is your sex?"
        read sex
        echo "so you're a $age old $sex called $name, is that right?"
        read right
                if test $right = Yes
                then echo "Great, thanks!"
                elif test $right = No
                then echo "Please enter your information again."
        fi
done

It's just a simple script that asks the user to input their information, and confirm that the details provided are correct. What I'm trying to do is have it so that if the details are incorrect and the user inputs "No" after being asked to confirm their details, a message is displayed asking them to enter their information again, and then return to the beginning of the script. However I'm struggling with getting to grips with the "while" command. Currently it doesn't matter what the user inputs as an answer, the script will restart anyway.

I've attempted to look online at some "while" loop tutorials, but so far I'm either not looking in the right places or It's just going straight over my head right now.

Can anyone offer some advice? If anyone does I really would appreciate it, I know that this is incredibly dull and basic for most of you...
# 2  
Old 10-22-2017
A quick fix would be to use break:
Code:
then 
  echo "Great, thanks!"
  break
elif ....

Which will end the loop when the user answers "Yes"

--
Or
Code:
right=No
while test $right != Yes
do
  ...
done

# 3  
Old 10-22-2017
Excellent, "break" worked a treat! Many thanks.

Any suggestions as to where to find some decent tutorials on using "while" loops? I don't just want to throw a messy script together and call it quits, I actually want to learn how to do this properly?

Appreciate the quick reply though. Smilie
# 4  
Old 10-22-2017
Quote:
Originally Posted by Meta
[...]
Code:
#!/bin/bash
while true; do
        echo "What is your name?"
        read name
        echo "What is your age?"
        read age
        echo "What is your sex?"
        read sex
        echo "so you're a $age old $sex called $name, is that right?"
        read right
                if test $right = Yes
                then echo "Great, thanks!"
                elif test $right = No
                then echo "Please enter your information again."
        fi
done

It's just a simple script that asks the user to input their information, and confirm that the details provided are correct. [...]
Quote:
Originally Posted by Meta
Excellent, "break" worked a treat! Many thanks.

Any suggestions as to where to find some decent tutorials on using "while" loops? I don't just want to throw a messy script together and call it quits, I actually want to learn how to do this properly?

Appreciate the quick reply though. Smilie
Let me give you a few tasks that will help you experiment toward your goal as highlighted in read
Before you utilize any input validate it as something you were expecting.
e.g.
How would you validate that the user have entered a proper gender? What if the user enters "Neutral" or any other variation of modern gender answers? What if it just pressed the ENTER/Return key?
How would you validate an age? What if the user enters 159 or 0 or just the ENTER/Return key?
How would you validate a name? Would you accept "Shakespeare's Hamlet" inputted as string for a name?
# 5  
Old 10-22-2017
Just an example for a multi-line while condition;
the last exit status before the do counts.
Code:
while
    echo "What is your name?"
    read name
    echo "What is your age?"
    read age
    echo "What is your sex?"
    read sex
    echo "so you're a $age old $sex called $name, is that right?"
    read right
    test "$right" != Yes
do
    if test "$right" = No
    then
        echo "Please enter your information again."
    fi
done
echo "Great, thanks!"

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If condition shell script beginner

Hi all I have the folloing process that needs checking often: ps -ef | grep ih bscsrtx 206 15901 0 11:28:10 pts/6 0:00 fih -r4 bscsrtx 218 15901 0 11:28:27 pts/6 0:01 aih bscsrtx 29763 15901 4 11:27:16 pts/6 0:59 rdh -prih root 429 27268 0 11:30:15 pts/td ... (13 Replies)
Discussion started by: fretagi
13 Replies

2. Shell Programming and Scripting

Beginner here, how to call a bash-script from python properly?

Hi everyone, i have the following script.sh: foo='lsusb | grep Webcam | cut -c16-18' sudo /home/user/public/usbreset /dev/bus/usb/001/$foo when i try to call this script from python using subprocess.call("script.sh", shell=True) it seems that only 'sudo /home/user/public/usbreset' is being... (6 Replies)
Discussion started by: hilfemir
6 Replies

3. Shell Programming and Scripting

Help in making a basic bash script

Hi All, I am trying to monitor CPU load of few processes, with the same name. The output that I get from top is the following 28171 root 20 0 1089m 21m 3608 S 103 0.3 15:16.89 /opt/ppp//h264rtptranscoder.bin --videoPort=14504 --audioPort=14505 27589 root 20 0 1060m 23m... (3 Replies)
Discussion started by: liviusbr
3 Replies

4. Shell Programming and Scripting

Bash Script to Ash (busybox) - Beginner

Hi All, I have a script that I wrote on a bash shell, I use it to sort files from a directory into various other directories. I have an variable set, which is an array of strings, I then check each file against the array and if it is in there the script sorts it into the correct folder. But... (5 Replies)
Discussion started by: sgtbobie
5 Replies

5. Homework & Coursework Questions

Shell Script (beginner)

1. The problem statement, all variables and given/known data: Arguments: http://farm9.staticflickr.com/8070/8212131370_8b6e8c10c5_c.jpg I am given these three arguments. $1, $2, $3 The first argument is the path to a directory. So, how would I go into the directory and compare files? I... (5 Replies)
Discussion started by: spider-man
5 Replies

6. Shell Programming and Scripting

Shell script (beginner) need help...

Hello, I'm new to Sheel script and I need your help for a script I need to develop (for me). Indead, I have a software which log all entry from internet and save it in text file. But, the log is practically unreadable because every 256 characters jump to a new line (even if the message is... (5 Replies)
Discussion started by: acidoangel
5 Replies

7. UNIX for Dummies Questions & Answers

Basic Unix bash script help

Hello there Been using Unix bash scripting for two days now so am very new to this. I am currently doing a project now and i'm basically making a noughts and crosses game (or tic tac toe). I have created the board using an array. When I try and check to see if the array is empty using an If... (3 Replies)
Discussion started by: ChrisHoogie
3 Replies

8. Shell Programming and Scripting

Beginner shell script help

. ./testFile.sh url=http://ichart.finance.yahoo.com/table.csv?s= suf=&d=5&e=9&f=2009&g=d&a=1&b=4&c=1999&ignore=.csv wget $url$s1$suf; sleep 10; cat header.txt > $s1.txt; chmod 777 $s1.txt; sed '1d' table.csv?s\=$s1 >> $s1.txt; rm -Rf table* Very new at shell scripting as you can see... (3 Replies)
Discussion started by: harte
3 Replies

9. Shell Programming and Scripting

Unix beginner. Need some very basic help.

Hey all I'm working on a script that will scour a directory of 6000+ files for a line that at the end has a number. Something like 'The number of outputs is: 39200' I'm trying to devise a script to add all of these numbers up, so far I have the grep statement that pulls out each individual... (7 Replies)
Discussion started by: TiznaraN
7 Replies

10. Shell Programming and Scripting

Shell Script for Beginner

I have a folder with lots of file. e.g. a.txt, b.txt, c.txt.... I want to put these files from the source directory and place them in a destination directory in a specific order, such as /destination/a/a.txt, /destination/b/b.txt, /destination/c/c.txt, ...... Please help. Thx :confused: (3 Replies)
Discussion started by: aaron_fong
3 Replies
Login or Register to Ask a Question