变量作为的bash脚本命令 [英] Variables as commands in bash scripts

查看:102
本文介绍了变量作为的bash脚本命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写措斯给定目录中的一个非常简单的bash脚本,加密的的输出,然后将所得的文件分割成多个较小的文件,因为备份介质不支持大文件

我没有很多使用bash脚本的经验。我相信,我在与正确引用我的变量,允许在参数空间的问题。该脚本如下:

 #! /斌/庆典#这个脚本措斯给出的目录,将其加密,并把
#它给定的目录(可能是一个USB密钥)。如果[$#-ne 2]
然后
    回声用法:`基本名$ 0` DIRECTORY backup_directory中
    1号出口
科幻DIRECTORY = $ 1
backup_directory中= $ 16
BACKUP_FILE =$ backup_directory中/`日期+%Y-%M-%胸苷%H-%M-%S.backup`TAR_CMD =焦油CV $目录
SPLIT_CMD =分裂-b1024米 - \\$ BACKUP_FILE \\ENCRYPT_CMD ='OpenSSL的DES3 -salt回声$ TAR_CMD | $ ENCRYPT_CMD | $ SPLIT_CMD$ TAR_CMD | $ ENCRYPT_CMD | $ SPLIT_CMD说做备份

运行此命令失败:


  

分裂:富/ 2009-04-27T14-32-04.backupAA:没有这样的文件或目录


我可以通过删除周围 $ BACKUP_FILE 报价在哪里设置 $ SPLIT_CMD 修复它。但是,如果我在备份目录的名称中含有空格这是行不通的。另外,如果我复制并粘贴从回声命令的输出直接到终端,它工作正常。显然有什么东西我不理解Bash是如何逃脱的事情。


解决方案

根本就没有把整个命令变量。你会惹上很多麻烦试图恢复报价参数。

还有:


  1. 避免使用全大写字母在脚本中的变量名。简单的方法来拍摄自己的脚。

  2. 请不要使用反引号,使用$(...)代替,它的巢更好。


 #! /斌/庆典如果[$#-ne 2]
然后
    回声用法:$(基名$ 0)DIRECTORY backup_directory中
    1号出口
科幻目录= $ 1
backup_directory中= $ 16
CURRENT_DATE = $(日期+%Y-%M-%胸苷%H-%M-%S)
backup_file =$ {backup_directory中} / $ {} CURRENT_DATE的.backup焦油CV$目录| OpenSSL的DES3 -salt |拆分-b1024米 - $ backup_file

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 backup media don't support huge files.

I don't have a lot of experience with bash scripting. I believe I'm having issues with quoting my variables properly to allow spaces in the parameters. The script follows:

#! /bin/bash

# This script tars the given directory, encrypts it, and transfers
# it to the given directory (likely a USB key).

if [ $# -ne 2 ]
then
    echo "Usage: `basename $0` DIRECTORY BACKUP_DIRECTORY"
    exit 1
fi

DIRECTORY=$1
BACKUP_DIRECTORY=$2
BACKUP_FILE="$BACKUP_DIRECTORY/`date +%Y-%m-%dT%H-%M-%S.backup`"

TAR_CMD="tar cv $DIRECTORY"
SPLIT_CMD="split -b 1024m - \"$BACKUP_FILE\""

ENCRYPT_CMD='openssl des3 -salt'

echo "$TAR_CMD | $ENCRYPT_CMD | $SPLIT_CMD"

$TAR_CMD | $ENCRYPT_CMD | $SPLIT_CMD 

say "Done backing up"

Running this command fails with:

split: "foo/2009-04-27T14-32-04.backup"aa: No such file or directory

I can fix it by removing the quotes around $BACKUP_FILE where I set $SPLIT_CMD. But, if I have a space in the name of my backup directory it doesn't work. Also, if I copy and paste the output from the "echo" command directly into the terminal it works fine. Clearly there's something I don't understand about how Bash is escaping things.

解决方案

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"

这篇关于变量作为的bash脚本命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆