Check for special characters in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check for special characters in a script
# 1  
Old 12-18-2006
Check for special characters in a script

Hi All,

In my shell script, i'm checking the date input against few constraints. It should be of YYYYMMDD format and the script should prompt the user with error message, if less than 8 digits are present or input contains special characters (*,&,%,^,$ ...etc). The script i'm using is given below.

Code:
#! /bin/sh
year=`echo $1 | cut -c 1-4`
month=`echo $1 | cut -c 5,6`
day=`echo $1 | cut -c 7,8`
if [`echo "$1\c" | wc -c` -ne 8 ]
then
echo "Invalid date, please re-enter"
exit
elif [ $year -gt 0 -a $month -gt 0 -a $month -le 12 -a $day -gt 0 -a $day -le 31 ];then
echo $year $month $day
else
echo "Invalid date"
fi

But, if the input contains special characters and the size of the input is 8, the script shows error like

Code:
ksh: $%#$:  not found
Invalid date, please re-enter
^C
[2] +  Done(127)               ./shellpgm36.sh %^&$%#$&
[1] +  Done                    ./shellpgm36.sh %^&$%#$&

Before this, the snippet i tried was

Code:
if [ $# -ne 0 ]
then
dateval=`echo $1 | grep '^[0-9]\{8\}$'`
else
 echo "Invalid date,please re-enter in YYYYMMDD format"
 exit
fi

This also fails for special characters.

How can i check against special characters? If the input contains operators like *,!,/,+... what will happen?

Any help will be appreciated.

Thanks in advance
Sumesh
sumesh.abraham
# 2  
Old 12-18-2006
echo $1 | grep "^[0-9]\{8\}" > /dev/null

if [[ $? -ne "0" ]]
then
exit
fi

use double codes in place of single

Last edited by aju_kup; 12-18-2006 at 08:12 AM..
# 3  
Old 12-18-2006
Aju,

Thanks for the reply.

I tried echo $1 | grep "^[0-9]\{8\}", still i get the same error.

Cheers,
Sumesh
sumesh.abraham
# 4  
Old 12-18-2006
can u pls show the script
# 5  
Old 12-18-2006
Here's the script.

Code:
#! /bin/sh
#! /bin/sh

dateval=`date +%Y%m%d`
dateinput=$dateval
if [ $# -eq 0 ]
then
dateval=`date +%Y%m%d`
echo $dateval
exit
fi
if [ $# -ne 0 ]
then
dateval=`echo $1 | grep '^[0-9]\{8\}$'`
if [ $? -ne 0 ]
then
 echo "Invalid date,please re-enter in YYYYMMDD format"
 exit
fi
fi

Thanks,
Sumesh
sumesh.abraham
# 6  
Old 12-18-2006
dont change the script ... try same as i posted

if [ $# -eq 0 ]
then
dateval=`date +%Y%m%d`
echo $dateval
exit
fi
if [ $# -ne 0 ]
then
echo $1 | grep "^[0-9]\{8\}" > /dev/null
if [ $? -ne 0 ]
then
echo "Invalid date,please re-enter in YYYYMMDD format"
exit
fi
fi

-----------

in your script ur are checking the exit status of assignment, not for grep command becouse at last assignment happening.
# 7  
Old 12-18-2006
Aju,

I tried to execute the modified script with input as ^%$@#&*, but still got the same error.

./DateValidation.sh ^%$@#&*
[1] 9653
ksh: 0: not found

Apart from this, removing the assignment statement

Code:
dateval=`echo $1 | grep "^[0-9]\{8\}$"`

by

Code:
echo $1 | grep "^[0-9]\{8\}" > /dev/null

result in displaying the value which i don't want.

Can u suggest any pattern for special characters?

Thanks,
Sumesh
sumesh.abraham
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tcsh script - sed command for special characters

Hi, I am using sed command to replace following line in gz file- sed -n 's/""COS_12_TM_1" ( 1101110101001001010011110100000010110100010010000000100000000010XX010000000 )"/""COS_12_TM_1" ( 110111010100100101001111MM00000010110100010010000000100000000010XX010000000 )"/g' filename.gz $x=... (4 Replies)
Discussion started by: Preeti Chandra
4 Replies

2. Shell Programming and Scripting

Need HELP with AWK split. Need to check for "special characters" in string before splitting the file

Hi Experts. I'm stuck with the below AWK code where i'm trying to move the records containing any special characters in the last field to a bad file. awk -F, '{if ($NF ~ /^|^/) print >"goodfile";else print >"badfile"}' filename sample data 1,abc,def,1234,A * 2,bed,dec,342,* A ... (6 Replies)
Discussion started by: shell_boy23
6 Replies

3. Shell Programming and Scripting

expect script with special characters?

Hello all, I'm writing an expect script that will connect to an IMAP server and issue IMAP commands. The problem is that some of the text I need to send includes "quotes" and also !@#$%^&* special characters. For example, my password is VFR$5tgb but I cannot "send" this because Expect doesn't... (2 Replies)
Discussion started by: lupin..the..3rd
2 Replies

4. Shell Programming and Scripting

script to tail file; problem with awk and special characters

Trying to use code that I found to send only new lines out of a log file by doing: while :; do temp=$(tail -1 logfile.out) awk "/$last/{p=1}p" logfile.out #pipe this to log analyzer program last="$temp" sleep 10 done Script works fine when logfile is basic text, but when it contains... (2 Replies)
Discussion started by: moo72moo
2 Replies

5. 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

6. Shell Programming and Scripting

Check input for lenght, special characters and letter case

I made menu script for users so they can run other script without going in shell just from menu. But i must control their input. These are criteria: Input must have 4 signs First two signs are always lower case letters Input shall not have some special signs just letters and numbers ... (1 Reply)
Discussion started by: waso
1 Replies

7. Shell Programming and Scripting

Single/Multiple Line with Special characters - Find & Replace in Unix Script

Hi, I am creating a script to do a find and replace single/multiple lines in a file with any number of lines. I have written a logic in a script that reads a reference file say "findrep" and populates two variables $FIND and $REPLACE print $FIND gives Hi How r $u Rahul() Note:... (0 Replies)
Discussion started by: r_sarnayak
0 Replies

8. Shell Programming and Scripting

SED script to backslash special characters

I have a shell script that I have written to be a kind of to-do/notepad that's quickly executable from the command line. However, special characters tend to break it pretty well. Ie: "notes -a This is an entry." works fine. "notes -a This is (my) entry." will toss back a bash syntax error on... (5 Replies)
Discussion started by: skylersee
5 Replies

9. UNIX for Dummies Questions & Answers

Need help to escape special characters in Korn shell script

Hi, I would like to display the following message from my shell (Korn) script Copy "old_file.txt" to "new_file.txt" My code looks as follows print "Copy "old_file.txt" to "new_file.txt"" However, when I execute the script, I get the following output Copy old_file.txt to... (6 Replies)
Discussion started by: rogers42
6 Replies

10. Shell Programming and Scripting

Special Character Check in Shell script

Hi, I'm currently working on a project that requires parsing xml file. One of the field in the xml is shown below (don't remember exactly): <variable="ITEM">12345678</variable> I coded my script keeping in mind that the value denoted in bold will always be a number. After getting just the... (1 Reply)
Discussion started by: mradul_kaushik
1 Replies
Login or Register to Ask a Question