1

I wrote this script, but I don't know why I have a syntax error:

#!/bin/bash
    echo Indica la fecha de hoy en formato dd-mm-aaaa
    read date
    echo Indica la hora actual en formato hh-mm
    read hora
    mkdir -p /re/plugins
    shopt -s extglob
    cp -R /files/spigot/plugins/!(dynmap) /re/plugins
    shopt -u extglob
    cp -R /files/spigot/world /re
    zip -r "$date"-"$hora".zip plugins world
    mv /re/*.zip /var/www/html/backup
    rm -R /re
    Echo copia de seguridad realizada correctamente el dia "$date" a las "$hora"h > /log.txt

The output

root@alex:/home/alex# sh pb.sh
Indica la fecha de hoy en formato dd-mm-aaaa
aaa
Indica la hora actual en formato hh-mm
sdf
pb.sh: 7: pb.sh: shopt: not found
pb.sh: 8: pb.sh: Syntax error: "(" unexpected
root@alex:/home/alex# 
2

2 Answers 2

9

Your problem is right here:

root@alex:/home/alex# sh pb.sh  

shopt is a shell builtin (see type shopt). /bin/sh isn't bash on Ubuntu, but a compatibility shell called dash. Dash doesn't have shopt.

Since the first line of your script is the shebang line (#!/bin/bash) you can just run your script like this:

./pb.sh  

and it should work fine. You may need to set the file as executable by running:

chmod +x pb.sh
0
2

pb.sh: 8: pb.sh: Syntax error: "(" unexpected

The !(dynmap) syntax is only recognized when the extglob shell option has been set, and it hadn't:

pb.sh: 7: pb.sh: shopt: not found

The shopt command wasn't found because it's a shell builtin and the particular shell that ran the script doesn't support it, as Seth's answer explains.

1
  • Good idea for clarifying the syntax error. I thought it was obvious but better be safe than sorry :)
    – Seth
    Nov 1, 2015 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.