passing argument in script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting passing argument in script?
# 8  
Old 09-14-2012
This way maybe?
Code:
#!/bin/sh

#Default values
TEXT="Data_0"
Lines=45

if [ "$1" ]; then
	[ "$1" -eq 0 ] >/dev/null 2>&1; x="$?"
	if [ "$x" -lt 2 ]; then 	
		echo "Arg is an integer, to be set to Lines"
		Lines="$1"
	else
		echo "Arg is not an integer, so, it's probably some text"
		TEXT="$1"
	fi
fi


Last edited by tukuyomi; 09-14-2012 at 05:47 PM.. Reason: Sorry, lots of edits, hope you catch the last one :S
# 9  
Old 09-14-2012
Hi tukuyomi,
Thanks for the reply !
As I understood it would work. But I need to provide both argument together. But yeah ECHO would help in recalling as what argument should be text and what should be integer?

Am I correct?
Pooja

Quote:
Originally Posted by tukuyomi
This way maybe?
Code:
#!/bin/sh

#Default values
TEXT="Data_0"
Lines=45

[ "$1" -eq 0 ] >/dev/null 2>&1; x="$?"
if [ "$x" -lt 2 ]; then 	
	echo "Arg is an integer, to be set to Lines"
	Lines="$1"
else
	echo "Arg is not an integer, so, it's probably some text"
	TEXT="$1"
fi

# 10  
Old 09-14-2012
Quote:
Originally Posted by nrjrasaxena
Hi tukuyomi,
Thanks for the reply !
As I understood it would work. But I need to provide both argument together. But yeah ECHO would help in recalling as what argument should be text and what should be integer?

Am I correct?
Pooja
I think you are very confused or i am confused Smilie

do you want to run one script multiple times and pass argument.

if you want to get return from the script as what type of argument is passed then just verify the string with numeric or characters..

Code:
./script.sh text
# here $1=text you can perform the operation here..

./script.sh 10
#here also $1=10

If i am guessing right you might talking about after running script 2 you want $2 as 10 right..?


That's not possible as per my knowledge.

hope it helps you..

and for checking string or integer check this...

Code:
$ [[ `echo "adsds" | sed 's/^[-+0-9][0-9]*//' | wc -c` -eq 1 ]] && echo "itz number" || echo "itz not number"
itz not number

$ [[ `echo "1022" | sed 's/^[-+0-9][0-9]*//' | wc -c` -eq 1 ]] && echo "itz number" || echo "itz not number"
itz number

$ [[ `echo "10df22" | sed 's/^[-+0-9][0-9]*//' | wc -c` -eq 1 ]] && echo "itz number" || echo "itz not number"
itz not number


Last edited by pamu; 09-14-2012 at 06:09 PM.. Reason: info added...
# 11  
Old 09-14-2012
You want your script to take
Code:
./script.sh text
./script.sh integer
./script.sh text integer

all three at the same time?
Or Am I confused as well? :s

Try this :
Code:
#!/bin/sh

#Define a function
intortext() {
	[ "$1" ] || return 0
	[ "$1" -eq 0 ] >/dev/null 2>&1; x="$?"
	if [ "$x" -lt 2 ]; then 	
		echo "Arg is an integer, to be set to Lines"
		Lines="$1"
	else
		echo "Arg is not an integer, so, it's probably some text"
		TEXT="$1"
	fi
}

#Default values
TEXT="Data_0"
Lines=45

#main()
intortext "$1"
intortext "$2"


Last edited by tukuyomi; 09-14-2012 at 06:16 PM..
# 12  
Old 09-14-2012
If your question is how to use it with only parameter 2, use this:
Code:
 ./script.sh "" integer

And I would prefer if [ -z "$1" ]... instead of if [ ! "$1" ]...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variable as an argument to another script

Hi, I am trying to pass a variable as an argument to another script. While substitution of variable, I am facing a problem. varaiable "a" value should be -b "FPT MAIN". When we pass "a" to another script, we are expecing it to get substitue as ./test.sh -b "FPT MAIN". But, it is getting... (9 Replies)
Discussion started by: Manasa Pradeep
9 Replies

2. Shell Programming and Scripting

Passing argument from Java to Shell script

Hi All, I want to pass array of argument from Java to a shell script.I can use process builder api and its exec() method to call the script,but the question is how to receive the parameter in the script. Thanks in advance ---------- Post updated at 10:00 PM ---------- Previous update was... (1 Reply)
Discussion started by: Abhijeet_Atti
1 Replies

3. Shell Programming and Scripting

Passing --usage as argument to awk script

I have the awk script below and things go wrong when I do awk -v dsrmx=25 -f ./checkSRDry.awk --usage I basically want to override the usual --usage and --help that awk gives. How do people usually handle this situation when you also want to supply your own usage and help concerning the... (2 Replies)
Discussion started by: kristinu
2 Replies

4. Shell Programming and Scripting

Passing value as a command line argument in awk script.

I have one working awk command line. Which taking data from the “J1202523.TXT” file and generating the “brazil.dat” file. PFB code. awk '{ DUNS = substr($0,0,9);if ( substr($0,14,3) == "089" ) print DUNS }' J1202523.TXT > Brazil.dat But now I want to pass two parameter as a command line argument... (4 Replies)
Discussion started by: humaemo
4 Replies

5. Shell Programming and Scripting

Passing argument to a script while executing it within current shell

Hi Gurus, I have written a script set_env.ksh to which I pass an argument and set the oracle login credentials based on the argument I pass. The script has code as below. ORACLE_SID=$1 DB_SCHEMA_LOGON=$DB_SCHEMA_USER/$DB_SCHEMA_PASSWORD@$ORACLE_SID; export DB_SCHEMA_LOGON; echo... (3 Replies)
Discussion started by: Sabari Nath S
3 Replies

6. Shell Programming and Scripting

Its Urgent ::: Passing Argument during Script Execution

Dear All, Plz, can someone clarify me regarding the below. Actually I want to write the script that will get the some parameter like "yes" or "No" as we wish during execution without prompting for entering that word. Plz tell me... Its Urgent.. Thanks & Regards ... (1 Reply)
Discussion started by: ks47
1 Replies

7. Shell Programming and Scripting

Need to run the script by argument passing

Hi All, I have a question regarding running this script by passing an argument, for example ./ShellParse.sh sun, how do i do that? So i want when i pass argument sun, it shouild execute things inside the for loop. I want to support some other platforms too, so there are more for loops to... (3 Replies)
Discussion started by: asirohi
3 Replies

8. Shell Programming and Scripting

Problem when passing argument to a shell script

Hi all, I'm new to Shell scripting. In my shell script for Bourne shell, the script accepts a date parameter which is optional. If the value is supplied, the supplied value should be assigned to a variable. If not, the current date will be assigned to the variable. My script is like this. #!... (9 Replies)
Discussion started by: sumesh.abraham
9 Replies

9. Shell Programming and Scripting

Passing argument from one script to other

Dear All, I have one script which accepts database name and user_id from the user, i have another script that will unload the data from all the tables based on the user_id accepted by the user. How can i pass the user_id from the 1st script to the other. My OS is sun solaris. Thanks in... (3 Replies)
Discussion started by: lloydnwo
3 Replies

10. UNIX for Dummies Questions & Answers

Passing argument to awk script

I am writing a shell script. Now i need to read in a string and send it to an awk file to compare and search for compatible record. I wrote it like tat: read serial | awk -f generate.awk data.dat p/s: the data file got 6 field. According to an expert, we can write it like tat: read... (1 Reply)
Discussion started by: AkumaTay
1 Replies
Login or Register to Ask a Question