Sponsored Content
Top Forums Shell Programming and Scripting Regex to validate parameter for sleep Post 303025524 by Ralph on Monday 5th of November 2018 08:10:03 AM
Old 11-05-2018
Regex to validate parameter for sleep

I'm trying to use a regular expression to validate a value for sleep entered by the user. The test should fail all negative values and 0 but let pass all combinations of + . and digits that would amount to a valid parameter for sleep.
Examples for valid: 1, 1.5, .5, 0.5, +1, +.5, +1.3 etc.
Examples for invalid: 0, .0, 0., 0.0, +0, +.0, +0, +0.0 etc. and any string starting with a -

I wrote a small program to test the regular expressions I'm using now - two expressions combined in a test statement. While looking a bit contrived they almost do the job except that strings like .1.1 still pass the test as valid.

Can someone help me take the final step?

Code:
#!/bin/bash

if [ $# -gt 0 ] ; then # user entered parameter for sleep
   # check fails if it's not a positive floating point number
   if ! [[ $1 =~ ^\+?\.?[0-9]+\.?[0-9]*$ ]] || [[ $1 =~ ^[\+-]?0?\.?0*$ ]]; then
      echo invalid value for sleep: $1
   else
     echo valid: $1
   fi
fi

Thanks.

------ Post updated at 01:08 PM ------

I found a solution by adding an && clause to the if condition:

Code:
if ! [[ $1 =~ ^\+?\.?[0-9]+\.?[0-9]*$ ]] || [[ $1 =~ ^[\+-]?0?\.?0*$ ]] && ! [[ $1 =~ ^\.*$ ]

but it looks even more contrived now.

Is there a way to solve the problem within one regular expression? Or something much shorter? (I'm not overly familiar with regular expressions and I expect an expert to come up easily with something shorter, humiliatingly shorter.)

------ Post updated at 01:10 PM ------

OMG. There's a ] missing at the end.
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how do I make dynamic parameter names? Or get the value of a parameter evaluated twi

Say I write something like the following: var1=1 var2=2 for int in 1 2 do echo "\$var$int" done I want the output to be: 1 2 Instead I get something like: $var1 $var2 (2 Replies)
Discussion started by: Awanka
2 Replies

2. Shell Programming and Scripting

Wrapping 'sleep' with my 'resleep' function (Resettable sleep)

This is a very crude attempt in Bash at something that I needed but didn't seem to find in the 'sleep' command. However, I would like to be able to do it without the need for the temp file. Please go easy on me if this is already possible in some other way: How many times have you used the... (5 Replies)
Discussion started by: deckard
5 Replies

3. Shell Programming and Scripting

Using regex in sed to validate the length of an entry

I'm having trouble using sed to validate the length of an entry. I want to have a user enter a phone number of either length 7, 10 or 11. Only numbers are allowed. Does anyone know how to do this? Here's the code I have so far. It only validates that numbers are entered but not the length. ... (1 Reply)
Discussion started by: snag49ers
1 Replies

4. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

5. Shell Programming and Scripting

Command that takes one parameter and then searches for the passed in parameter

Hi I am looking for a unix command or a small shell script which can takes one parameter and then searches for the passed in the parameter in any or all files under say /home/dev/ Can anyone please help me on this? (3 Replies)
Discussion started by: pankaj80
3 Replies

6. Shell Programming and Scripting

Passing parameter to script, and split the parameter

i am passing input parameter 'one_two' to the script , the script output should display the result as below one_1two one_2two one_3two if then echo " Usage : <$0> <DATABASE> " exit 0 else for DB in 1 2 3 do DBname=`$DATABASE | awk -F "_" '{print $1_${DB}_$2}` done fi (5 Replies)
Discussion started by: only4satish
5 Replies

7. Shell Programming and Scripting

Resolving a parameter which is passed as parameter

Hi, I have the following files. ->cat scr.sh export TMP_DIR=/home/user/folder1 export TMP_DIR_2=/home/user/folder2 while read line do cat "$line" done<file_list.dat ------------------------ -> cat file_list.dat $TMP_DIR/file1.txt $TMP_DIR_2/file2.txt --------------------------- -> cat... (6 Replies)
Discussion started by: barath
6 Replies

8. Shell Programming and Scripting

Call Script with Parameter (that has another parameter)

Hi. How do I achieve this sh /EDWH-DMT02/script/MISC/exec_sql.sh "@/EDWH-DMT02/script/others/CSM_CKC/Complete_List.sql ${file_name}" Complete_List.txt The /EDWH-DMT02/script/MISC/exec_sql.sh has two parameters and it's working fine with this sh /EDWH-DMT02/script/MISC/exec_sql.sh... (7 Replies)
Discussion started by: aimy
7 Replies

9. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies
regcmp(1)							   User Commands							 regcmp(1)

NAME
regcmp - regular expression compile SYNOPSIS
regcmp [-] filename... DESCRIPTION
The regcmp command performs a function similar to regcmp and, in most cases, precludes the need for calling regcmp from C programs. Bypass- ing regcmp saves on both execution time and program size. The command regcmp compiles the regular expressions in filename and places the output in filename.i. OPTIONS
- If the - option is used, the output is placed in filename.c. The format of entries in filename is a name (C variable) followed by one or more blanks followed by one or more regular expressions enclosed in double quotes. The output of regcmp is C source code. Compiled regular expressions are represented as extern char vectors. filename.i files may thus be #included in C programs, or filename.c files may be compiled and later loaded. In the C program that uses the regcmp output, regex(abc,line) applies the regu- lar expression named abc to line. Diagnostics are self-explanatory. EXAMPLES
Example 1: Examples of the regcmp command. name "([A-Za-z][A-Za-z0-9_]*)$0" telno "({0,1}([2-9][01][1-9])$0){0,1} *" "([2-9][0-9]{2})$1[ -]{0,1}" "([0-9]{4})$2" The three arguments to telno shown above must all be entered on one line. In the C program that uses the regcmp output, regex(telno, line, area, exch, rest) applies the regular expression named telno to line. ENVIRONMENT VARIABLES
A general description of the usage of the LC_* environmental variables can be found in environ(5). LC_CTYPE Determines how regcmp handles characters. When LC_CTYPE is set to a valid value, regcmp can display and handle text and filenames containing valid characters for that locale. LC_MESSAGES Determines how diagnostic and informative messages are presented. This includes the language and style of the messages, and the correct form of affirmative and negative responses. In the "C" locale, the messages are presented in the default form found in the program itself (in most cases, U.S. English). ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWtoo | +-----------------------------+-----------------------------+ |CSI |Enabled | +-----------------------------+-----------------------------+ SEE ALSO
regcmp(3C), attributes(5), environ(5) SunOS 5.10 Dec 20 1996 regcmp(1)
All times are GMT -4. The time now is 09:07 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy