N
N
nasnetstep2015-12-30 12:55:42
PHP
nasnetstep, 2015-12-30 12:55:42

How to write a BASH file correctly?

I do the same thing all the time

  1. ssh -i /var/www/keys/my_key.pem [email protected] (below are the steps already on the server via ssh)
  2. cd /var/www/
  3. git fetch
  4. git reset --hard origin/my_branch
  5. bash my_bash.sh

Questions:
How to write bash.sh that will perform similar actions?
How to set the branch name parameter (my_branch) to execute bash.sh?
How to ssh with a list of commands (2, 3, 4,5) ?
Thank you all for the answers, I already answered the last question myself, maybe it will be useful to someone:
ssh -i /var/www/keys/my_key.pem [email protected] "cd /var/www/ && git fetch && git reset --hard origin/$1 && bash my_bash.sh"

Answer the question

In order to leave comments, you need to log in

5 answer(s)
J
jlekapb, 2015-12-30
@jlekapb

run something like this:sh ./bash.sh my_branch

#!/bin/bash

ssh -i /var/www/keys/my_key.pem -f [email protected]
cd /var/www/
git fetch
git reset --hard origin/$1
bash my_bash.sh

A
Armenian Radio, 2015-12-30
@gbg

  1. Naturally, copy this case to a file with the .sh extension
  2. Add the line #!/bin/bash to the beginning
  3. Command line options are substituted for $1, $2, $3, and so on.
  4. Give the script the right to execute.

M
microphone, 2015-12-30
@microphone

cat <<EOF > bash.sh
#!/bin/bash      
# Путь может быть другим -> whereis bash
case "$#" in
0)
# На входе ноль параметров, выполним ссш
ssh -i /var/www/keys/my_key.pem -f [email protected]
         ;;
1)
# На входе 1 параметр, выполним ссш и что-то еще 
ssh -i /var/www/keys/my_key.pem -f [email protected]
cd /var/www/
git fetch
git reset --hard origin/$1
bash my_bash.sh
         ;;
        *)
# На входе хрен пойми что - дай подсказаньку
                echo "Usage:  \"bash ./bash.sh my_branch\" OR \" bash ./bash.sh \" ";
        ;;
esac
EOF

N
nasnetstep, 2015-12-30
@nasnetstep

ssh -i /var/www/keys/my_key.pem [email protected] "cd /var/www/ && git fetch && git reset --hard origin/$1 && bash my_bash.sh"

K
kvspb, 2015-12-30
@kvspb

very similar to deploy, I would use ansible

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question