validation against special characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting validation against special characters
# 1  
Old 06-04-2009
validation against special characters

I have a criteria like bloew.
user entered the uid like <START_UID>-<END_UID>
it menas if he enter 00001-12345
START_UID=00001 and END_UID=12345
both are separated by `-`.
I need to validate whether user entered uids like above, he should have to enter '-', otherwise error msg has to print. And need to validate whether both uids are numeric only. If he enter 00001-1234( error msg has to print. And it should accepts only one occurance of '-' symbol.
can any one give the solution.I am trying with this sol
OPTARG=$1
cnt=1;
while [ $cnt -le $length ]
do
c=$(echo "$OPTARG"|cut -c$cnt-$cnt)
if [[ $c = "-" ]]
then
FIRST_FIELD=${OPTARG%%-*}
SECOND_FIELD=${OPTARG##*-}
fi
((cnt+=1))
done
But when i enter 3663-929(
-bash: syntax error near unexpected token `('
This error am getting .

Thanx
# 2  
Old 06-04-2009
validation against special char

Hi ,
Please try below

OPTARG=$1
echo $OPTARG
len="`echo ${#OPTARG}`"
echo $len
cnt=1
while [ $cnt -le $len ]
do
c=$(echo "$OPTARG"|cut -c$cnt-$cnt)
if [[ $c = "-" ]]
then
FIRST_FIELD=${OPTARG%%-*}
SECOND_FIELD=${OPTARG##*-}
fi
((cnt+=1))
done
echo $FIRST_FIELD
echo $SECOND_FIELD
# 3  
Old 06-04-2009
Hi ,

U have sent the same code what i posted. Its not working at all..
same errors am getting. Please check once my requirements and posted code
# 4  
Old 06-05-2009
Try using case to check in input. Also, OPTARG is a shell built-in, so you might want use a different name.
Code:
$ read reply
123-456(

$ case "$reply" in [0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9]) echo good;; *) echo bad;; esac
bad

$ read reply
12345-67890

$ case "$reply" in [0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9]) echo good;; *) echo bad;; esac
good

# 5  
Old 06-05-2009
Quote:
Originally Posted by KiranKumarKarre
I have a criteria like bloew.
user entered the uid like <START_UID>-<END_UID>
it menas if he enter 00001-12345
START_UID=00001 and END_UID=12345
both are separated by `-`.
I need to validate whether user entered uids like above, he should have to enter '-', otherwise error msg has to print. And need to validate whether both uids are numeric only. If he enter 00001-1234( error msg has to print. And it should accepts only one occurance of '-' symbol.
can any one give the solution.I am trying with this sol

When you post code, please wrap it in [code] tags.
Quote:
Code:
OPTARG=$1


It's a bad habit to use variables that the shell uses. OPTARG is used with getopts.
Quote:
Code:
cnt=1;
 while [ $cnt -le $length ]


You haven't defined $length.
Quote:
Code:
                do
                c=$(echo "$OPTARG"|cut -c$cnt-$cnt)


You don't need '-$cnt', just '-c$cnt'.

In fact, you don't need cut at all. You don't even need the loop.
Quote:
Code:
                if [[ $c = "-" ]]
                then
                         FIRST_FIELD=${OPTARG%%-*}
                        SECOND_FIELD=${OPTARG##*-}
                                fi
                ((cnt+=1))
                done


Code:
range=$1
first_field=${range%%-*}
second_field=${range#*-}
case $first_field$second_field in
     *[!0-9]*) echo INVALID; exit 1 ;;
     *) echo OK ;;
esac

Quote:
But when i enter 3663-929(
-bash: syntax error near unexpected token `('
This error am getting .

Parentheses on the command line mkust be escaped (or quoted). It has nothing to do with your script; echo 3663-929( will give you the same error.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to escape all special characters?

I have an application which I am integrating with that accepts the password via a CLI. I am running in to issues with passwords that contain special characters. I tried to escape them all, but I ran in to an issue where I cannot escape the characters ' ] My attempt is as follows: $... (2 Replies)
Discussion started by: AMG1978
2 Replies

2. Homework & Coursework Questions

Protection and special characters

I am learning from the class how to protect the special characters and the script that I wrote here does not work when I am trying to pick up a single quote. It would complaint about parentheses problem. Please, someone could enlighten me. Thanks in advance, Scopiop Input file Hi, * ?... (2 Replies)
Discussion started by: scopiop
2 Replies

3. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

4. Shell Programming and Scripting

special characters

Hey guys, I'm trying to replace "]Facebook" from the text but sed 's/]Facebook/Johan/g' is not working could you please help me with that? (6 Replies)
Discussion started by: Johanni
6 Replies

5. Shell Programming and Scripting

Escaping special characters

I'm attempting a little hack to get grep to highlight (change foreground color to red) a found string. Assuming a target file "test" consisting of the word "albert": My executable "algrep" consists of this: grep $1 $2 | sed "s/$1/\\\033 And when run: algrep al test Produces this:... (2 Replies)
Discussion started by: tiggyboo
2 Replies

6. UNIX for Dummies Questions & Answers

How to see special characters?

Hi all, I was wondering how can i see the special characters like \t, \n or anything else in a file by using Nano or any other linux command like less, more etc (6 Replies)
Discussion started by: gvj
6 Replies

7. Shell Programming and Scripting

remove special characters

hello all I am writing a perl code and i wish to remove the special characters for text. I wish to remove all extended ascii characters. If the list of special characters is huge, how can i do this using substitute command s/specialcharacters/null/g I really want to code like... (3 Replies)
Discussion started by: vasuarjula
3 Replies

8. Shell Programming and Scripting

Special characters

When I open a file in vi, I see the following characters: \302\240 Can someone explain what these characters mean. Is it ASCII format? I need to trim those characters from a file. I am doing the following: tr -d '\302\240' ---------- Post updated at 08:35 PM ---------- Previous... (1 Reply)
Discussion started by: sid1982
1 Replies

9. UNIX for Dummies Questions & Answers

Substitue 'Special Characters' in VI

Hi All, I am using LATEX and need to delete all the lines in a file matching: \begin{work} I know there are several ways to do this, but I am trying to do it with the substitute command in VI. The problem is I can't get substitute to recognize the character '\'! How do I do it? ... (7 Replies)
Discussion started by: ScKaSx
7 Replies

10. UNIX for Dummies Questions & Answers

special characters

I have one file which is named ^? ( the DEL character ) I'd like to know how to rename or copy the file by using its i-node number TYIA (2 Replies)
Discussion started by: nawnaw
2 Replies
Login or Register to Ask a Question