Help with Passing argument and testing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Passing argument and testing
# 1  
Old 02-28-2010
Power Help with Passing argument and testing

Hi all

First of all thanks for everyone to read by doubt.Am beginner in shell scripting
Following are my doubts

i have to pass an argument to shellscript how can i do that

second i have to test the argument and shows error when nothing is passes

third i have to match exact argument and pass to next steps.Below are the example of my script

checkpair.sh i have to pass argument either 1 or 11

but if statement should only match for 1 or 11 not anyother number

I need this helpSmilie
# 2  
Old 02-28-2010
Quote:
Originally Posted by zeebala1981
...
i have to pass an argument to shellscript how can i do that
second i have to test the argument and shows error when nothing is passes
third i have to match exact argument and pass to next steps.Below are the example of my script
checkpair.sh i have to pass argument either 1 or 11
but if statement should only match for 1 or 11 not anyother number
...
Something like this ?

Code:
$ 
$ cat -n myscript.sh
     1  #!/bin/bash
     2  if [ $# != 1 ]
     3  then
     4    echo "You must pass exactly one argument to this script!"
     5    exit 1
     6  fi
     7  if [[ $1 != 1 && $1 != 11 ]]
     8  then
     9    echo "The input parameter must be 1 or 11!"
    10    exit 2
    11  fi
    12  echo "You have passed 1 or 11 to this script!"
    13
$ 
$ ./myscript.sh
You must pass exactly one argument to this script!
$ 
$ ./myscript.sh 10 20 30
You must pass exactly one argument to this script!
$ 
$ ./myscript.sh 99
The input parameter must be 1 or 11!
$ 
$ ./myscript.sh 1
You have passed 1 or 11 to this script!
$ 
$ ./myscript.sh 11
You have passed 1 or 11 to this script!
$ 
$

tyler_durden
# 3  
Old 02-28-2010
MySQL

Quote:
Originally Posted by durden_tyler
Something like this ?

Code:
$ 
$ cat -n myscript.sh
     1  #!/bin/bash
     2  if [ $# != 1 ]
     3  then
     4    echo "You must pass exactly one argument to this script!"
     5    exit 1
     6  fi
     7  if [[ $1 != 1 && $1 != 11 ]]
     8  then
     9    echo "The input parameter must be 1 or 11!"
    10    exit 2
    11  fi
    12  echo "You have passed 1 or 11 to this script!"
    13
$ 
$ ./myscript.sh
You must pass exactly one argument to this script!
$ 
$ ./myscript.sh 10 20 30
You must pass exactly one argument to this script!
$ 
$ ./myscript.sh 99
The input parameter must be 1 or 11!
$ 
$ ./myscript.sh 1
You have passed 1 or 11 to this script!
$ 
$ ./myscript.sh 11
You have passed 1 or 11 to this script!
$ 
$

tyler_durden

tyler

Thanks for the timely help its really working.Could you explain the following line alone please

if [[ $1 != 1 && $1 != 11 ]]
# 4  
Old 03-01-2010
Quote:
Originally Posted by zeebala1981
...
Could you explain the following line alone please

if [[ $1 != 1 && $1 != 11 ]]
Nope I won't, sorry. I can, but I won't.
Check the man page of your shell; it's all there. Nothing is made up.
Since you are a beginner, it's essential that you get into the habit of looking up the man page or info page as frequently as needed.

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing a second argument

I am trying to pass a second argument like so: if ] then export ARG2=$2 else message "Second argument not specified: USAGE - $PROGRAM_NAME ARG1 ARG2" checkerror -e 2 -m "Please specify if it is a history or weekly (H or W) extract in the 2nd argument" fi however, it always goes... (4 Replies)
Discussion started by: MIA651
4 Replies

2. Shell Programming and Scripting

Passing argument not retrieving.

Hi I have a script which am trying to pass an argument which am trying to call using $1 but its not taking the value inside the if loop as it showing the error as if: Empty if.. Any help on this will be helpful. #!/usr/bin/csh echo $1 if ('$1' == "pp") then echo "Printing $1" endif (4 Replies)
Discussion started by: rogerben
4 Replies

3. Shell Programming and Scripting

Argument passing

How to pass the alphabet character as a argument in case and in if block? ex: c=$1 if a-z ]] then echo "alphabet" case $1 in a-z) echo "the value is a alphabet" edit by bakunin: please use CODE-tags. We REALLY mean it. (9 Replies)
Discussion started by: Roozo
9 Replies

4. Shell Programming and Scripting

Help with passing argument

Hi, I have a script that is scheduled with cron and runs every night. The cron part looks like this: 00 20 * * 0,1,2,3,4,5,6 /usr/local/bin/BACKUP TBTARM HOT DELETE My issue is with the 3rd parameter. Somewhere in the script, i want to tell the script to delete some files if the 3rd... (7 Replies)
Discussion started by: dollypee
7 Replies

5. Shell Programming and Scripting

Passing user argument

Hi all: I'm trying to pass an argument to a command but it's being difficult. #!/bin/bash set -xv if ; then echo "More than 1 argument entered" echo "Please enter a month using 3 character names, ie, Jan, Mar, Apr, Dec" && exit 1 fi if ; then echo "Please enter a month using... (2 Replies)
Discussion started by: raggmopp
2 Replies

6. Shell Programming and Scripting

passing argument in script?

hi, I want to implement some function to perform following task if ; then $TEXT = "Data_0" else $TEXT = $1 fi if ; then $Lines = 45 else $Lines = $2 fi Kindly suggest, thanks (11 Replies)
Discussion started by: nrjrasaxena
11 Replies

7. Shell Programming and Scripting

passing an option as an argument!

Hi Folks I have got to the point where I can specify the arguments but how to pass an option is still mystery to me. Example: temp.csh a b c d set temp1 = $argv set temp2 = $argv set temp3 = $argv echo $temp1 a echo $temp2 b echo $temp3 c d I WANT: temp.csh a b c d -S 1 set temp1... (2 Replies)
Discussion started by: dixits
2 Replies

8. Shell Programming and Scripting

passing Argument

Hi All, i have script like below.. echo "1) first option" echo "" echo "2) second option" echo "" echo "*) please enter the correct option" read select case $select in 1) echo "first option selected" ;; 2) echo "second option selected" ;; *) echo "please enter the correct... (4 Replies)
Discussion started by: Shahul
4 Replies

9. Shell Programming and Scripting

passing argument into awk

i'm trying to pass a numerical argument with function xyz to print specfic lines of filename, but my 'awk' syntax is incorrect. ie xyx 3 (prints the 3rd line, separated by ':' of filename) function xyz() { arg1=$1 cat filename | awk -F: -v x=$arg1 '{print $x}' } any ideas? (4 Replies)
Discussion started by: prkfriryce
4 Replies

10. Programming

Thread Argument Passing

#include <stdio.h> #include <pthread.h> #define NUM_THREADS 4 /* function to be executed by the new thread*/ void *PrintHello(void * threadid) { printf("\n %3d:Hello World!\n",threadid); pthread_exit(NULL); } int main(int argc, char * argv) { int *taskids; int... (2 Replies)
Discussion started by: narom
2 Replies
Login or Register to Ask a Question