How can i passed a parameter to a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can i passed a parameter to a file?
# 1  
Old 04-28-2009
How can i passed a parameter to a file?

i got a file called Pass1, and then i need to passed a number to my script with the '-p pass_mark' option.

Example type
Pass1 -p 18

to pass 18 to my script for comparing things, so how can i do it?
# 2  
Old 04-28-2009
If you want named parameters, check up on the getopts function (available in a ksh and bash near you)
If you only want to pass any parameter, they are automagically stored in $1 to $9 individually and $*/$@ collectively.
# 3  
Old 04-28-2009
example:

Code:
whie getopts "p:" opt
do
case $opt in
p) pass_mark = $OPTARG ;;
esac
done

In your cass to pass , just the mark, you could just run the script as
Code:
scriptname 45

and then in your script, accept
Code:
pass_mark=$1


cheers,
Devaraj Takhellambam
shift $((OPTIND -1))
# 4  
Old 04-28-2009
Quote:
Originally Posted by devtakh
example:

Code:
whie getopts "p:" opt
do
case $opt in
p) pass_mark = $OPTARG ;;
esac
done

In your cass to pass , just the mark, you could just run the script as
Code:
scriptname 45

and then in your script, accept
Code:
pass_mark=$1


cheers,
Devaraj Takhellambam
shift $((OPTIND -1))
Thank you
# 5  
Old 04-29-2009
i did somethings like that, but is not working

Code:
while getopts "p:" opt
do
case $opt in
p) pass_mark = $OPTARG ;;
esac
done

pass_mark=$1

if [ 50 -lt $1 ]
then
echo "50 is less than $1"
else
echo "50 is not less than $1"
fi

# 6  
Old 04-29-2009
can i use other command in stead of getopts?
# 7  
Old 04-29-2009
Quote:
Originally Posted by mingming88
can i use other command in stead of getopts?
Yes you could just use a command line parameter.

In your script

Code:
pass=$1
if [ $pass -lt 50 ]; then
echo "Pass mark of $pass is less than 50"
else
echo "Pass mark of $pass is greater than 50"
fi

Run the script as

Code:
scriptname 45

cheers,
Devaraj Takhellambam
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Resolving a parameter which is passed as parameter

Hi, I have the following files. ->cat scr.sh export TMP_DIR=/home/user/folder1 export TMP_DIR_2=/home/user/folder2 while read line do cat "$line" done<file_list.dat ------------------------ -> cat file_list.dat $TMP_DIR/file1.txt $TMP_DIR_2/file2.txt --------------------------- -> cat... (6 Replies)
Discussion started by: barath
6 Replies

2. Shell Programming and Scripting

How to get the parameter value from the parameter file in perl?

hi all, i have a parameter file of following format, i want a method which can get the value of specific parameter. parameter file format: <Parameter Name="FileLocationWindows"> <Description> The directory location of the logger file. ... (1 Reply)
Discussion started by: laxmikant.hcl
1 Replies

3. Shell Programming and Scripting

Command that takes one parameter and then searches for the passed in parameter

Hi I am looking for a unix command or a small shell script which can takes one parameter and then searches for the passed in the parameter in any or all files under say /home/dev/ Can anyone please help me on this? (3 Replies)
Discussion started by: pankaj80
3 Replies

4. Shell Programming and Scripting

Incrementing the date depending on the Counter (parameter) passed to the script

Hello Everyone, I have been trying to complete a shell script where, I need to increment the date depending on the file (depending on the date) availability on the remote server. i.e. Basically, I will be passing a counter (like parameter 1 or 2 or 3 or 4). First I will check for the... (1 Reply)
Discussion started by: filter
1 Replies

5. Shell Programming and Scripting

Grepping file and returning passed variable if the value does not exist in file at all.

I have a list of fields that I want to check a file for, returning that field if it not found at all in the file. Is there a way to do a grep -lc and return the passed variable too rather then just the count? I am doing some crappy work-around now but I was not sure how to regrep this for :0 so... (3 Replies)
Discussion started by: personalt
3 Replies

6. UNIX for Dummies Questions & Answers

checking if parameter passed is a number

I have written a function that fills an array and another function where if a parameter is supplied it will jump to that part of the array and cat it to the screen. I need to put in some checks to make sure the parameter supplied is firstly a number and then not a number great than the length of... (2 Replies)
Discussion started by: magnia
2 Replies

7. Shell Programming and Scripting

Detecting Doublequotes(") When passed as parameter to shell script

Hi, I am executing a shell script which takes a string as a parameter. The scipt should validate the string and create the directoy with the name of specfied string. The following is the specified command and its parameter. test.sh "abc abc" The shell script is not able to identify... (4 Replies)
Discussion started by: raghu.amilineni
4 Replies

8. Shell Programming and Scripting

how to find the path of a file when it is passed as ....filename(parameter) to script

hi unix guru's..................:confused: question is posted in the #3 permalink shown below. (3 Replies)
Discussion started by: yahoo!
3 Replies

9. UNIX for Dummies Questions & Answers

checking parameter values passed to script

Hi, I will pass 3 parameters for a script.I have to check the file name and create a new file name with time stamp. the parameters which i'm passing are /dir/stg/filename.txt /dir/path/head.txt /dir/path/tail.txt Now i have to check filename like : if it is a.txt i have to create... (2 Replies)
Discussion started by: ammu
2 Replies

10. Shell Programming and Scripting

manipulate a passed in parameter

I am passing a file name as a parameter to shell script the parameter is getfile.txt.gpg how do i process this parameter to get name like getfile.txt only and eleminate the .gpg text?? Thanks in advance (2 Replies)
Discussion started by: rudoraj
2 Replies
Login or Register to Ask a Question