在命令内使用 shell 变量

发布于 2024-09-25 05:30:37 字数 654 浏览 0 评论 0原文

我的服务器使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謌踐踏愛綪 2024-10-02 05:30:37

尝试这样的事情:

mydate=`date +"%m-%d-%Y"`;
input="/home/user1/public_html/misc/_pics_var/F???????.jpg";
output="/home/user1/public_ftp/misc/pics_$mydate.tar.gz";
tar cvzf $output $input && echo "$output created succesfully!";
if [ ! $? == 0 ]; then echo "$output failed"; fi;

我们创建一个日期作为变量。
我们使用输出变量中的日期变量创建输入和输出变量。
然后执行命令并检查是否执行没有错误。

Try this something like this:

mydate=`date +"%m-%d-%Y"`;
input="/home/user1/public_html/misc/_pics_var/F???????.jpg";
output="/home/user1/public_ftp/misc/pics_$mydate.tar.gz";
tar cvzf $output $input && echo "$output created succesfully!";
if [ ! $? == 0 ]; then echo "$output failed"; fi;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文