how to do validation the input requirement with if/else


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to do validation the input requirement with if/else
# 1  
Old 01-11-2011
how to do validation the input requirement with if/else

Hello Everyone,

I have the following script;
------------------------------------------------------------------
Code:
#!/bin/ksh
set HSC=$1
set SEX=$2
set EXP='export ORACLE_SID=AFISDB'

if ($#argv !=2);
then
     echo Usage $0 HSC SEX
else

date > modify_gender_trace.txt
$EXP
echo "Modifying Gender for HSC: $HSC to Gender : $SEX"
sqlplus fldadmin/fld << EOF >> modify_gender_trace.txt
update JPN_DATA set SEX='$SEX' where JPN_DATA_ID in (
select JPN_DATA_ID from JPN_DATA_LINK where REPRESENTATION_ID in
(select REPRESENTATION_ID from DPI_PERSON_REPRESENTATION where PIN='$HSC'));
commit;
quit
echo "Successfully Commited" >> modify_gender_trace.txt
EOF
fi


-----------------------------------------------------------------

I will launch this file (let says: file abc.ksh) with two inputs.
For example:

Code:
./abc.ksh 8456201 M



If there is no input or only one input,
the script will tell the user that
Usage abc.ksh HSC SEX.

When i launched the script, it will give error saying if: Expression syntax.

I am not sure what is wrong with the script.Smilie

I hope someone can help me with this.

Many thanks for the help.
Smilie

Moderator's Comments:
Mod Comment Please use code tags for listings and console output.

Last edited by pludi; 01-11-2011 at 07:32 AM..
# 2  
Old 01-11-2011
If condition should be:
Code:
if  [ $# -ne 2 ]

This User Gave Thanks to anurag.singh For This Post:
# 3  
Old 01-11-2011
There are several more issues in the script.

The first line says it is a "ksh" script. The "set" syntax is from "csh" and will not work with "ksh".

The last "echo" should be after the "EOF" line because it is a unix command not an Oracle command.
The script assumes that "${ORACLE_HOME}" is already set and that "sqlplus" can be found in "${PATH}".
I can't comment on your Oracle commands or environment.

Assuming you are actually writing the script in "ksh":

Code:
#!/bin/ksh
HSC="$1"
SEX="$2"
export ORACLE_SID="AFISDB"

if [ $# -ne 2 ]
then
     echo "Usage `basename $0` HSC SEX"
     exit
fi

date > modify_gender_trace.txt
echo "Modifying Gender for HSC: $HSC to Gender : $SEX"

sqlplus fldadmin/fld << EOF >> modify_gender_trace.txt
update JPN_DATA set SEX='$SEX' where JPN_DATA_ID in (
select JPN_DATA_ID from JPN_DATA_LINK where REPRESENTATION_ID in
(select REPRESENTATION_ID from DPI_PERSON_REPRESENTATION where PIN='$HSC'));
commit;
quit
EOF
echo "Successfully Commited" >> modify_gender_trace.txt

This User Gave Thanks to methyl For This Post:
# 4  
Old 01-12-2011
Methyl and Anurag Singh, thousand thanks man.

Done it !!

But I just thought of something, how to do validation of the two inputs ?
For example; the FIRST input which is the HSC must be consisted of 12 numbers while the SEX should be either M or F. Only one alphabet.

or do I need to create a new thread for this question ?
# 5  
Old 01-13-2011
It would be better to start a new thread.

Don't forget to show sample valid and invalid data and to make the validation rules clear. Examples help.

Also post what action to take on invalid data. Convention is to display an error then exit. What do you want to happen if the data is invalid?

Last edited by methyl; 01-13-2011 at 08:07 AM.. Reason: paste corruption
# 6  
Old 01-13-2011
Quote:
Originally Posted by wingcross
For example; the FIRST input which is the HSC must be consisted of 12 numbers
if you have at least bash 3.2+, you can use regex
Code:
[[ $FIRST =~ "^[0-9]{12}$" ]] && echo "ok"

Quote:
Originally Posted by wingcross
while the SEX should be either M or F. Only one alphabet.

or do I need to create a new thread for this question ?
for this, you can use regex as above, or use simple case/esac

Code:
case "$SEX" in
  [Mm] ) echo "Male";;
  [Ff] ) echo "Femle";;
esac

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Input Email ID Validation

Hi Experts, I've a script where users will input the email id. I want to loop the script until the user enter proper email id. Its validating for all condition and error out if any wrong data, but for one condition alone its not erroring out. Capture_EmailID() { echo -e "Please Enter... (1 Reply)
Discussion started by: senthil.ak
1 Replies

2. Windows & DOS: Issues & Discussions

Put the numeric validation in user input when value is 5.1.2.3

Hi I need to put the validation in batch script when user will enter the build number it should be numeric.I can put the validation for numeric values but there is .(dot) in number so it would not take it as numeric. Is it possible we can store it in variable and remove the .(dot) from the... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

3. Windows & DOS: Issues & Discussions

Validation in user input in batch script

I need to insert the validation in my batch script.When user enter the value it should be numeric+minimum length should be 2 for e.g. 02,03 if he puts 1a,1A,2a3 t hen should print the message that it is wrong and print te message enter valid value. Echo RC is in format of 02 set /p... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

4. Shell Programming and Scripting

perl: How to improve with minimal validation of its input??

The Code: #!/usr/bin/perl use strict; use warnings; print "Please enter numbers, separated by commas: "; my $data=<STDIN>; chomp $data; my @dataset=split(/,/, $data); my $sum = 0; foreach my $num (@dataset) { $sum += $num; } my $total_nums = scalar(@dataset); my $mean =... (1 Reply)
Discussion started by: 300zxmuro
1 Replies

5. Shell Programming and Scripting

Input validation of file

Hi, I'm trying to do input validation for a file. If the user fails to enter a file name, the script should prompt for the file name or exit. I'm using this script to integrate with a bigger script which does ftp of the files which are "read". #!/bin/ksh echo "enter a file name" if then... (16 Replies)
Discussion started by: sam_bd
16 Replies

6. Shell Programming and Scripting

Input Validation of comma separated values

Hello all, I am working on a script and have the first part solved of numerical input validation. Below the code validates that the input is a numerical value between 100 and 1000. If not, it errors out. Now I need to be able to read values separated by a comma. For example, instead of my... (5 Replies)
Discussion started by: LinuxRacr
5 Replies

7. Shell Programming and Scripting

Command line user input validation

Hi guys, I have a piece of snippet below which asks the user to input some numbers if isDatasubEnabled && isReconEnabled; then echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit" elif isDatasubEnabled &&... (4 Replies)
Discussion started by: pyscho
4 Replies

8. Shell Programming and Scripting

Input validation ?

I am trying to validate user input, at the moment what i have is as below :- echo "\n\tPlease, enter package name to import: \c" read PACKAGE So far this works perfect , But if the user does not input any thing it stalls :( What I need is, If the user does not enter the name of the... (9 Replies)
Discussion started by: systemali
9 Replies
Login or Register to Ask a Question