Variables as commands in bash scripts

前端 未结 5 1440
名媛妹妹
名媛妹妹 2020-11-28 10:54

I am writing a very simple bash script that tars a given directory, encrypts the output of that, and then splits the resultant file into multiple smaller files since the bac

5条回答
  •  春和景丽
    2020-11-28 11:42

    Simply don't put whole commands in variables. You'll get into a lot of trouble trying to recover quoted arguments.

    Also:

    1. Avoid using all-capitals variable names in scripts. Easy way to shoot yourself on the foot.
    2. Don't use backquotes, use $(...) instead, it nests better.

    #! /bin/bash
    
    if [ $# -ne 2 ]
    then
        echo "Usage: $(basename $0) DIRECTORY BACKUP_DIRECTORY"
        exit 1
    fi
    
    directory=$1
    backup_directory=$2
    current_date=$(date +%Y-%m-%dT%H-%M-%S)
    backup_file="${backup_directory}/${current_date}.backup"
    
    tar cv "$directory" | openssl des3 -salt | split -b 1024m - "$backup_file"
    

提交回复
热议问题