Validating commandline argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Validating commandline argument
# 1  
Old 03-17-2010
Bug Validating commandline argument

Hi,
I am calling a script script2.shl from script1.shl as below

script2.shl "TABLE_NAME" -r 10

In that I have to validate the parameter 4.

i.e : it should be only 10 20 30 40 50

I know that I can do it by checking like below
Code:
if [[ $4 -eq 10 -o $4 -eq 20 -o $4 -eq 30 -o $4 -eq 40 -o $4 -eq 50 ]]; then
 echo "TRUE"
else
  echo "FALSE"
fi

But my question here is, can I check all these in a single statement and not like giving "-o" for each value.
# 2  
Old 03-17-2010
Quote:
Originally Posted by mr_manii
Hi,
I am calling a script script2.shl from script1.shl as below

script2.shl "TABLE_NAME" -r 10

In that I have to validate the parameter 4.

i.e : it should be only 10 20 30 40 50

I know that I can do it by checking like below
Code:
if [[ $4 -eq 10 -o $4 -eq 20 -o $4 -eq 30 -o $4 -eq 40 -o $4 -eq 50 ]]; then
 echo "TRUE"
else
  echo "FALSE"
fi

But my question here is, can I check all these in a single statement and not like giving "-o" for each value.
Code:
case $4 in
     [12345]0) echo true;;
     *) echo false;;
esac

# 3  
Old 03-17-2010
Use the below if condition.

Code:
if [ `sed '/^[1-5]0/!d' <<<$4` ]
then
         echo "TRUE"
else
           echo "FALSE"
fi

# 4  
Old 03-17-2010
Quote:
Originally Posted by thillai_selvan
Code:
if [ `sed '/^[1-5]0/!d' <<<$4` ]


The external command (sed) is slow and unnecessary.

The "here string" (<<<) is not portable.

That test will succeed even if $4 is 1000.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking commandline

mymk target How to check is on commandline or no? Cannot to find out ;( Know that I need to use if.....new in shell sorry Please use code tags next time for your code and data. (2 Replies)
Discussion started by: Manueldo
2 Replies

2. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

3. OS X (Apple)

Opening Applications from the commandline

as you probably/may know, one can open any application from the application folder with the command open -a program_name I found that out today and so I created aliases in .bash_profile and they work. alias safari="open -a safari" alias gimp="open -a gimp" alias seamonkey="open -a... (0 Replies)
Discussion started by: butterbaerchen
0 Replies

4. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

5. Shell Programming and Scripting

Using curl in commandline

Hi, I am using curl to hit a url using http in solaris 10 using commandline, I want to transfer an attachment(using multipart curl -F) also as a part of the request. If anyone has used kindly help me with the syntax. I am using below command: /usr/local/bin/curl -v... (1 Reply)
Discussion started by: manishmaha
1 Replies

6. Shell Programming and Scripting

Passing commandline argument to a function

Hi, I have 2 ksh scripts. Script1.ksh contains function definition. script1.ksh function f1() { while getopts a:c: args do case $args in a) ARG1=$OPTARG ;; c) ARG2=$OPTARG ;; \?) echo "Error no valid Arguments passed" esac done echo $ARG1 echo $ARG2 script2.sh (2 Replies)
Discussion started by: siba.s.nayak
2 Replies

7. Shell Programming and Scripting

Handling values with space while passing commandline argument from wrapper script in KSH

Hi there, I have a wapper script which passes the argument from command prompt to inner script.. It works fine as long as the argument containing single word. But when value contains multiple word with space, not working as expected. I tried my best, couldn't find the reason. Gurus, pls.... (2 Replies)
Discussion started by: kans
2 Replies

8. UNIX for Dummies Questions & Answers

list only directories in commandline.

I want to list only directories in the command line using ls command. Can anyone please help me. (9 Replies)
Discussion started by: jhmr7
9 Replies

9. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies
Login or Register to Ask a Question