LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-14-2008, 10:17 PM   #1
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Question pass file-name as command line argument


Hi all,

I have made a script which has a necessity that the file name should be "OVO_Report".

How can I modify the script so that it could take any file name provided as the first argument in the command line (WITHOUT making much changes to my present script).

I need something like capture filename given in the command line in a variable and use that variable in the script.

For e.g.
Code:
./my_script "any_filename"
Pls help, I haven't worked on scripts which take command line arguments.

Regards,
VIKAS
 
Old 10-14-2008, 10:43 PM   #2
born4linux
Senior Member
 
Registered: Sep 2002
Location: Philippines
Distribution: Slackware, RHEL&variants, AIX, SuSE
Posts: 1,127

Rep: Reputation: 49
you can use $1 - $1 is the first argument:

so, in your script, you can set:

nfileSource=$1


then use nfileSource throughout your script.

if you can only need 1 argument (the filename), you can also:


if [[ $# != 1 ]]
then
echo "\nUSAGE: $0 filename\n"
exit 0
fi

nfileSource=$1

echo "target file is ${nfileSource}."
 
Old 10-14-2008, 11:52 PM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Thanks born4linux,
works perfect, will make a note for future.

Regards,
vikas
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I pass command to gnome-terminal as an argument? crasywolf Linux - Newbie 5 05-11-2010 09:00 AM
bash script 'for each command line argument' true_atlantis Linux - Newbie 3 01-28-2009 01:51 PM
Perl command line argument noir911 Programming 8 01-15-2008 04:59 AM
Trying to get a script to accept a command line argument gmccammon Linux - Newbie 1 06-24-2007 09:51 PM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration