Pass command line argument to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass command line argument to variable
# 1  
Old 09-24-2010
Pass command line argument to variable

Hi,

I had written a shell script to pass command line argument to variable in a function.

Here is my code:

main
Code:
if [-f $1]; then
  .$1
  echo $1
  get_input_file
else
  echo "input file $1 is not available"
fi
 
get_input_file()
{
FILE = "$1"
echo $FILE
}

but I am getting error as FILE not found.
Can anyone correct me in this script. I am a new to shell scripting.
Thanks a lot in advance.

Last edited by Scott; 09-24-2010 at 08:24 AM.. Reason: Code tags, please...
# 2  
Old 09-24-2010
You need space around [ test as if [ -f $1 ]; then
No space when you declare variables FILE="$1"

Code:
#!/bin/bash -x

get_input_file()
{
FILE="$1"
echo $FILE
}

if [ -f $1 ]; then
  # .$1
  echo $1
  get_input_file $1
else
  echo "input file $1 is not available"
fi

# 3  
Old 09-24-2010
Thanks for reply.
But the error still exists, after changing
input_file = " $1 "

echo $input_file

Please help me out.
# 4  
Old 09-24-2010
Quote:
Originally Posted by Poonamol
Thanks for reply.
But the error still exists, after changing
input_file = " $1 "

echo $input_file

Please help me out.
Please reread the 2nd line of the post of danmero.
# 5  
Old 09-24-2010
Bug

Hi
Try this code :

Code:
get_input_file()
{
FILE=$1
echo $FILE
}
if [ -f $1 ]; then
  . $1
  echo $1
  get_input_file $1
else
  echo "input file $1 is not available"
fi

# 6  
Old 09-27-2010
Thanks a lot dashing201. Its working fine.

I just update the code as below,
Code:
get_input_file()
{
FILE=$1
echo $FILE
TYPE=`echo $FILE | cut -d "_" -f 3`
echo "Type is $TYPE"
EXTN=`echo $FILE | cut -d "." -f 5`
echo "Extension is $EXTN"
}

And I am putting some values in input file as,
Code:
abcdefghij;20100903040607;1234567891;GLOBAL;

but getting output as,
Code:
Sample.ksh[97]: abcdefghij:  not found.
Sample.ksh[97]: 20100903040607:  not found.
Sample.ksh[97]: 1234567891:  not found.
Sample.ksh[97]: GLOBAL:  not found.
MigBIOS_CR_GLOBAL_20100924021422.CSV
Type is
Extension is

Could you please tell me why error is occurring for each value in input file and the values of TYPE and EXTN are not getting displayed as GLOBAL and CSV respectively?
Could you please tell me where I did the mistake in code?



Moderator's Comments:
Mod Comment Please use code tags

Last edited by Franklin52; 09-27-2010 at 10:48 AM..
# 7  
Old 09-27-2010
Hi

You cannot have two different delimiters to extract fields. In your case you are using '.' as well as '_'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read the output of a command line by line and pass it as a variable?

Hi, I have some 2000 names in a table like below. Java Oracle/SQL ANSI SQL SQL,DWH,DB DB&Java And by using for loop in my code i am able to get a single word but if there is any special character or space then it is considering as a next line. I have to execute the below queries in... (10 Replies)
Discussion started by: Samah
10 Replies

2. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

3. Shell Programming and Scripting

How to pass each line of a text file as an argument to a command?

I'm looking to write a script that takes a .txt filename as an argument, reads the file line by line, and passes each line to a command. For example, it runs command --option "LINE 1", then command --option "LINE 2", etc. I am fetching object files from a library file, I have all the object file... (2 Replies)
Discussion started by: Paul Martins
2 Replies

4. UNIX for Dummies Questions & Answers

How to pass command line argument in shell script?

I need to write a shell script, when I run that script I should pass those arguments if not, then script should not run and pass the error message like invalid option - - should pass the argument. and Exit from the script (8 Replies)
Discussion started by: Nsharma3006
8 Replies

5. Shell Programming and Scripting

How to pass command line argument in shell script?

I need to write a shell script, when I run that script I should pass those arguments if not, then script should not run and pass the error message like invalid option - - should pass the argument. and Exit from the script https://www.unix.com/images/misc/progress.gif (1 Reply)
Discussion started by: Nsharma3006
1 Replies

6. Red Hat

pass a variable line number to sed

num=10 sed -n '$num p' test.txt sed -n '10 p' test.txt works however i am putting the sed command in a loop and the line number is not static Can someone please help me how to achive this. (1 Reply)
Discussion started by: figure20012
1 Replies

7. Shell Programming and Scripting

How to pass a filename as a command line argument

Hi,I have a script which is given below :#!/bin/bash. ini_script.shdb2 connect to $DB_NAME user $DB2_UID using $DB2_PASSWORDfor file in `ls -1 ./sql/ddw/`do echo "Executing the file $file" echo db2 -tvf $filedonedb2 quiti want this script to accept directorie's names present in... (1 Reply)
Discussion started by: ektubbe
1 Replies

8. Shell Programming and Scripting

pass variable as sar argument

I am trying to get the details of iowait for last hour using sar. When I give date as argument it works. # sar -u -s 22:24:00 -e 23:24:00 Linux 2.6.35.13-26 (XX.server.com) 04/11/2012 10:30:03 PM CPU %user %nice %system %iowait %idle 10:40:04 PM all 5.03 ... (2 Replies)
Discussion started by: anil510
2 Replies

9. Programming

Need help in storing command line argument argv[2] to a variable of int type

The following program takes two command line arguments. I want the second argument (fileCount) to be stored/printed as a int value. I tried my best to typecast the char to int (check the printf statement at last) but is not working...the output is some junk value. This program is in its... (3 Replies)
Discussion started by: frozensmilz
3 Replies

10. Shell Programming and Scripting

Passing the command line argument in a variable

Hi, I am new to unix. Is their a way to pass the output of the line below to a variable var1. ls -1t | head -1. I am trying something like var1=ls -1t | head -1, but I get error. Situation is: I get file everyday through FTP in my unix box. I have to write a script that picks up first... (1 Reply)
Discussion started by: rkumar28
1 Replies
Login or Register to Ask a Question