0

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:

scp ~/local/path/general_web_app/.bash_profile [email protected]:/home/HOSTNAME/.bash_profile

scp ~/local/path/general_web_app/.gitconfig [email protected]:/home/HOSTNAME/.gitconfig

SSH directly:

ssh [email protected] 
cd /www/www/
git init
git add .
git commit -a -m "first commit"
cd ~/private/ && mkdir repos

EDIT, FIND, REPLACE SAVE AND EXIT:

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

2
  • yes, yes and it depends on what your requirements are.
    – suspectus
    Dec 6, 2013 at 10:44
  • you'd prefer a configuration management tool like ansible or salt for these kind of things....
    – magor
    Jul 15, 2021 at 7:59

1 Answer 1

2

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' < local_file.sh

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
  whoami

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).

4
  • 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, 2013 at 11:15
  • @MEM please read the small edit to my post, you may find it useful. Dec 6, 2013 at 11:19
  • You had a useless use of cat there and probably meant to use sed instead of grep?
    – slhck
    Dec 6, 2013 at 12:05
  • @slhck Yes and yes. But you should leave an expression like I am sorry, there is nothing wrong with that. Dec 6, 2013 at 12:12

You must log in to answer this question.

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