The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Exit Status within sudo su - $user quigley007 Shell Programming and Scripting 7 07-29-2008 09:37 AM
handling maximum number characters in an input file chrysSty UNIX and Linux Applications 1 05-12-2008 11:19 AM
Bourne Shell: Special characters Input Prevention totziens Shell Programming and Scripting 37 04-23-2008 05:05 AM
Disallowing certain characters from user input paqman Shell Programming and Scripting 1 08-09-2007 06:44 PM
Getting user input stevefox Shell Programming and Scripting 3 02-16-2007 02:09 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-30-2008
ajp7701 ajp7701 is offline VIP Member  
Supporter
  
 

Join Date: Dec 2007
Posts: 63
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 (permalink)  
Old 10-30-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink 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 (permalink)  
Old 10-30-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,875
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:00 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0