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
whats missing
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
whats missing
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.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: whats missing
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.
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').
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.
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 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 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.
Re: whats missing
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.
Opening the terminal first, then running the script proved successful. Sorted! Thanks anyway.