Read a output which should be only numeric - linux

In unix, i need to read a o/p which should be numeric, if suppose user entered character it should throw error as "invalid identifier". Is that possible in if loop?

try this...
unset get_num
while [[ ! ${get_num} =~ ^[0-9]+$ ]]; do
echo "Please enter in a number:"
read get_num
done
echo This is a number : ${get_num}

Related

Bash script that allows one word as user input

Made a script that the user gives a "parameter" and it prints out if it is a file, directory or non of them. This is it :
#!/bin/bash
read parametros
for filename in *
do
if [ -f "$parametros" ];
then
echo "$parametros is a file"
elif [ -d "$parametros" ];
then
echo "$parametros is a directory"
else
echo " There is not such file or directory"
fi
exit
done
Altough i want the user to be allowed to give only one word as a parameter. How do i make this happen ? (For example if user press space after first word there would be an error message showing "wrong input")
#!/bin/bash
read parametros
if [[ "$parametros" = *[[:space:]]* ]]
then
echo "wrong input"
elif [[ -f "$parametros" ]]
then
echo "$parametros is a file"
elif [[ -d "$parametros" ]]
then
echo "$parametros is a directory"
else
echo " There is not such file or directory"
fi
See http://mywiki.wooledge.org/BashFAQ/031 for the difference between [...] and [[...]].
You have to use the $#. It gives the number of the parameters.
The code will be something like:
if [ "$#" -ne 1 ]; then
printf 'ERROR!\n'
exit 1
fi
First, I'm curious why you want to restrict to one word - a file or directory could have spaces in it, but maybe you are preventing that somehow in your context.
Here are a few ways you could approach it:
Validate the input after they enter it - check if it has any spaces, eg: if [[ "parametros" == *" " ]]; then...
Get one character at a time in a while loop, eg with: read -n1 char
Show an error if it's a space
Break the loop if it's 'enter'
Build up the overall string from the entered characters
1 is obviously much simpler, but maybe 2 is worth the effort for the instant feedback that you are hoping for?

Why doesn't string comparison with wildcards work properly?

I wrote this shell code, but it doesn't get the good output.
Even though the $csoport gets the "...: No such user" output, id doesn't echoes the following line I wrote there.
read felhasznalo
while [ "$felhasznalo" != "exit" ]
do
csoport=`groups $felhasznalo`
echo "$csoport"
if [[ "$csoport" == *": No such user"* ]] ; then
echo -n "Nincs ilyen felhasznalo a rendszerben"
else
echo "$csoport"
fi
echo -n "Felhasznalo: "
read felhasznalo
done
You shouldn't try to match the error messsage since you only care if groups fails. You ought to do:
if ! csoport=$(groups "$felhasznalo"); then
printf "Nincs ilyen felhasznalo a rendszerben"
else
echo "$csoport"
fi

How to ask user input again and get new value if validation fails?

In my program I need to validate user input getting by "read" in shell script. It check user input has spaces and if validation fails we should give user a another chance to input again.
stringValidator() {
if [[ ${1} =~ " " ]]; then
echo Should not contain spaces
echo Enter Again!
read input
echo $2=$input
return $2
else
return $1
fi
}
echo "Enter below details"
echo -e "client-id :"
read clientId
stringValidator "$clientId" ${!clientId#}
Here I tried to do was send the variable name also into stringValidator() and reassign value to that name (here it is clientId).
But the problem is I can't return that variable from stringValidator(). I use many read inputs though I showed only one here. Therefore I need a separate function for validation.
If you have better way to do this please mention.
The statement "return" is not used like that, it's to send back an error code (numerical).
That code should do what you want though :
#!/bin/bash
echo "Enter below details"
while true; do
echo -n "client-id : "
read clientID
if [[ $clientID =~ " " ]]; then
echo "ClientID Should not contain spaces"
continue
else
break
fi
done
echo "do something else"
EDIT
To answer your comment, this code in ksh would do what you ask :
#!/bin/ksh
function stringValidator
{
typeset locVarName
typeset locVar
locVarName=$1
nameref locVar=$1
while true; do
if [[ $locVar =~ " " ]]; then
echo "$locVarName should not contain spaces"
echo -n "$locVarName : "
read locVar
continue
else
break
fi
done
}
echo "Enter below details"
echo -n "Client-ID : "
read clientId
stringValidator "clientId"
echo "Final value is $clientId"
echo "do something else"
Running would result to something like that :
Enter below details
Client-ID : test t
clientId should not contain spaces
clientId : tesgg
Final value is tesgg
do something else
Hope it helps !
Separate Validation function from read input. Next, introduce it into a while loop to ask for enter input again up to a correct value. Use ECHO instead of RETURN to get the value from a function.

How to read two input values in bash?

I am writing a bash script which takes multiple user inputs. Before the script will take any action, I want to ensure that all values have been added by a user.
#/bin/bash
read -p "Please enter the domain Name: " domain
read -p "Please Enter path where you want to save your result: " path
if [[ -z "$domain" && "$path"]]; then
echo "You have not entered the Domain Name"
exit 1
else
echo "Do Something Here"
fi
I have checked with 1 user input, working fine, but when trying with 2 user inputs, I am getting an error.
./test.sh: line 5: unexpected token `;', conditional binary operator expected
./test.sh: line 5: syntax error near `;'
./test.sh: line 5: `if [[ -z "$domain" && "$path"]]; then'
Thanks!
Because you're using the [[ double brackets, you can use || to test if either of your conditions is true. In this case, your code would look like this:
#!/usr/bin/env bash
read -p "Please enter the domain Name: " domain
read -p "Please Enter path where you want to save your result: " path
if [[ -z "$domain" || -z "$path" ]]; then
echo "Either you have not entered the Domain Name, or you have not entered the path."
exit 1
else
echo "Do Something Here"
fi
Note that spaces around the brackets are necessary.
As others have pointed out, errors should be specific, so you should consider something like this:
if [[ -z "$domain" ]]; then
echo "You have not entered the Domain Name"
exit 1
elif [[ -z "$path" ]]; then
echo "You have not entered the path"
exit 1
fi
echo "Do something here"
It's a bit more verbose but gives the user more specific feedback.
You are getting syntax error because you forgot to put a space between "$path" and ] (bash uses spaces as delimiters).
If you want to fail when at least one condition is wrong, you should use the || (OR) operator.
#/bin/bash
read -p "Please enter the domain name: " domain
read -p "Please enter the path where you want to save your result: " path
if [[ -z "$domain" ]] || [[ -z "$path" ]] ; then
echo "You didn't enter the domain name or the save path"
exit 1
else
echo "Do something here"
fi

bash condition with regular expression

I'm going to learn bash programming. I just wrote a simple script in order to read numbers from input stream and check them to be in valid numerical format using regular expression.
in fact script should take input until the input be nonnumerical.
but it doesn't work properly.
code:
i=0
echo "plz enter number in valid format: "
while true
do
read input
if [[ $input =~ *[^0-9]* ]]; then
echo "YOU DIDN'T ENTER A VALID NUMBER"
break
else
arr[$i]=$input
echo $input >> inputnums
fi
done
when i enter a number or character condition is true. I mean i have echo "message" in output.
You're mixing shell globing with regex, change your if condition to:
if [[ $input =~ [^0-9] ]]; then
regex should use .* not * as used by shell shell glob.
Remove the * from the regex:
#!/bin/bash
i=0
echo "plz enter number in valid format: "
while true
do
read input
if [[ $input =~ ^[^0-9]+$ ]]; then
echo "YOU DIDN'T ENTER A VALID NUMBER"
break
else
arr[$i]=$input
echo $input
fi
done

Resources