How to write a shell script?

So any time I set up a Wordpress site for a client on my linode server (Ubuntu 12.04) I run these commands, one-by-one, via the command line. It doesn't take long, but its tedious.

I'd like to know how to create a script that I can execute from the command line that would encapsulate all these individual commands. It should:

1. Be able to accept a number of parameters (e.g. siteurl, dbname, etc…)

2. I need to be able to use those paramters as variables in the script (e.g. mkdir /srv/www/{siteurl}/public_html)

3. Where should such scripts be located? What permissions should I set on them? What are the naming conventions?

I imagine its fairly trivial to put something like this together, I just want to make sure I do it right and following whatever conventions there are for these kind of things.

2 Replies

3. probably /usr/local/bin owned by root, mod 755. pick a name that doesn't conflict with an existing name. also make it wordy so it won't likely conflict in the future.

say addwordpresssite, then you would need to type addw[tab] blah

the rest you'll need to find a scripting tut, it's a lot of fun.

Chapters 34 and 35 of the FLOSS Manuals intro to the command line (1.7MB PDF) may be enough to get you going. For more detail, the Advanced Bash Scripting Guide is pretty good. You'll be handling command line arguments using positional parameters. chesty's recommendations are sound.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct