Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Preamble: Never created a shell bash script before. So, and first of all, I'm not even sure if this IS the right tool for the job. If it isn't, please, and before all, let me know what, in your opinion, is a best alternative.

Context: I often do repetitive tasks each time I try to setup a web site. I wish to automatize those tasks by running one or two commands. UPDATE: Shared host is Unix based. Local machines are Mac OS X so, also Unix based.

As an example, here are some of the tasks (commands) I often do:


scp ~/local/path/general_web_app/.bash_profile

scp ~/local/path/general_web_app/.gitconfig

SSH directly:

cd /www/www/
git init
git add .
git commit -a -m "first commit"
cd ~/private/ && mkdir repos


pico ~/remote/private/repos/general_hub.git/hooks/post-update
replace "user" WITH "hostname";
hit "cmd x"
hit "y"

AFAIK: Those are, the different kind of tasks I wish the script to do.

Question 1: Can bash script handle SCP, SSH directly, and EDIT FIND REPLACE SAVE AND EXIT?

Question 2: Some params should be asked to the user who runs the script, and those params, should be used to be placed inside some files (replacing specific keywords there existent) - can this be accomplished?

Question 3: Should we copy the files to the remote and edit directly on the remote, or, should we grab the files locally, "edit them locally on a temp location", and then, place them on the server? I presume the second is much more complicated.

Any examples that I may look at?

Thanks a lot in advance

share|improve this question
yes, yes and it depends on what your requirements are. – suspectus Dec 6 '13 at 10:44
up vote 2 down vote accepted

The short answer is: yes to all.

I will give you some examples. The following is an example of a command which executes a local bash script onto a remote machine: please notice that it is not necessary to copy it remotely:

   ssh user@remote_pc 'bash -s' <

This is so simple it may not deserve writing a whole script to handle it, but should you wish to do so, it can also be arranged via a script. For this to work, though, you will have to set up passwordless login for ssh connections. If you do not, the ssh/scp commands within the script will halt as they wait for you to enter the remote user's password.

As for editing and so on, Unix has the ultimate stream editor sed, which does exactly this: for instance, the command

 sed 's/a/A/g' old_file.txt > new_file.txt

substitutes lower-case a into upper case A, and places the output into a new file. This too can be easily scripted.

As for the parameters, shell script can read parameters from a file, or they can query the environment for some parameters. There is no need to ask a user for his login name,

  echo $USER

will do it, even from within the script. Same thing for a home address, echo $HOME, and so on.

As for your third question, I hope my first answer provided a reply to that as well: the first command allows you to execute a script file, which you edited locally, onto a remote server, without even having to copy it over there.

Welcome to the world of *Nix, where your wish is my command.

Sorry, I forgot: this command

  export -p | more

will display all local variables which are available everywhere (technically, which have been exported).

share|improve this answer
I already have ssh keys stored on authorized_keys so that no password is prompt. About the parameters, that's a great point. Instead of asking the user, I can try to find those answers. Actually in order to answer those questions, the user should look at the environment and look at them, so I guess, the same can do the system, if, those questions are not subjective. Thanks a lot. It's my first step to dig in and try to narrow down a script. – MEM Dec 6 '13 at 11:15
@MEM please read the small edit to my post, you may find it useful. – MariusMatutiae Dec 6 '13 at 11:19
You had a useless use of cat there and probably meant to use sed instead of grep? – slhck Dec 6 '13 at 12:05
@slhck Yes and yes. But you should leave an expression like I am sorry, there is nothing wrong with that. – MariusMatutiae Dec 6 '13 at 12:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .