Sponsored Content
Top Forums Shell Programming and Scripting How to validate input parameters? Post 302409986 by bakunin on Saturday 3rd of April 2010 02:09:22 PM
Old 04-03-2010
This is a very good question! I have a ksh function dealing with exactly this im my library.

Lets start with an integer: an integer is a sequence of digits 0-9 preceeded optionally with a plus or a minus sign:

Code:
# i assume the string in question was passed as argument $1 here
if [ -n "$(print - $1 | sed 's/[+-]*[0-9][0-9]*//')" ] ; then
     print - "string is not an integer"
else
     print - "string is an integer"
fi

The sed statement will delete out a possible integer and if the remaining string still contains characters ("-n") it can't be an integer. This mechanism is the core of my f_CheckInteger() function.

General numerics are a bit more complicated. I didn't bother to implement localized decimal separators (americans write "123.4", germans write "123,4", etc.) or something such, so i ended with the quite simple definition:

A numerical value is a string consisting of an optional plus/minus sign, followed by one or more digits 0-9, optionally followed by a full stop "." followed by one or more digits 0-9.

These are valid numbers according to this definition: 0.123 123 123.0, -123.4
These are not valid numbers: 0..123 0.123.456 123. +-12

Code:
# i assume the string in question was passed as argument $1 here
if [ -n "$(print - $1 | sed 's/[+-]*[0-9][0-9]*\(\.[0-9][0-9]*\)*//')" ] ; then
     print - "string is not a numeric"
else
     print - "string is a numeric"
fi

I hope this helps.

bakunin
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to validate input values

Hi How would i validate value of a variable whether it is number,date or string Thanks in advance Sas (5 Replies)
Discussion started by: SasDutta
5 Replies

2. Shell Programming and Scripting

Need to validate a date input format

Hi all, I have a shell script(K shell) which takes a date as input. i want the input to be in DD-MM-YYYY format. Can i enforce such a format of input string using just one line of code? OR do i need to parse the input date into different components and test them using Case statements... (2 Replies)
Discussion started by: rajugp1
2 Replies

3. Shell Programming and Scripting

validate input

the user inputs names that have to be inside square brackets I want to check if the user puts the brackets and if not ask him to re-enter the names (9 Replies)
Discussion started by: DDoS
9 Replies

4. UNIX for Dummies Questions & Answers

BASH validate user input

Hey, im trying to validate a user input and need some help. The input needs to be just a single letter. Im using a case to so this eg: read answer case $answer in *) echo "OK" ;; *) echo "This is a number" read answer ;; *) echo... (2 Replies)
Discussion started by: 602chrislys
2 Replies

5. Shell Programming and Scripting

Validate and sort input

Hi, This will most likely be a simple answer. Currently I have a situation where my script will be sent various options: -o1 -o2 -oe3@somthing.com Now, if I want to run a certain command based on the option I am sent, I am doing the following. for o in $(echo $options) do if ... (3 Replies)
Discussion started by: stuaz
3 Replies

6. Shell Programming and Scripting

Another validate input Question.

I'm writing a bash shell script to 'help' me post to susepaste (I can NEVER remember the time options). Here's the code: #!/bin/bash ########## # # Project : personal script. # Started : Wed Aug 03, 2011 # Author : Habitual # Description : susepaste c-li script with user... (5 Replies)
Discussion started by: Habitual
5 Replies

7. Shell Programming and Scripting

Epic - Validate input size

Is there an easy way to validate an input field size. Let us say a script is asking to enter 10 digits mobile number, how do I write a script to validate it is numeric and is 10 digits in length? I just need an easy way w/o using looks ...etc. Is there such a away ? Here is what I have so far... (6 Replies)
Discussion started by: mrn6430
6 Replies

8. Shell Programming and Scripting

How to validate user's input..?

$Input_filename=$ARGV; if (!-d $Input_filename && ! -e $Input_filename) { print "USAGE: Please enter '$ABCD/def/dsed.txt' as an arguement \n"; exit; } 1. Input Is suppose to be something like "$ABCD/def/dsed.txt". if the input is wrong the script should throw an ERROR message.... (2 Replies)
Discussion started by: Rashid Khan
2 Replies

9. Shell Programming and Scripting

Validate input files and update

We have a job which we need to run on daily bases, before loading data in a table we need to validate whether the input file is received or not.Inputfile formatsrc_sps_d_Call_Center_Reporting_yyyymmdd_01.dat SPS-Service nameYYYY-yearMM-MonthDD-dayLike above we will get n number of files for... (1 Reply)
Discussion started by: katakamvivek
1 Replies

10. Shell Programming and Scripting

Validate input files daily

We have a job which we need to run on daily bases, before loading data in a table we need to validate whether the input file is received or not. Daily client will place the files in a particular path.Below files which I need to process for 04/01/2013(Load date).... (2 Replies)
Discussion started by: katakamvivek
2 Replies
apertium-validate-acx(1)												  apertium-validate-acx(1)

NAME
apertium-validate-acx - This application is part of ( apertium ) This tool is part of the apertium open-source machine translation toolbox: http://www.apertium.org. SYNOPSIS
apertium-validate-acx <input_file> DESCRIPTION
This is an intermediate tool from Apertium level 2 engine. You should never have to use it independently. It is a script that validates a set of structural acx rules against the apertium structural acx rules RNG using the xmllint utility. OPTIONS
It has no options. FILES
input_file A acx.xml file acx.rng The DTD used to validate the input file. SEE ALSO
apertium-gen-modes(1), apertium-gen-oldbil(1), apertium-interchunk(1), apertium-validate-modes(1), apertium-validate-interchunk(1), aper- tium-validate-postchunk(1). BUGS
Lots of them...lurking in the dark and waiting for you! AUTHOR
Copyright (c) 2005-2007 Universitat d'Alacant / Universidad de Alicante. This is free software. You may redistribute copies of it under the terms of the GNU General Public License <http://www.gnu.org/licenses/gpl.html>. 2006-03-11 apertium-validate-acx(1)
All times are GMT -4. The time now is 11:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy