【问题标题】:Compress a dictionary with shell script使用 shell 脚本压缩字典
【发布时间】:2011-03-16 19:06:08
【问题描述】:

我想使用以下格式压缩字典:2011.16.03_root_backup.tar.gz(日期后跟用户名)。但是我的 shell 脚本代码不起作用,并且缺少一些代码。你能帮帮我吗?

#!/bin/sh
date=$(date +"%y-%d-%m")
user=$(whoami)
target=(i want to get this as an argument)
tar -cvf ./$date_$user_backup.tar.gz $target 

【问题讨论】:

  • 您的脚本在哪些方面不起作用?这是有用的信息

标签: linux shell unix ubuntu compression


【解决方案1】:

在你的 tar 命令中试试这一行:

tar -czvf ./${date}_${user}_backup.tar.gz ${target}

问题是 $date 或 $user 之后的 _ 不被 shell 视为单词分隔符,这就是这里需要 ${var} 形式的原因。

【讨论】:

  • 你确定它会在没有z选项的情况下压缩它吗?
  • @khachik:谢谢,这是一个很好的发现,这是一个错字。我在上面的 tar 命令中添加了 z 选项。
  • 下划线不被视为单词分隔符,因为它是变量名中的有效字符。
猜你喜欢
  • 1970-01-01
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-15
相关资源
最近更新 更多