LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 05-06-2011, 04:53 AM   #1
ramecare
Member
 
Registered: Feb 2011
Posts: 179

Rep: Reputation: 0
Shell Scripting command line argument


Hi to all,

I am very new to shell scripting.How does one pass a command-line parameter to a shell script?for the below program
#/bin/bash
mount -t cifs -o user=ramkannan,password=Linux123@ //10.200.1.125/ramkannan /MT
cd /MT/test
date=`/bin/date "+\%Y-\%m-\%d-\%H-\%M-\%S"`
mysqldump -uroot -pram2@ employeedb > $date.sql
gzip $date.sql

I want to pass parameter for everything,i tried in google and did but iam getting error while passing parameter to all

#/bin/bash
mount -t cifs -o user=$1,password=$2 //10.200.1.125/ramkannan /MT
cd /MT/test
date=`/bin/date "+\%Y-\%m-\%d-\%H-\%M-\%S"`
mysqldump -uroot -pram2@ employeedb > $date.sql
gzip $date.sql

i was getting error while passing parameter to all,Can any one help on the above program.

Thanks

Last edited by ramecare; 05-06-2011 at 04:55 AM.
 
Old 05-06-2011, 05:33 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
First of all, please use [code][/code] tags around your code, to preserve formatting and to improve readability.

Second, what exactly are the error messages you're getting? Just saying something "doesn't work" is not very helpful for diagnosing problems.

In any case, parameter expansion is done just like any other variable, so your code seems to be ok. Except for one thing. I think you perhaps want to replace the username twice in the command.
Code:
mount -t cifs -o user="$1",password="$2" //10.200.1.125/"$1" /MT
Variables should also generally be double-quoted, unless you need word-splitting or other expansion to be done on the variable contents. Not that it really matters with single-word names, but passwords could have shell-reserved characters in them.

Speaking of which, it's not secure to have a script that requires you to pass the password as a parameter. It will remain in your command history. It would be better to use a read command and have it prompt the user for input, or perhaps retrieve it from a file that only the user calling the script has read-access to.

Also, I also wouldn't cd straight into the folder without first testing that the share was successfully mounted (Hint, try a grep on /etc/mtab).

Here are a few useful bash scripting references:
http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://wiki.bash-hackers.org/start

By the way, $(..) is recommended over `..`
 
Old 05-06-2011, 07:23 AM   #3
ramecare
Member
 
Registered: Feb 2011
Posts: 179

Original Poster
Rep: Reputation: 0
Thank u very much its working now
 
  


Reply



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
[SOLVED] shell scripting: value of argument dissapears depending on argument order akelder Programming 5 03-21-2011 11:27 PM
(SOLVED) bash shell read user argument from command line and test it atnonis Programming 12 08-30-2010 07:11 AM
LXer: Using Grep To Streamline Your Shell And Command Line Scripting LXer Syndicated Linux News 0 08-08-2008 07:11 AM
Shell Scripting - use the output of a command as an argument for another timgiffney Programming 6 04-17-2006 11:13 AM
command line options for firefox for shell scripting. dr_zayus69 Programming 1 05-25-2005 11:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:31 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