0

I am trying to write a bash script which

  1. installs git then
  2. creates a .gitconfig file and cat's config text into it.

Because the first task requires installing git, the script needs to be run as root, however, I want to de-elevate the script to a regular user while creating and cating config text into the .gitconfig file.

heres what I have so far (I have removed extraneous content from script)

#! /bin/bash

apt-get install -y git

if [ ! -f /home/$admin_user/.gitconfig ]
then
    echo -e "${yellow}Creating .gitconfig file${normal}"
    sudo -s -u $admin_user <<EOF
        cat <<- DGC >> /home/$admin_user/.gitconfig
            [alias]
            lg1 = ..
            lg2 = ...
            lg3 = ...

            [user]
                email = server_admin@DO_droplet.com
                name = server_admin
            # 
            [push]
                default = simple
        DGC
    EOF
else
    echo -e "${yellow} gitconfig already exists - so I will not make any modifications${normal}"
fi

when I run this I get an error

script.sh: line 782: syntax error: unexpected end of file

I think the error is being caused by the HEREDOCs?

I am open to the other suggestion other than using sudo and HEREDOC to de-elevate to $admin_user - I am just looking for a solution to creating the .gitconfig as $admin_user and not root

the_velour_fog
  • 2,094
  • 4
  • 17
  • 29
  • You cannot have any leading whitespace before the `EOF` delimiter. If you absolutely require whitespace, you need to use `<<-EOF`with a dash, in which case leading tabs will be ignored; or maybe use `____________EOF` flush to the left margin which still visually resembles an indented delimiter. – tripleee Sep 07 '15 at 08:37
  • d'oh! I should have seen that, yes that fixed it thanks @tripleee – the_velour_fog Sep 07 '15 at 09:16

0 Answers0