Find the answer to your Linux question:
Results 1 to 8 of 8
I have a simple bash script to automate database creation for a new development branch. It is run using ./branch-db from to Code: mysqldump -u root -pPASSWORD_IS_HERE $1 > $2.sql ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,475

    Bash script parameter substitution


    I have a simple bash script to automate database creation for a new development branch.

    It is run using ./branch-db from to

    Code:
    mysqldump -u root -pPASSWORD_IS_HERE $1 > $2.sql
    works fine but
    Code:
    mysql -u root -pPASSWORD_IS_HERE -e 'create schema $2'
    simply creates a schema called $2 instead of being called to in the example. I have also tried
    Code:
    mysql -u root -pPASSWORD_IS_HERE -e 'create schema ${2}'
    but that gives a syntax error.

    How do I get the $2 to substitute correctly?
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    Conkybots: Interactive plugins for your Conkys!

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,346
    Use doublequotes instead of single.
    Doublequotes allow variable evaluation.
    Code:
    mysql -u root -pPASSWORD_IS_HERE -e "create schema $2"
    Depending on what and how much you want to pass, the quoting and escaping can be a nightmare.
    It might be a better approach to write a file with all the sql code and just do:
    Code:
    mysql -u root -pPASSWORD_IS_HERE  < initialize.sql
    P.S. The pw on the commandline is a big NoNo, as it will show in the processlist.
    You can use an option file instead.
    You must always face the curtain with a bow.

  3. #3
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,475
    Double quotes like PHP. I didn't even thing of that even after all my years with PHP

    It's a dev box with no access to the outside workd and no important data so the command line password isn't a problem. It also shows the password in the mysql history - and that's in plain text

    Passing only alphanumeric and underscores so escaping shouldn't be an issue I would hope?
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    Conkybots: Interactive plugins for your Conkys!

  4. #4
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,346
    Alphanumeric and underscores are not a problem.

    <preacher mode>
    Is it an option for you to invest time and thought into a systemmanagement tool like puppet or chef?
    A mysql server manifest can: install all needed packages (even cross platform), config the daemon according to env, host or even dynamically accordding to e.g. ram or number of cores, initialize databases, take care of db dump crons, configure logging, etc

    In short: it offers repeatability and consistency

    I even use it for my private machines and vms.
    Deploy a new one, install puppet, copy my private key and a (very hackish) manifest to the box.
    Code:
    puppet apply hackish_manifest
    and the new workstation has all my usual tools, configured correctly.
    </preacher mode>
    You must always face the curtain with a bow.

  5. #5
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,475
    Not for me - I'm a humble developer who only gets to play on the dev server occasionally as needed. It may be worth mentioning to the guys who look after the live boxes but I suspect they will have something like that in place anyway.

    [edit]If I get time I may look into it at home. More knowledge is more knowledge after all[/edit]
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    Conkybots: Interactive plugins for your Conkys!

  6. #6
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,346
    There is this tutorial:
    Learning Puppet ? Documentation ? Puppet Labs

    It can be done in half a day (max) and it will give you a first impression and overview.
    Especially as a dev you might find it appealing, as it is not scripting any more but programmatically designing and maintaining a dev/work/production environment.
    You must always face the curtain with a bow.

  7. #7
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    664
    This is just wonderful to observe, two major contributors having this conversation without hubris.

  8. #8
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,475
    Quote Originally Posted by Irithori View Post
    Bookmarked. Thank you.
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    Conkybots: Interactive plugins for your Conkys!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •