exit script if user input not four characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting exit script if user input not four characters
# 1  
Old 10-30-2008
exit script if user input not four characters

#!/usr/bin/bash
###script to input four characters. wxyz
echo "input first string"
read instring1
echo "input second string"
read instring2
##
echo "first string is:" $instring1
echo "second string is:" $instring2
##IF instring1 or instring2 are NOT 4 characters (xxxx) , exit 1.
##how??

If it is any easier, the four input characters "should" be numbers, like 0032 and 0198, or 2359 and 0900 , but I'd settle for trusting that the user knows to input digits and not alphas....
# 2  
Old 10-30-2008
Hammer & Screwdriver Here is one approach

Code:
> if [ `echo "hello" | wc -c` -gt 4 ]; then echo "too many characters"; fi
too many characters
> if [ `echo "hell" | wc -c` -gt 4 ]; then echo "too many characters"; fi
too many characters

Huh???

Code:
> if [ `echo "hell" | wc -c` -gt 5 ]; then echo "too many characters"; fi
>

That is because the echo adds a new-line after it sends the output; to start to a new line. Thus, you have to check for one more character.
# 3  
Old 10-30-2008
Code:
read -penter:\ 
case $REPLY in
  *[^0-9]* | "" ) printf "invalid number\n"; exit 1;;
           ???? ) printf "ok, let's go\n";;
              * ) printf "wrong length\n"; exit 2;;
esac

If your shell does not support read -p, use your original syntax.

P.S. Actually, this is sufficient:

Code:
read -penter:\ 
case $REPLY in
  [0-9][0-9][0-9][0-9] ) 
    printf "ok, let's go\n";;
                     * ) 
    printf "invalid number\n" 
    exit 1;;
esac

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help Me. The script should not exit until the user gives an input.

Hi everyone, I'm new here and just a beginner in linux scripting. Just want to ask for help on this one. I am trying to create a script that will accept user input (year-month and user/s). I wanted to have the script to continue running, until the user inputs a DATE and name/s of user/s. ... (2 Replies)
Discussion started by: Helskadi
2 Replies

2. Shell Programming and Scripting

Exit script and open program via other user

Hello all.. so i have a problem i need to solve .. #! /bin/bash $SHELL dtterm -title my_prog -e su -user -c 'export DISPLAY=:0.0 ; /path/to/my/prog' & 2> /dev/null $SHELL intr exit This script will work on solaris 10 system in right clikt menu - in a secure system so i need to... (0 Replies)
Discussion started by: defs
0 Replies

3. Shell Programming and Scripting

Checking the user input in perl for characters and length

My question is basically as the title says. How can I check a user inputted string is only certain characters long (for example, 3 characters long) and how do I check a user inputted string only contains certain characters (for example, it should only contain the characters 'u', 'a', 'g', and 'c')... (4 Replies)
Discussion started by: Eric1
4 Replies

4. Shell Programming and Scripting

Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?

Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop.... (9 Replies)
Discussion started by: hilltop_yodeler
9 Replies

5. Ubuntu

Exit user in bash script

I'm writing a bunch of scripts to automatically configure Ubuntu and I want to run the code below to remove the white dots from the login screen: sudo xhost +SI:localuser:lightdm sudo su lightdm -s /bin/bash gsettings set com.canonical.unity-greeter draw-grid false The problem is that... (3 Replies)
Discussion started by: maerlyngb
3 Replies

6. Shell Programming and Scripting

Create user if UID not exist; else, exit the script

Hi, I want to write a script to check whether an user ID is used in my server and then create that user. If the user ID is not used, I will echo something like "OK, continue" and then continue to execute the script. Else, I will echo something like "Used, exit" and then exit the script. As... (4 Replies)
Discussion started by: dirkaulo
4 Replies

7. Shell Programming and Scripting

Script interacts with user , based on user input it operates

i have a script which takes input from user, if user gives either Y/y then it should continue, else it should quit by displaying user cancelled. #!/bin/sh echo " Enter your choice to continue y/Y OR n/N to quit " read A if then echo " user requested to continue " ##some commands... (7 Replies)
Discussion started by: only4satish
7 Replies

8. Shell Programming and Scripting

User Input Shell Script

Hello I am trying to create a user input shell scipt. The objective is user should enter the circuit number and the input is saved in a log file. If the user does not enter anything then the question should prompt it until the circuit no. is entered. Can any one please correct the code below.... (3 Replies)
Discussion started by: sureshcisco
3 Replies

9. Shell Programming and Scripting

Exit script if the user dosent enter any data within 5 seconds

Hello friends, Kindly help me in developing a script that asks user to enter a value and will wait for 5 seconds for the feedback. If there is no answer from the user the script will perform exit or it will continue doing something else Ex: If yu have a multi OS system i believe while... (3 Replies)
Discussion started by: frozensmilz
3 Replies

10. Shell Programming and Scripting

Disallowing certain characters from user input

Hey, I've create a custom useradd script, and I don't want the person creating the user to be able to put comma's in any of the input fields, because it could corrupt the /etc/passwd file. I don't care what other characters they put in there, so is there a way I can just check all the input... (1 Reply)
Discussion started by: paqman
1 Replies
Login or Register to Ask a Question