Variables as commands in bash scripts

前端 未结 5 1437
名媛妹妹
名媛妹妹 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:54

    There is a point to only put commands and options in variables.

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

    You can relocate the commands to another file you source, so you can reuse the same commands and options across many scripts. This is very handy when you have a lot of scripts and you want to control how they all use tools. So standard_tools would contain:

    export tar_create="tar cv"
    export openssl="openssl des3 -salt"
    export split_1024="split -b 1024m -"
    

提交回复
热议问题