在命令内使用 shell 变量
我的服务器使用的是 CentOS 5.5(几乎是 Red Hat Linux)。
我想将一组图片备份到带时间戳的文件中。 这段代码可以工作:
z_cmd1=$(tar cvzf /home/user1/public_ftp/misc/pics_20100925_142230.tar.gz /home/user1/public_html/misc/_pics_var/F???????.jpg)
echo "tar output =[${z_cmd1}]"
但是我当然希望时间戳是自动的。
下面的代码不起作用。不知何故,第三行(带有 tar 的那一行)失败了。 “tar”做了一些事情,但它不会在预期的目标文件夹中创建任何文件。为什么?
z_fname=$(date +"/home/user1/public_ftp/misc/pics_%Y%m%d_%H%M%S.tar.gz")
echo "File name =[${z_fname}]"
z_cmd1=$(tar cvzf ${z_fname} /home/user1/public_html/misc/_pics_var/F???????.jpg)
echo "tar output =[${z_cmd1}]"
谢谢。
my server is using CentOS 5.5 (which is almost Red Hat Linux).
I want to backup a set of pictures into time-stamped files.
This code would work:
z_cmd1=$(tar cvzf /home/user1/public_ftp/misc/pics_20100925_142230.tar.gz /home/user1/public_html/misc/_pics_var/F???????.jpg)
echo "tar output =[${z_cmd1}]"
but of course I want the time stamp to be automatic.
The following code does not work. Somehow, the third line (the one with the tar) fails. 'tar' does something, but it does not create any file at the expected destination folder. Why?
z_fname=$(date +"/home/user1/public_ftp/misc/pics_%Y%m%d_%H%M%S.tar.gz")
echo "File name =[${z_fname}]"
z_cmd1=$(tar cvzf ${z_fname} /home/user1/public_html/misc/_pics_var/F???????.jpg)
echo "tar output =[${z_cmd1}]"
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这样的事情:
我们创建一个日期作为变量。
我们使用输出变量中的日期变量创建输入和输出变量。
然后执行命令并检查是否执行没有错误。
Try this something like this:
We create a date as variable.
We create the input and output variables, using the date variable in the output variable.
Then execute the command and check if it executed without errors.