Visit Our UNIX and Linux User Community


passing argument in script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting passing argument in script?
# 1  
Old 09-14-2012
passing argument in script?

hi,
I want to implement some function to perform following task
Code:
if [ firstArgument is not provided ]; then
$TEXT = "Data_0"
else
$TEXT = $1
fi

if [ secondArgument is not provided]; then
$Lines = 45
else 
$Lines = $2
fi

Kindly suggest,

thanks
Pooja
# 2  
Old 09-14-2012
$# return the number of argument.
This User Gave Thanks to delugeag For This Post:
# 3  
Old 09-14-2012
Which shell are you using?

In sh, dash, should run on bash as well:
Code:
#!/bin/sh

if [ ! "$1" ]; then
TEXT="Data_0"
else
TEXT="$1"
fi

if [ ! "$2" ]; then
Lines=45
else 
Lines=$2
fi


Last edited by tukuyomi; 09-14-2012 at 05:18 PM.. Reason: No $ in front of variables names in assignments, no spaces between =
This User Gave Thanks to tukuyomi For This Post:
# 4  
Old 09-14-2012
Quote:
Originally Posted by tukuyomi
Which shell are you using?

In sh, dash, should run on bash as well:
Code:
#!/bin/sh

if [ ! "$1" ]; then
TEXT = "Data_0"
else
TEXT = "$1"
fi

if [ ! "$2" ]; then
Lines = 45
else 
Lines = $2
fi

Perfect, thanks..Smilie

---------- Post updated at 03:15 PM ---------- Previous update was at 03:12 PM ----------

Quote:
Originally Posted by nrjrasaxena
Perfect, thanks..Smilie
ummm, confusion..SmilieSmilie
my firstargument is String and second argument is integer.

so what if I do following:
Code:
./script.sh text 
and 
./script.sh 10

I am just wondering, wont it take 10 as first argument???? in principle it should be my second argument..?? and vice versa..

Pooja
# 5  
Old 09-14-2012
try this..
Code:
./script.sh text 10

# 6  
Old 09-14-2012
Try as pamu says. To go further, you may need to quote your text if it contains spaces:
Code:
./script.sh 'text with spaces to be taken as first arg' integer_arg


Last edited by tukuyomi; 09-14-2012 at 05:26 PM.. Reason: Code tags, please
# 7  
Old 09-14-2012
Hi pamu,
Quote:
Originally Posted by pamu
try this..
Code:
./script.sh text 10


I am confuse as I want to use it both ways:
Code:
./script.sh 10

And here I want code to understand that it is secondArgument

and also like
Code:
 ./script.sh data_0

where code should know that its firstArgument.

I hope, I stated it clearly now?

Thnaks
Pooja

Previous Thread | Next Thread
Test Your Knowledge in Computers #759
Difficulty: Medium
Commodore filed for bankruptcy in 1994 and its assets were purchased by a German PC manufacturer who created the subsidiary company Amiga Technologies.
True or False?

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

Featured Tech Videos