whats missing

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
mintybuntu

whats missing

Post by mintybuntu »

Being a total noob i have bodged together two basic scripts and the second part dont work.
My eyes are killing me from trying to learn all this stuff and need a break.
Could anyone please tell me whats missing.

#!/bin/bash

clear
echo -n "Enter your name: "
read name

clear
echo "Hello $name "
echo "What is your favourite colour?"
read color

clear
echo "$color is a good colour"

echo -n "Are you man or mouse? "
read character

#script goes awol around here
case $character in

man ) echo " B#######!..Thats what you think."
;;

mouse ) echo " Have a piece of cheese."
;;

* ) echo "Answer the question..Dumb-ass!"
echo " Try again!."

esac
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
jahid

Re: whats missing

Post by jahid »

Everything is working as expected, so which part isn't working?
hupnuk
Level 1
Level 1
Posts: 18
Joined: Sat Mar 29, 2014 9:36 pm

Re: whats missing

Post by hupnuk »

Hello mintybuntu,

You do not need to remove the newline, neither have to use 'echo'.
The read command has the '-p PROMPT' parameter to prompt for a question.
Also, when having variables that could potionally contain anything, 'printf' may be more suitable as 'echo' may go haywire if some weird character is given.

Code: Select all

clear
read -p 'Enter your name ' name

clear
printf '%s\n' "Hello $name"
read -p 'What is your favourite colour? ' color

clear
printf '%s\n' "$color is a good colour"
For the original question, i do not really know what you think is wrong, but i think you want it to loop on a wrong answer.
For this you can use something like 'while' or 'select' (i'll use while as it is more posix applicant and shell does not contain 'select').

Code: Select all

while read -p 'Are you man or mouse? ' character; do
	case $character in
		man )
			printf '\n%s\n' 'B#######!..Thats what you think.'
			break
			;;
		mouse )
			printf '\n%s\n' 'Have a piece of cheese.'
			break
			;;
		* )
			printf '\n%s\n%s\n' 'Answer the question..Dumb-ass!' 'Try again!.'
			;;
	esac
done
The command while loops as long as it does not get a positive/true condition at the end of the loop.
The printf...well is not really needed, but i like printf. You may use echo if you wish instead. (The '\n' btw stands for newline. The '%s' stands for string and keeps the actual string from being formatted)

Btw, you could also use /bin/sh instead of bash, so it is posix applicant and thus will more easily run on other distro's with different bases.
Last edited by hupnuk on Tue Dec 02, 2014 4:45 pm, edited 1 time in total.
mintybuntu

Re: whats missing

Post by mintybuntu »

Sorry! i was right clicking on script and selecting run in terminal which ran up to the awol comment then terminal closed.
Opening the terminal first, then running the script proved successful. Sorted! Thanks anyway.
mintybuntu

Re: whats missing

Post by mintybuntu »

Thanks hupnuk, i will study all that.
Locked

Return to “Scripts & Bash”