Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-15-2024, 02:34 AM
|
#1
|
Member
Registered: Jan 2008
Location: P.E.I. Canada
Posts: 32
Rep:
|
[Bash]declare -i error message
Code:
#! /usr/bin/env bash
set -euo pipefail
declare -i num1=0
read -r -p 'Enter first number: ' num1
echo "You entered: ${num1}"
How can I display a message(better than the default) when the user enters a non-integer value for num1?
Last edited by gerard4143; 05-15-2024 at 01:38 PM.
|
|
|
05-15-2024, 03:18 AM
|
#2
|
Senior Member
Registered: Mar 2020
Posts: 3,706
Rep:
|
Well, Zsh has got try-blocks, but Bash doesn't have anything like this AFAIK.
|
|
|
05-15-2024, 03:42 AM
|
#3
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,702
|
Code:
read -r -p 'Enter first number: ' num1 || { here comes the error handling }
In general this is how you can handle errors in shell. But not in this case, because entering a non-integer string will not cause an error (but set num1 to 0).
You can do:
1. allow to read anything and check if it is a number
2. do not accept if num1=0
3. do nothing
|
|
1 members found this post helpful.
|
05-15-2024, 04:54 AM
|
#4
|
Senior Member
Registered: Mar 2020
Posts: 3,706
Rep:
|
@ pan64. Have you tried it? It doesn't work in this case. I was thinking perhaps
Code:
trap 'echo Wrong; exit 1' ERR
would help, but that didn't work either. Trapping on EXIT does work though, so you can at least add your own message to that of Bash.
|
|
|
05-15-2024, 05:44 AM
|
#5
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,702
|
Quote:
Originally Posted by shruggy
@ pan64. Have you tried it? It doesn't work in this case. I was thinking perhaps
Code:
trap 'echo Wrong; exit 1' ERR
would help, but that didn't work either. Trapping on EXIT does work though, so you can at least add your own message to that of Bash.
|
what do you mean? I mean what should be tried?
|
|
|
05-15-2024, 05:55 AM
|
#6
|
Senior Member
Registered: Mar 2020
Posts: 3,706
Rep:
|
The code from the first post.
|
|
|
05-15-2024, 05:57 AM
|
#7
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,702
|
yes, I tried. And I also wrote it won't work in this case.
|
|
|
05-15-2024, 06:00 AM
|
#8
|
Senior Member
Registered: Mar 2020
Posts: 3,706
Rep:
|
Ah sorry, my fault. I didn't read your post thoroughly.
|
|
|
05-16-2024, 12:48 PM
|
#9
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Posts: 2,917
|
Allow all input then check for an integer:
Code:
#!/bin/bash
declare -i num1=0
while
read -r -p 'Enter first number: ' inp
[[ ! $inp =~ ^[-+]?[0-9]+$ ]]
do
echo "bad input, try again" >&2
done
num1=$inp
echo "You entered: ${num1}"
Within [[ ]] the =~ operator checks the LHS against an extended regular expression (ERE).
Can also be done with the == or != operator that checks the LHS against an extended glob:
Code:
[[ $inp != ?([-+])+([0-9]) ]]
Last edited by MadeInGermany; 05-16-2024 at 12:52 PM.
|
|
1 members found this post helpful.
|
05-16-2024, 01:46 PM
|
#10
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,702
|
the most interesting way to do it is:
Code:
declare -i num1=0
read -r -p 'Enter first number: ' inp
read -r num1 <<<"$inp"
[[ $num1 = $inp ]] || whoopppps
(Or just an idea)
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 05:07 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|