-4

How to write in Linux in Bash in shellscript a part that can do something like this

enter a number

must be in a range from 4-999

if not, ask again to enter

enterednumber * 5 * randomnumber [1-9] (single diget)

display the result and ask if good

enter - for new run (enterednumber * 5 * randomnumber [0-9] (single diget) )

y - for yes, oke, go on in the script

n - go back to the point to "enter a number"" again

e - for exit the script


i know that with the command "read" there is a prompt to enter something and i know that with cat /dev/urandom | tr -cd '0-9' | head -c 1 is to become a single random digit

4
  • You said you want a number from 1 to 9 but then show tr -cd '0-9' which outputs digits from 0 to 9 - which is correct?
    – Ed Morton
    Commented Nov 18, 2024 at 11:06
  • Does the number in the range 4-999 need to be an integer or could it be something like 5.7 or 9e3?
    – Ed Morton
    Commented Nov 18, 2024 at 11:12
  • the number should be in a range from 0-9. no 5.7 or 9e3.
    – user447274
    Commented Nov 19, 2024 at 3:52
  • 5.7 IS a number between 0 and 9. My point about 9e3 was just can you have exponents which are also numbers in the range 0 to 9. My question was - does the number need to be an integer?
    – Ed Morton
    Commented Nov 19, 2024 at 12:15

2 Answers 2

1
#!/bin/bash

while true; do
        while true; do
                read -r -p 'Enter number [4-999]: ' num

                if [[ $num =~ ^([4-9]|[1-9][0-9]{1,2})$ ]]; then
                        break
                fi
        done

        result=$(( 5 * num * (1 + (RANDOM%9)) ))

        PS3="Is $result ok? "
        select yesno in yes no exit; do
                case $REPLY in
                        1|y) break 2 ;;
                        2|n) break ;;
                        3|e) exit ;;
                esac
        done
done

echo 'Rest of script here'

This script starts with an input loop (the outer while loop). This is an infinite loop that iterates until the given conditions are satisfied. The conditions for this loop are that the user is happy with some computed value and selects to continue or to terminate the script.

The computed value also requires an input loop (the inner while loop). This loop iterates until the string the user inputs at the prompt is some number between 4 and 999.

Once the user has entered a satisfactory string (a whole decimal number in the range [4,999]), the result value is computed and presented to the user along with a selection of options. The input from the user is again performed by an input loop, but in this case, it's a select loop (since we're presenting the user with a menu of options to pick from).

If the user picks either 1 or y, the code breaks out of both the select and the other while loop using break 2. If the user picks 2 or n, the code only breaks out of the select loop, which causes the program to query again for a number between 4 and 999. If the user selects 3 or e, the script terminates via exit.

2
  • Thanks for the code, but how to add another key "a" for to do again the entered number with a new random number?
    – user447274
    Commented Nov 19, 2024 at 3:57
  • @user447274 Just put another while true; do ...; done loop around the result calculation and the select loop, add a fourth menu option to the select statement and modify the case statement to the new code (change break 2 to break 3, break to break 2, and add a break for when the user picks a). I won't change my current answer as this was not part of the question.
    – Kusalananda
    Commented Nov 19, 2024 at 6:44
1

Assuming the number you want entered between 4 and 999 is an integer:

$ cat tst.sh
#!/usr/bin/env bash

beg=4
end=999
mult=5

while read -r -p "Enter an integer [$beg-$end]: " num; do

    if [[ "$num" =~ ^[0-9]+$ ]] && (( beg <= num )) && (( num <= end )); then

        printf '%d\n' "$(( num * mult * (1 + $RANDOM % 9) ))"

        read -r -p 'Is that good [yne]? ' rslt
        if [[ "$rslt" =~ ^[nN] ]]; then
            continue
        else
            break
        fi

    fi

done

if [[ ! "$rslt" =~ ^[yY] ]]; then
    exit
fi

echo 'Continuing...'

It treats any response to the "Is that good" question other than y or n (any case and optionally followed by other chars, e.g. yes or NO) as e in case that's not obvious. I don't think there's anything I could add to explain it, it just does what the code clearly does.

3
  • Thanks for the code, but how to add another key "a" for to do again the entered number with a new random number ?
    – user447274
    Commented Nov 19, 2024 at 3:56
  • I don't know what that 'how to add another key "a"' means but just run the script and you'll see it does what you asked for,
    – Ed Morton
    Commented Nov 19, 2024 at 12:16
  • If what you asked for isn't what you actually want then accept an answer to the question you asked and then ask a new followup question.
    – Ed Morton
    Commented Nov 19, 2024 at 12:17

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.