简体   繁体   中英

BASH script with sudo

I'm currently writing a little script in bash to ask the user for some setting. The script is asking some infos, and then run other scripts. I have to run it under sudo, because I have some apt-get install in scripts.

But when I launch it with sudo, I have this error :

./install_all: 1: ./install_all: Syntax error: "(" unexpected

Here is the beginning of my script :

function askAllInfo() {
    echo -e "
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
    |     Choix de la version de PHP     | 
    |                                    |
    |              1.) PHP5              |
    |              2.) PHP7              |
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#\n"

    read -e -p "Selectionner la version PHP pour l'installation de Maarch (PHP5) : " phpVERSION
    if [ "$phpVERSION" == "1" ] || [ "$phpVERSION" == "" ]; then
        phpVersion='5'
    elif [ "$phpVERSION" == "2" ]; then
        phpVersion='7'
    fi

    echo -e "
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
    |    Choix du chemin d'installation de MAARCH :    |
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#\n"
    read -e -p "Merci de spécifier le chemin pour l'installation de Maarch (/var/www/maarch_courrier) : " maarchURL
    if [ "$maarchURL" == "" ]; then
        MAARCH_SITE=/var/www/maarch_courrier
    else
        MAARCH_SITE=$maarchURL
    fi

    echo -e "
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
    |    Choix du chemin des docservers :    |
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#\n"
    read -e -p "Merci de spécifier le chemin des docservers (/var/docserver) : " docserversURL
    if [ "$docserversURL" == "" ]; then
        DOCSERVER_PATH=/var/docserver
    else
        DOCSERVER_PATH=$docserversURL
    fi

    echo -e "
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
    |        Choix du nom de la BDD :        |
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#\n"
    read -e -p "Merci de spécifier le nom de la base de donnée (maarch) : " databaseName
    if [ "$databaseName" == "" ]; then
        DATABASE_NAME=maarch
    else
        DATABASE_NAME=$databaseName
    fi

    echo -e "
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
    |        Choix du login de l'utilisateur de la BDD :        |
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#\n"
    read -e -p "Merci de spécifier le login de l'utilisateur de la base de donnée (postgres) : " databaseUser
    if [ "$databaseUser" == "" ]; then
        DATABASE_USER=postgres
    else
        DATABASE_USER=$databaseUser
    fi

    echo -e "
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
    |          Choix du mdp de l'utilisateur de la BDD :        |
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#\n"
    read -e -p "Merci de spécifier le mot de passe de l'utilisateur de la base de donnée (postgres) : " databasePass
    if [ "$databasePass" == "" ]; then
        DATABASE_PASSWORD=postgres
    else
        DATABASE_USER=$databasePass
    fi
}

askAllInfo

How can I fix this issue ?

Thanks in advance

Assuming you're showing the very beginning of the script, I think it's probably running under regular sh . The sh language does not support the function keyword ( reference ). If you add

#!/bin/bash

as the first line of your script, so that it runs under bash , you should be OK.

Alternatively, you can remove the word function from the first line of your script. However, it's better to add the #! so you can make use of the bash features.


As a test, I tried using dash (which doesn't support some of the bash extensions) on Cygwin. The line

function foo() {

gave me the corresponding error:

dash: 1: Syntax error: "(" unexpected

but

foo() {

worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM