bash script question

Linux for blind general discussion blinux-list at redhat.com
Mon Dec 12 08:07:33 UTC 2022


Inline posting with some additional pointers.

On 12/12/22 05:52, Linux for blind general discussion wrote:
> #!/bin/bash
>
> quiz() {

I would use 'local response', in shell you need to define the scope
yourself.

>    echo "Starting quiz..."
>    echo "What is 2+2?"
>    read response

You are using response twice in your script, given that this code is the
body of a function should use 'local' to make it only usable in the
function (function scope) and not in all of your script (global scope).

>    if [ "$response" == 4 ]

This is a bad practiss to use '==' in shell you can use one ('[
"$response" = '4' ]')
Given that you are comparing two strings you should also make 'four' a
string!

>    then
>      echo "You got it."
>    else
>      echo "That is incorrect."
>    fi
> }
>
> until [ "$response" == 4 ]; do

Same as above.

>    echo "Begin math quiz? (y/n)"
>    read response
>    if [ "$response" == "y" ] || [ "$response" == "Y" ]
>    then

A more straightforward alternative would be to use a case statement.

>      SECONDS=0

Why are you capitalizing your variable?

>      quiz
>      echo "The quiz took you $SECONDS seconds to complete."
>    else
>      echo "Exiting..."
>      exit
>    fi
> done
>
HTH.

--
John Doe



More information about the Blinux-list mailing list