[[+content_image]]
G
G
gagareg2020-04-18 17:27:20
bash
gagareg, 2020-04-18 17:27:20

Why does if work like this in bash?

Good afternoon, there is such a simple script

NAME=$1
FORMAT=$2
INPUT_FILE=$3
DATA=$(date "+%Y-%m-%d")
ARHIVE_NAME=$(echo $NAME-$DATA.$FORMAT)

# tar or zip
if [[ "$FORMAT"=="tar" ]]; then
    tar cf $ARHIVE_NAME $INPUT_FILE 
    echo "tar successfully"
elif [[ "$FORMAT"=="zip" ]]; then
    zip $ARHIVE_NAME $INPUT_FILE
    echo "zip successfully"
fi


Why, regardless of the second parameter, does it always output tar successfully .

~/Documents/Bash/bash-scripting
❯ bash creating-archives.sh isamarskiy zip file
tar successfully

~/Documents/Bash/bash-scripting
❯ bash creating-archives.sh isamarskiy tar file
tar successfully

❯ ls
creating-archives.sh  file  isamarskiy-2020-04-18.tar  isamarskiy-2020-04-18.zip


As we can see, archives are created, but if you specify zip, then for some reason echo does not work.

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
X
xupyp1, 2020-04-18
@gagareg

In the end, in any case, you get tar (check, try to unpack)
You insert the extension through a variable, but if only works according to the first condition.
Stupid syntax error
[[ "$FORMAT"=="tar" ]]
replace with
[[ "$FORMAT" == "tar" ]]
with zip equivalent

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question