Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

Weird formatting with bash script + it trying to execute unwanted commands

I'm trying to make a script to backup my Minecraft server and it was working fine yesterday but not anymore.

Code:

#!/bin/bash
mc_backup() {
    now=$(date "+%Y-%m-%d_%Hh%Mm")
    backupFile="$backupPath/${server}_${now}.tar"
    cd "$serverPath" || exit
    dir=("$(ls)")
    for target in "${unwanted[@]}"; do
        for i in "${!dir[@]}"; do
            if [[ ${dir[i]} = "$target" ]]; then
                unset 'dir[i]'
            fi
        done
    done
    for i in "${!dir[@]}"; do
        updatedDir+=("${dir[i]}")
    done
    dir=("${updatedDir[@]}")
    unset updatedDir
    for i in "${dir[@]}"; do
        echo "Backing up the server ($i)..."
        as_user "tar -C $serverPath -rf $backupFile $i"
    done
    echo "Compressing backup..."
    as_user "gzip -f $backupFile"
    echo "Done."
}

Output:

Backing up the server (backups
banned-ips.json
banned-players.json
logs
nukkit.jar
nukkit.yml
ops.txt
players
plugins
resource_packs
server.properties
white-list.txt
worlds)...
tar: backups/Nukkit_2021-01-21_14h46m.tar: file is the archive; not dumped
bash: line 1: banned-ips.json: command not found
bash: line 2: banned-players.json: command not found
bash: line 3: logs: command not found
bash: line 4: nukkit.jar: command not found
bash: line 5: nukkit.yml: command not found
bash: line 6: ops.txt: command not found
bash: line 7: players: command not found
bash: line 8: plugins: command not found
bash: line 9: resource_packs: command not found
bash: line 10: server.properties: command not found
bash: line 11: white-list.txt: command not found
bash: line 12: worlds: command not found
Compressing backup...
Done.

I want to know how to fix the bash: line *: *: command not found errors and the fact that all the logs of what it's backing up are bunched together: Backing up the server (long list)... instead of

Backing up the server (a)...
Backing up the server (b)...

Could it be my tar command is incorrect and that's messing with the rest of the script?

question from:https://stackoverflow.com/questions/65834902/weird-formatting-with-bash-script-it-trying-to-execute-unwanted-commands

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Most likely, a new file has been created that contains a * or something similar, which is being interpreted as a wildcard or other special pattern instead of a filename.

If you can control the file names, an easy hacky fix is to make sure there are no special characters in file names. If not, you need to more carefully quote and escape the variables you are using to build the tar and gzip commands.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...