Bash : how do i check the user input and make sure is only chracter or only number ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash : how do i check the user input and make sure is only chracter or only number ?
# 1  
Old 09-07-2008
Bash : how do i check the user input and make sure is only chracter or only number ?

Bash : how do i check the user input and make sure is only character or only number ?
# 2  
Old 09-07-2008
one way
Code:
echo "$user_input" | tr -dc '[:digit:]' | read check_it
if [[ ${#check_it} -eq 0 ]] ; then
     echo 'all numbers'
fi
echo "$user_input" | tr -dc '[:alpha:]' | read check_it
if [[ ${#check_it} -eq 0 ]] ; then
     echo 'all alphabetic characters'
fi

# 3  
Old 09-07-2008
if i only want to check the first digit wheter is a number "0" or not .
how to do it
# 4  
Old 09-07-2008
Code:
echo "$user_input | grep -q '^0' && echo 'first character is zero'

# 5  
Old 09-07-2008
grep for ^0

Code:
grep "^0"

# 6  
Old 09-07-2008
i want check the user input and make sure only
alphabetic characters and spaces how i can do that?

"enter something"
read input

i want check the $input . how ?
# 7  
Old 09-07-2008
Code:
case $input in
  *[!0-9]*) case $input in
    *[! A-Za-z]*) echo not all numbers or all spaces+alphabetics;;
    *) echo all spaces or alphabetics;;
  esac;;
  *) echo all numbers;;
esac

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Would like to check user input for letters within a loop

Hi All, #!/bin/bash #Just trying to check if letters are in the user input. Any tips? # I have tried regexp and using 0-9 etc, i cannot get this to work either in just an if statement or while in a loop. echo "Please pick a number" read num if ; then echo "Please enter a number"... (7 Replies)
Discussion started by: jvezinat
7 Replies

2. Shell Programming and Scripting

How to check the user input to be valid using shell script?

How to check the user input to be valid using shell script? The valid input is in the format like as follows. 1. It can only have r,w,x or a hyphen and nothing else. 2. ensure the r, w, x are in the correct order. for example: rwxr-xr-x is a valid format. Thanks (5 Replies)
Discussion started by: hyeewang
5 Replies

3. Shell Programming and Scripting

How-To Check & Filter user input

Hi, On my Java webpage which invokes the shell script has two checkboxes viz ... apache and weblogic apache require one parameter i.e apache home from the user while Weblogic requires three or five params from the user vi.z weblogic_home or <jdk_home, config_home & pid>, username and... (4 Replies)
Discussion started by: mohtashims
4 Replies

4. Shell Programming and Scripting

Check user input

Hi, I need my script to check if the user enters 3 values if not 5 values to my script and alert if the input has any other number of values. for example: ./myscript.sh 22 56 3221 - > correct ./myscript.sh 22 56 3221 45 777 -> correct ./myscript.sh 22 56 3221 45 -> incorrect Please... (6 Replies)
Discussion started by: mohtashims
6 Replies

5. Shell Programming and Scripting

Maximum number from input by user

I am trying to calculate the maximum number from four numbers input by the user. I have the following code, but it does not work. It says there's an error with the last line "done". Any help would be appreciated. max=0 echo "Please enter four numbers: " for i in 1 2 3 4 do read number... (17 Replies)
Discussion started by: itech4814
17 Replies

6. Shell Programming and Scripting

Help with Bash user input

I am starting to learn how to use bash and I would like the script to do the following: Asks the user for his/her name Asks the user for one number Asks the user for another number Then it adds the two numbers, Also multiply the two numbers I have the part where you get the name, and I... (3 Replies)
Discussion started by: boyboy1212
3 Replies

7. Shell Programming and Scripting

Bash user input

Hi all, I currently have a script which uses read -p for user interaction. e.g. read -p "New user? " user Is it possible to have it so if the user enters nothing and just presses return it can resort to a specified value instead? Thanks! :) (5 Replies)
Discussion started by: JayC89
5 Replies

8. Shell Programming and Scripting

Check if the input is number or text?

i have to check if is input number or text, i case that is text then exit script exaple is hrere, but is not working check_it() { if ) ] then if (( $1 > 0 && $1 < 13 )); then echo "input number is ok" else ... (2 Replies)
Discussion started by: waso
2 Replies

9. UNIX for Dummies Questions & Answers

how to check the user input from terminal

Hello everybody!!! I am writing my own rm command in unix. I prompt the user to type if he wants to delete a file and then read what he typed. But how do i check what he typed? This is my program so far: echo 'Delete prog1.c (y/n)?' read yesOrNo if yesOrNo == 'y' then rm prog1.c... (6 Replies)
Discussion started by: mskart
6 Replies
Login or Register to Ask a Question