LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 05-15-2024, 02:34 AM   #1
gerard4143
Member
 
Registered: Jan 2008
Location: P.E.I. Canada
Posts: 32

Rep: Reputation: 4
[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.
 
Old 05-15-2024, 03:18 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Well, Zsh has got try-blocks, but Bash doesn't have anything like this AFAIK.
 
Old 05-15-2024, 03:42 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
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.
Old 05-15-2024, 04:54 AM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
@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.
 
Old 05-15-2024, 05:44 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
Quote:
Originally Posted by shruggy View Post
@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?
 
Old 05-15-2024, 05:55 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
The code from the first post.
 
Old 05-15-2024, 05:57 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
yes, I tried. And I also wrote it won't work in this case.
 
Old 05-15-2024, 06:00 AM   #8
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Ah sorry, my fault. I didn't read your post thoroughly.
 
Old 05-16-2024, 12:48 PM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
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.
Old 05-16-2024, 01:46 PM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
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.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pros/cons of declare variable in bash Arct1c_f0x Programming 7 02-15-2021 04:45 PM
g++ 2.9-gnupro-98r2, error: cannot declare references to functions; use pointer to function instead anums Linux - Newbie 2 08-29-2016 07:45 PM
[SOLVED] how to declare an array from command substitution in bash mia_tech Linux - General 1 03-08-2014 01:18 PM
I feel stupid: declare not found in bash scripting? Mohtek Programming 6 04-29-2010 08:09 AM
g++ compiler error declaration does not declare anything Basiltp Programming 4 10-11-2004 11:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration