0

I have done few month ago a Linux script that announce on my server a reboot message with a timer. Since I have done some changes on my Linux, I have this error (launchSave.sh) Syntax error: "(" unexpected for this line TIMER=("30" "20" "5" "2" "1" "1" "1")

The two modifications I have done on my Linux is using dash rather than bash (I changed it back to try) and using kexec for warm reboots (with this tutorial : https://wiki.debian.org/BootProcessSpeedup#Using_readahead_to_load_files_from_disk). I don't know if it's the reason but I suspect it. Like you can see I'm not an expert of Linux and script.

I have test declare -a TIMER= and TIMER("30" "20" "5" "2" "1" "1" "1") but always a "(" or ")" syntax error.

EDIT

#! /bin/bash
TIMER=("30" "20" "5" "2" "1" "1" "1") ## Time between each annoucer
TOTAL_TIME="60" ## Total time variable of the timer to be subtracted
BUNGEECORD="bungeecord"
TODAY=`date +"%d%b%Y"`

mkdir /save/logs/${TODAY}
exec > /save/logs/${TODAY}/stop-log.txt ## Log file 

screen -x ${BUNGEECORD} -X stuff 'maintenance\n' ## Starting maintenance mode
screen -x ${BUNGEECORD} -X stuff 'alert &cVous pourrez vous reconnecter dans 5 minutes environ !\n'

## Loop to advert player from restarting
for time in ${TIMER[@]}
do

    screen -x ${BUNGEECORD} -X stuff 'alert &cRedémarrage automatique journalier dans &4'$TOTAL_TIME' &csecondes.\n'
    TOTAL_TIME=$(($TOTAL_TIME-$time))
    sleep $time

done

cd /home || exit

## Loop to stop every single server of the list above
for server in *
do

    ## Disociate BungeeCord to other server.
    if [ ! $server = ${BUNGEECORD} ]; then

        echo "Shutting down : $server"

        screen -x $server -X stuff "stop\n" ## run stop command in server screen

        if [ $? -eq 0 ]; then
            echo "$server successfully shutting down"
        else
            echo "Error found during shutting down : $server"
        fi

    else 
        echo "Shutting down : ${BUNGEECORD}"

        screen -x ${BUNGEECORD} -X stuff "end\n" ## run stop command in server screen

        if [ $? -eq 0 ]; then
            echo "${BUNGEECORD} successfully shutting down"
        else
            echo "Error found during shutting down : ${BUNGEECORD}"
        fi
    fi

done

sh sauvegarde.sh ## Launch save script

exit

B.

BoBsmil3Y
  • 89
  • 1
  • 8
  • 1
    The array assignment is a "bash" feature. You script must be executed with '#! /bin/bash' or equivalent. – dash-o May 04 '20 at 07:33
  • Also, considering sharing small script that demonstrate the problem, and allows replication. Hard to help you otherwise – dash-o May 04 '20 at 07:33
  • I add all my script, and I already use '#! /bin/bash' :/ . I launch the script with : sh script.sh – BoBsmil3Y May 04 '20 at 07:43
  • 2
    @BoBsmil3Y : You launch it in the wrong way. If you run a script by `sh....`, it will be run by sh. If you would run it by, say, `perl .....`, it would be run by Perl. If you want to do it by bash, run it by `bash script.sh`. Alternatively, do a `chmod +x script.sh` and run it simply by `script.sh`. Your `#!` line will then ensure that bash is used. – user1934428 May 04 '20 at 08:06
  • Thanks ! For me, 'sh' was the standard launch command for bash. – BoBsmil3Y May 04 '20 at 08:31
  • 1
    When you launch with 'sh ...', it will start with '/bin/sh'. When bash starts as '*/sh', it will disable many non-Posix features. Try launching with './script.sh' ,or 'bash ./script.sh' or equivalent. – dash-o May 04 '20 at 09:25
  • @user1934428: Consider posting that as an answer. – Keith Thompson May 04 '20 at 09:53

1 Answers1

0

You launch it in the wrong way. If you run a script by sh ...., it will be run by sh. What sh actually is, depends on the platform. It sometimes is symlinked either bash or zsh, but even then, those shells "know" that they are started by a different name and behave differently.

If you want a script being executed by bash, either run it by bash script.sh or, alternatively, do a chmod +x script.sh and run it simply by ./script.sh. In the latter case, your #! line will then ensure that bash is used.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
user1934428
  • 19,864
  • 7
  • 42
  • 87
  • 1
    And in fact being able to run it as `script.sh` (or `./script.sh` if it's in the current directory -- `.` shouldn't be in your `$PATH` -- but I digress) is the whole point of the `#!/bin/bash` line. – Keith Thompson May 04 '20 at 10:12
  • I absolutely appreciate this disgress! I updated my answer accordingly. – user1934428 May 04 '20 at 11:34