Errors in if condition validations-Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Errors in if condition validations-Shell Script
# 1  
Old 10-23-2012
Errors in if condition validations-Shell Script

Hello All,
i am facing difficulty in validating the values,
kindly help me in resolving the issue.
Thanks a lot in advance.

-Chandra

Script:Test.sh
Code:
#! /bin/sh

# ***************************************************************************

# Function to display help
function usage()
{
    echo ""
    echo "  Script to transfer data from Source Site"
    echo "  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
    echo ""
    echo "  -h -help           	    Display Help"
	echo "  -site=Target Site Name  Target Site Name[comma seperated values is allowed]"
    echo "  -file=File Path   	    File Contains the List which needs to be shared to Target Site"
	echo "  -include=True/true  Argument has to be used in conjuction with -Item_Id & -Rev_Id"
	echo "  -id=Id             Id Which needs to be transfered with Include Switch"
	echo "  -rev= Revision Id    Rev of the Id"
    echo ""
	echo "   Example:1"
    echo "   Test.sh -site=ABCD -file=./ItemRev_list.txt"
    echo ""
    echo "   Example:2"
    echo "   Test.sh -site=XYZ -include=True -id=123 -rev=A"	
    echo ""
	echo "   Example:3"
    echo "   Test.sh -site=ABCD,XYZ -include=True -id=123 -rev=A"	
    echo ""
    echo "  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
    echo ""
}

# **********************************************************

if [ $# -eq 0 ]; then
echo "" echo "No Parameter provided" usage exit 1
fi # Parse command line options for PARAM; do
if [[ "$PARAM" = -h* ]]; then
usage echo "" exit 0
fi if [[ "$PARAM" = -include=* ]]; then
INCLUDE=$(echo $PARAM | cut -d"=" -f2) echo "INCLUDE : $INCLUDE"
fi if [[ "$PARAM" = -id=* ]]; then
ITEM_ID=$(echo $PARAM | cut -d"=" -f2) echo "ITEM_ID : $ITEM_ID"
fi if [[ "$PARAM" = -rev=* ]]; then
REV_ID=$(echo $PARAM | cut -d"=" -f2) echo "REV_ID : $REV_ID"
fi if [[ "$PARAM" = -site=* ]]; then
SITE=$(echo $PARAM | cut -d"=" -f2) echo "SITE : $SITE"
fi if [[ "$PARAM" = -file=* ]]; then
FILE=$(echo $PARAM | cut -d"=" -f2) echo "FILE : $FILE"
fi
done #include parameter value validation if [[ ( "$INCLUDE" -ne "True" ) || ( "$INCLUDE" -ne "true" ) ]]; then
echo "include will accept only True or true values ..." echo "" usage exit 0
fi #checking if include is mentioned along with file option #include should not be used with file option if [[ ( "$FILE" != "" ) && ( "$INCLUDE" -eq "true" ) ]]; then
echo "File option is not allowed with Include Switch 1" echo "" usage exit 0
elif [[ ( "$FILE" != "" ) && ( "$INCLUDE" -eq "True" ) ]]; then
echo "File option is not allowed with Include Switch 2" echo "" usage exit 0
fi #file should be existing with non zero size if [[ "$FILE" != "" ]]; then
if [ -e $FILE -a -s $FILE ]; then
echo ""
else
echo "$FILE is not existing or empty file" echo "" exit 0
fi
fi #include should be used along with item_id & rev_id parameters if [[ ( "$INCLUDE" -eq "True" ) || ( "$INCLUDE" -eq "true" ) ]]; then
if [[ ( "$ITEM_ID" == "" ) || ( "$REV_ID" == "" ) ]]; then
echo "With include option, item_id & rev_id parameters has to be supplied..." usage exit 0
fi
fi if [[ ( "$INCLUDE" -eq "True" ) || ( "$INCLUDE" -eq "true" ) ]]; then
echo "include switch provided..." echo "....."
elif [ "$FILE" != "" ]; then
echo "File switch provided..." echo "....."
else
echo "The Script is not provided with the required arguments" echo "The below script help information explains about the required parameters..." echo "" usage exit 0
fi # EOF exit 0

Requirement:The below examples only should work, other case it should exit the script
Example:1
Code:
Test.sh -site=ABCD -file=./ItemRev_list.txt

Example:2
Code:
Test.sh -site=XYZ -include=True -id=123 -rev=A

Example:3
Code:
Test.sh -site=ABCD,XYZ -include=True -id=123 -rev=A

Example:4
Code:
Test.sh -site=ABCD,XYZ -file=./ItemRev_list.txt


Last edited by duddukuri; 12-04-2012 at 07:06 AM.. Reason: code tags
# 2  
Old 10-23-2012
I'd use case with its file wild cards meta character set. It gives the code more structure.

Is this sh or bash, because I think you need to/should ask for bash on the line 1 #! to get happy [[]] interpretation.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

While condition in shell script

while do if ;then read driverName else driverName="" fi done can anyone please explain what exactly is happening on 1st line...is it like the conditions being ORed...I have no clue about this. (4 Replies)
Discussion started by: rtagarra
4 Replies

2. Shell Programming and Scripting

How to turn off ora errors in shell script?

I have a shell script which select total count from a table and use its value in a if condition like below connect_string="username/password@tnsname" tot=`sqlplus -s $connect_string << EOF set echo off set feedback off set head off select count(*) from test_table; EOF ` if then echo... (2 Replies)
Discussion started by: vel4ever
2 Replies

3. UNIX for Dummies Questions & Answers

Interpreting Shell Script errors when called from CRON

Hi All, I am calling a series of shell scripts via CRON so everything is running as root. However, in my error log file I am seeing the following errors. Please can anyone offer any advise as to the possible causes and solution to prevent the errors from appearing. The Error 1227 seems to... (2 Replies)
Discussion started by: daveu7
2 Replies

4. UNIX for Dummies Questions & Answers

Invalid option errors running shell script

The script below fails with the following error messages: gzip: invalid option -- 'w' Try `gzip --help' for more information. mysqldump: Got errno 32 on write cp: invalid option -- 'w' Try `cp --help' for more information. rm: invalid option -- 'w' Try `rm --help' for more information. ... (1 Reply)
Discussion started by: ANNACTION
1 Replies

5. Shell Programming and Scripting

shell script for conky getting errors....

So I have a script that does a greater than or less than with a variable. My problem is that when I checked the script from the command line I was getting this error.. I am not really familiar with shell scripting. I am trying to get this script to work with conky so that it will do the... (8 Replies)
Discussion started by: krisdeniseriley
8 Replies

6. Emergency UNIX and Linux Support

Seek help on shell script syntax errors

I want to delete archivelog files that has been archived and applied from primary database to standby database. This piece of script is working in Linux server. However, I copy it to Unix server with tiny modification. It won't work and generate the error message. I have checked code carefullt... (8 Replies)
Discussion started by: duke0001
8 Replies

7. Shell Programming and Scripting

Capture makefile errors in shell script

Hi, I have a bash script which calls a few "make". I would like to know whether the makefile failed with any errors. How do I do that in the script? Thanks, S (2 Replies)
Discussion started by: suryaemlinux
2 Replies

8. Shell Programming and Scripting

Bad substitution errors in shell script

Hello, I was hoping for a second pair of eyes or a little bit of help figuring out what my error is in a script. I did some searching in the forums and didn't find anything so please forgive me if it a similar problem has been discussed before. My script accepts normal user arguments; however,... (2 Replies)
Discussion started by: Jackinthemox
2 Replies

9. Shell Programming and Scripting

Help with shell script errors

hey watsup guys i am new in the shell script world. so i need help fom you guys, i have written these two codes and they both give the same errors( expr : syntax error). Code 1 : #! /bin/sh # count1 appends an increment to a file 200 times # note that a file called numbers must be... (5 Replies)
Discussion started by: surubi_abada
5 Replies

10. Shell Programming and Scripting

Parse for errors shell script

All, I have a shell script which parses the /var/adm/messages file for errors every 15 minutes as a cron job. The script runs at 01, 16, 31, and 46 minutes every hour. The problem is if the error is encountered any time during the beginning of hour I can get paged three times. I would like to... (2 Replies)
Discussion started by: bubba112557
2 Replies
Login or Register to Ask a Question