LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   pass file-name as command line argument (https://www.linuxquestions.org/questions/programming-9/pass-file-name-as-command-line-argument-676409/)

vikas027 10-14-2008 10:17 PM

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

born4linux 10-14-2008 10:43 PM

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}."

vikas027 10-14-2008 11:52 PM

Thanks born4linux,
works perfect, will make a note for future.

Regards,
vikas


All times are GMT -5. The time now is 04:32 PM.