0

I would like to make a bash script that will create a git-hook, so I can run $ bash ./create-hook.sh example.com and it creates this file:

#!/bin/sh
NAME="example.com"
TARGET="/var/www/${NAME}"
TEMP="/var/www/${NAME}_temp"
REPO="/srv/git/${NAME}.git"

mkdir -p $TEMP
git --work-tree=$TEMP --git-dir=$REPO checkout -f
rm -rf $TARGET/*
mv $TEMP/* $TARGET
rm -rf $TEMP
# etc.

```

So here is the current status of the script (not working):

#!/bin/sh

if [[ $# -eq 0 ]]
  then
    echo 'No name provided'
    exit 1
fi

sudo bash -c 'cat << EOF > post-receive
TARGET="/var/www/$1"
TEMP="/var/www/$1_temp"
REPO="/srv/git/$1.git"

mkdir -p \$TEMP
git --work-tree=\$TEMP --git-dir=\$REPO checkout -f
rm -rf \$TARGET/*
mv $TEMP/* \$TARGET
rm -rf \$TEMP

How to pass the variable $1 from the command line to the created file?

François Romain
  • 13,617
  • 17
  • 89
  • 123

2 Answers2

0

the << EOF* will write whatever is after it until you write EOF into the file so $1 will be sent just like that (textually $1) into the file. I guess you'll have to write the file by echoing:

( echo TARGET='"/var/www'$1'"'; echo TEMP='"/var/www/'$1'_temp"'; echo REPO='"/srv/git/'$1'.git"'; etc etc ) > post_receive

Or something along those lines

eftshift0
  • 26,375
  • 3
  • 36
  • 60
0

I found a solution here using tee … instead of bash -c 'cat…:

sudo tee > post-receive << EOF
TARGET="/var/www/$1"
# …
EOF
François Romain
  • 13,617
  • 17
  • 89
  • 123