how to assign script argument to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to assign script argument to a variable
# 1  
Old 08-25-2009
how to assign script argument to a variable

I have a script file. test.sh
I am running it by command
sh test.sh 10102004

where 10102004 is the script argument.
I wan to assign this 10102004 to a variable.
How can i do this?

I tried &1 and awks ARGV[1] its not working Smilie
# 2  
Old 08-25-2009
Quote:
Originally Posted by rohankit
I have a script file. test.sh
I am running it by command
sh test.sh 10102004

where 10102004 is the script argument.
I wan to assign this 10102004 to a variable.
How can i do this?

I tried &1 and awks ARGV[1] its not working Smilie
Code:
me@hostname:~/tmp> ./tmp2.sh test1 test2
./tmp2.sh
test1
test2
me@hostname:~/tmp> cat tmp2.sh
#!/bin/sh
echo $0
echo $1
echo $2

me@hostname:~/tmp>

The script name will be $0, and each argument after that is incremented by 1.
# 3  
Old 08-25-2009
Inside your script:

var=$1

which means, var=10102004
(first argument of your script)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

Need a script to check if an argument is valid shell variable

I need a script that should print 'yes' if the argument is a valid shell variable name else 'No' if it is not a valid shell variable. A valid one begins with an alphabet or percentage (%) character and is followed by zero or more alphanumberic or percentage (%) characters. For example: $... (6 Replies)
Discussion started by: pingiliarjun
6 Replies

3. 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

4. UNIX for Dummies Questions & Answers

using awk iteratively in a script to assign variable values

I have a log file that has certain fields that I want to evaluate, and depending on the value in those fields, I want to put the value of a different field in that line in a particular variable that I'll use later on down the log file. Sort of like setting a switch to change what I do with a bunch... (5 Replies)
Discussion started by: pts2
5 Replies

5. Shell Programming and Scripting

In bash script, how to assign output of a command to a variable while keeping tabs?

Hi, wondering if it's been asked before but didn't find matches from google. Basically I have this line: myvar=$(echo -e "a\tb") Now somehow the '\t' from the echo output gets replaced with white space and then stored in $myvar. It creates a problem for me later to use tab as delimiter to do... (2 Replies)
Discussion started by: birddie
2 Replies

6. Shell Programming and Scripting

How to write script read file and assign it as variable?

Hi all, I want write a csh script which must be able: 1.read a file 2.assign value in file as variable and can i use read in csh script? thx (2 Replies)
Discussion started by: proghack
2 Replies

7. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

8. Shell Programming and Scripting

how to assign the output of the interective script to the variable

Hi, I work in ksh88. I have an interective script which prompts the user for the input and returns numeric value depending on the input provided. I need to call this script inside another script and then assign the resulting output the the variable. The call like that A=`my script` obviously... (6 Replies)
Discussion started by: aoussenko
6 Replies

9. Shell Programming and Scripting

How to assign the result of a SQL command to more than one variable in shell script.

Hi Friends... Please assist me to assign the result of a SQL query that results two column, to two variables. Pls find the below code that I write for assigning one column to one variable. and please correct if anything wrong.. #! /bin/sh no=' sqlplus -s uname/password@DBname... (4 Replies)
Discussion started by: little_wonder
4 Replies

10. Shell Programming and Scripting

how to assign sql output data to shell script variable

Hi Guys ! I am new to unix and want to find out how we can make sql statement data to shell script variable? Any help/suggestion is greatly appreciated -Chandra (1 Reply)
Discussion started by: kattics
1 Replies
Login or Register to Ask a Question