如何根据上一个命令的结果运行不同的命令?

发布于 2024-12-19 07:36:59 字数 188 浏览 0 评论 0原文

例如,我尝试使用 cURL 上传 4 个文件(A、B、C、D),但有时会失败。我的脚本是这样的:

for f in `ls`
do
  curl -T $f ftp.server.com
done

A、B、C 上传成功,而 D 出现某种错误。我想要做的是删除 A、B、C,只保留目录中的 D。

For example, there are 4 files (A,B,C,D) which I was try to upload with cURL, but sometimes it failed. My script is like this:

for f in `ls`
do
  curl -T $f ftp.server.com
done

A,B,C were uploaded successfully, while D left with some kind of errors. What I want to do is to remove A,B,C and keep only D in the directory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

感情旳空白 2024-12-26 07:36:59

首先,您已经在小脚本中编写了一个错误:

for files in `ls`

这应该是这样的:

for files in *

不同之处在于 ls 方法无法正确处理名称中带有空格的文件:

$ ls -l
total 8
-rw-r--r-- 1 sarnold sarnold   15 2011-11-23 01:25 bad file
drwxr-xr-x 2 sarnold sarnold 4096 2011-11-21 03:07 blubber
$ for f in `ls` ; do echo === $f === ; done
=== bad ===
=== file ===
=== blubber ===
$ for f in * ; do echo === $f === ; done
=== bad file ===
=== blubber ===
$ 

现在,进入上传 - and-remove 问题:

for f in * ; do curl -T $f ftp.example.com && rm $f ; done

&& 是一个短路运算符;仅当第一个命令返回退出值 0 时,它才会执行第二个命令(这通常意味着进程返回值“成功”——是的,有点倒退)。有一天您可能还会发现 || 运算符很有用,可以在出现故障时执行程序。也许最令人惊讶的是,您可以在一个命令上同时使用它们来根据成功或失败情况酌情执行某些操作:

$ true && echo success || echo failure
success
$ false && echo success || echo failure
failure
$ 

First things first, you've coded a bug into your little script:

for files in `ls`

This should read:

for files in *

The difference is that ls approach won't properly handle files with whitespace in their names:

$ ls -l
total 8
-rw-r--r-- 1 sarnold sarnold   15 2011-11-23 01:25 bad file
drwxr-xr-x 2 sarnold sarnold 4096 2011-11-21 03:07 blubber
$ for f in `ls` ; do echo === $f === ; done
=== bad ===
=== file ===
=== blubber ===
$ for f in * ; do echo === $f === ; done
=== bad file ===
=== blubber ===
$ 

Now, onto the upload-and-remove issue:

for f in * ; do curl -T $f ftp.example.com && rm $f ; done

The && is a short-circuit operator; it will execute the second command only if the first command returns an exit value of 0 (which normally means "successful" in process return values -- yes, a little backwards). You might also someday find the || operator useful, to execute programs if something has failed. And perhaps most surprising, you can use them both on one command to do something on success or failure as appropriate:

$ true && echo success || echo failure
success
$ false && echo success || echo failure
failure
$ 
朮生 2024-12-26 07:36:59

$? 指返回码< /a>.假设“0”表示 curl 上传成功,那么您需要如下所示的内容:

for files in `ls`
do
    curl -T $f ftp.server.com
    if [[ $? -eq 0 ]]; then
      rm $f
    fi
done

$? refers to the return code. Assuming "0" indicates a successful curl upload, then you want something like this:

for files in `ls`
do
    curl -T $f ftp.server.com
    if [[ $? -eq 0 ]]; then
      rm $f
    fi
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文