[SOLVED] How to exclude input of all digits in bash script? I want only letters to work, not numbers and signs

Issue

This Content is from Stack Overflow. Question asked by Марифат Гадиров

#!/bin/bash
if [[ -n $1 ]]
then
if [[ -n $2 ]]
then
echo “Error: you entered too many parameters”
else
regular=”^[-+]?[0-9]+([.][0-9]+)?$”
if [[ $1 =~ $regular ]]
then
echo “Error: You did not enter text”
else
echo $1
fi
fi
else
echo “No parameters found”
fi



Solution

Try this:

if [ $# -eq 1 ]
then
    regular="^[a-zA-Z]+$"
    if [[ $1 =~ $regular ]]
    then
        echo $1
    else
        echo "Error: You did not enter text"
    fi
elif [ $# -gt 1 ]
then
    echo "Error: you entered too many parameters"
else
    echo "No parameters found"
fi
  • $# is the number of input, you can check it instead of $1, $2 and so on
  • Also, instead of checking the wrong input, you can check the correct input


This Question was asked in StackOverflow by Марифат Гадиров and Answered by Milad Keshavarzi It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?