ssh - Is it possible to automatize a shared host setup using bash shell script?

07
2014-04
  • MEM

    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

  • Answers
  • MariusMatutiae

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


  • Related Question

    unix - bash shell script which adds output of commands
  • John Kube

    Let's say I have a command called foo which prints a number to the screen when called:

    $ foo
    3
    

    Let's also say I have another command called bar which prints another number to the screen when called:

    $ bar
    5
    

    I'm looking to write a shell script which will arithmetically add together the output of foo and bar (3+5=8). How would I do that? (The outputs from the commands are not known ahead of time. They just so happen to have been 3 and 5 the last time they were run. They could have been something else.)


  • Related Answers
  • Benjamin Bannier

    Use bash's let to evalutate arithmetric expressions.

    #!/bin/bash
    a=`echo 3`
    b=`echo 5`
    
    let c=$a+$b
    echo $c
    

    Just substitute the calls to echo with your program calls.

  • Dennis Williamson

    An alternative to let is to use double-parenthesis syntax:

    (( c = $(foo) + $(bar) ))
    

    or

    echo $(( $(foo) + $(bar) ))
    

    or using variables, you can omit the dollar sign on the right hand side of the equals:

    (( c += $(foo) + num ))
    

    (which also illustrates an incremental assignment)

    If you're using non-integers you can use bc:

    echo "$(foo) + $(bar)" | bc
    

    or

    c=$(echo "$(foo) + $(bar)" | bc)
    

    One advantage of using double parentheses is that you can put spaces around the operands and operators to make things more readable:

    (( c = ( a + b ) * ( i - j ) ))
    
  • Ignacio Vazquez-Abrams

    bash:

    bc < <({ foo ; echo + ; bar ; } | tr '\n' ' ' ; echo)
    

    If the output is integers only:

    $(( $(foo) + $(bar) ))