0

I have the following script:

#!/bin/bash

#Path where all the files will be executed

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done

SCRIPT_PATH="$( cd -P "$( dirname "$SOURCE" )" && pwd )"/..

# Printing functions
black='\E[30;40m'
red='\E[31;40m'
green='\E[32;40m'
yellow='\E[33;40m'
blue='\E[34;40m'
magenta='\E[35;40m'
cyan='\E[36;40m'
white='\E[37;40m'


cecho ()                     # Color-echo.
                             # Argument $1 = message
                             # Argument $2 = color
{
local default_msg="No message passed."
                             # Doesn't really need to be a local variable.

message=${1:-$default_msg}   # Defaults to default message.
color=${2:-$black}           # Defaults to black, if not specified.

  echo -e "$color"
  echo "$message"
  tput sgr0                      # Reset to normal.

  return
}

cecho "Setting Up Piwik" $magenta
cecho "Configuring piwik database settings" $cyan

read -p "Instert a password for piwik's database: " PIWIK_MYSQL_ROOT_PASSWORD

cecho "Configuring Email settings for reports." $cyan



#Create .data folder with piwik required stuff
if [ ! -f  ${SCRIPT_PATH} ]; then
 mkdir -p ${SCRIPT_PATH}
fi

read  -p "Instert smtp host: " MAIL_HOST
read -p "Insert smtp port: " MAIL_PORT
read -p "Insert your email username: " MAIL_USER
read -p "Insert your email password: " MAIL_PASS

cat > ${SCRIPT_PATH}/ssmtp.conf << EOF
UseTLS=Yes
UseSTARTTLS=Yes
root=${MAIL_USER}
mailhub=${MAIL_HOST}:${MAIL_PORT}
hostname=${MAIL_HOST}
AuthUser=${MAIL_USER}
AuthPass=${MAIL_PASS}
EOF

echo "www-data:${MAIL_USER}:${MAIL_HOST}:${MAIL_PORT}" >> ${SCRIPT_PATH}/revaliases

cecho "Setting up wordpress" $magenta

cecho "Setting up wordpress' database settings" $magenta

read -p "Insert a password for database ROOT user: " WORDPRESS_MYSQL_ROOT_PASSWORD
read -p "Insert a username for a NEW database user that wordpresss will use for connection: " WORDPRESS_MYSQL_USER
read -p "Insert a password for the user created above: " WORDPRESS_MYSQL_PASSWORD

COMMAND="env PIWIK_MYSQL_ROOT_PASSWORD=${PIWIK_MYSQL_ROOT_PASSWORD} WORDPRESS_MYSQL_ROOT_PASSWORD=${WORDPRESS_MYSQL_ROOT_PASSWORD} WORDPRESS_MYSQL_USER=${WORDPRESS_MYSQL_USER} WORDPRESS_MYSQL_PASSWORD=${WORDPRESS_MYSQL_PASSWORD} docker-compose" > $SCRIPT_PATH/start.sh

touch "${SCRIPT_PATH}/start.sh"
STARTUP_SCRIPT_PATH= "${SCRIPT_PATH}/start.sh"

touch "${SCRIPT_PATH}/stop.sh"
STOP_SCRIPT_PATH= "${SCRIPT_PATH}/stop.sh"

echo $COMMAND." up -d" > ${STARTUP_SCRIPT_PATH}
chmod u+x ${STARTUP_SCRIPT_PATH}

cecho "Startup script generated" $green

echo $COMMAND." stop " > ${STOP_SCRIPT_PATH}
chmod u+x ${STOP_SCRIPT_PATH}

cecho "Stop script generated" $green

echo "In order to starrt up the service run: "
cecho ${STARTUP_SCRIPT_PATH} $green

echo "You can stop the services via:"
cecho ${STOP_SCRIPT_PATH} $green

And on the execution I have these error messages:

./scripts/install2.sh: line 84: /home/pcmagas/Kwdikas/Docker/ellakcy/scripts/../start.sh: Permission denied
./scripts/install2.sh: line 87: /home/pcmagas/Kwdikas/Docker/ellakcy/scripts/../stop.sh: Permission denied
./scripts/install2.sh: line 89: ${STARTUP_SCRIPT_PATH}: ambiguous redirect
chmod: missing operand after 'u+x'
Try 'chmod --help' for more information.

Startup script generated
./scripts/install2.sh: line 94: ${STOP_SCRIPT_PATH}: ambiguous redirect
chmod: missing operand after 'u+x'
Try 'chmod --help' for more information.

The source of the errors are these lines of script:

touch "${SCRIPT_PATH}/start.sh"
STARTUP_SCRIPT_PATH= "${SCRIPT_PATH}/start.sh"

touch "${SCRIPT_PATH}/stop.sh"
STOP_SCRIPT_PATH= "${SCRIPT_PATH}/stop.sh"

What I want/try to do is my script by given some parameters to auto-generate scripts in order to start and stop some docker container services.

But somehow the plan backfires to me right now. What can I try next?

1

1 Answer 1

1

You need sudo to get permission to write the script files, and there were spaces between the variable name and value; there must not be any spaces around the = signs:

sudo touch "${SCRIPT_PATH}/start.sh"
STARTUP_SCRIPT_PATH="${SCRIPT_PATH}/start.sh"  # Note no spaces ..PATH="${..

sudo touch "${SCRIPT_PATH}/stop.sh"
STOP_SCRIPT_PATH="${SCRIPT_PATH}/stop.sh" # Again here.
Sign up to request clarification or add additional context in comments.

2 Comments

Worked without sudo. It was jjust a space issue. (Sometimes when developing on languages like php and js you forget about not too use space on bash script.)
@DimitriosDesyllas Bash is the source of all my woes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.