-1

This is the code I'm using to check the current git branch,

branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')

I want to run the following code conditionally if the branch is master

cd build && aws s3 cp . s3://www.examle.com/ --recursive

How do I do this in bash?

Melissa Stewart
  • 3,483
  • 11
  • 49
  • 88
  • As in most languages you would use an `if` statement. Look them up in your favorite [bash language reference](https://mywiki.wooledge.org/BashGuide/TestsAndConditionals#Conditional_Blocks_.28if.2C_test_and_.5B.5B.29) – that other guy Mar 14 '19 at 21:30
  • BTW, I'd maybe make it `(cd build && exec aws s3 ...)` -- adding the `exec` balances out the performance impact of the subshell created by the parens (by consuming it), while scoping the `cd` to only change the directory where that one `aws s3` command is run. – Charles Duffy Mar 14 '19 at 21:33

2 Answers2

3

Try like below

[[ "$branch" == master ]] && cd build && aws s3 cp . s3://www.examle.com/ --recursive
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
3
branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
if [ "$branch" = master ]; then
   cd build && aws s3 cp . s3://www.examle.com/ --recursive
fi
Amir
  • 1,885
  • 3
  • 19
  • 36