将 bash 变量放入 zip 命令的文件名中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9590034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting bash variable into filename for zip command
提问by Jared Henderson
In a bash script, how do I use a variable to create a specifically named zipped file? For example, I would like to do something like:
在 bash 脚本中,如何使用变量来创建特定命名的压缩文件?例如,我想做这样的事情:
VERSION_STRING='1.7.3'
zip -r foo.$VERSION_STRING foo
Where I ideally end up with a file called foo.1.7.3.zip
理想情况下,我最终会得到一个名为 foo.1.7.3.zip
It seems like I'm having 2 problems:
好像我有两个问题:
- the zip command is treating
$VERSION_STRING
like it's null or empty - the
.
afterfoo
also seems to be mucking it up
- zip 命令将
$VERSION_STRING
其视为 null 或空 - 在
.
后foo
似乎也摆弄起来
采纳答案by NPE
The following works fine here using bash 4.1.5:
以下使用 bash 4.1.5 可以正常工作:
#!/bin/bash
VERSION_STRING='1.7.3'
echo zip -r foo foo.$VERSION_STRING.zip
I've added the echo
to see the actual command rather than run it. The script prints out
我添加了echo
查看实际命令而不是运行它。脚本打印出来
zip -r foo foo.1.7.3.zip
回答by cyber-monk
you can use ${VERSION_STRING}
to clearly wrap your variable name
你可以${VERSION_STRING}
用来清楚地包装你的变量名
回答by Andy
I think you have your zip inputs backwards. For me the following command:
我认为您的 zip 输入向后。对我来说以下命令:
zip -r foo.${VERSION_STRING}.zip foo
creates a valid zip file. Your command does not (at least with my version of zip).
创建一个有效的 zip 文件。您的命令没有(至少在我的 zip 版本中)。