bash'ls'在检查文件存在后找不到文件

时间:2019-07-17 00:57:01

标签: linux bash

我有这个简单的代码

#!/bin/bash

TIMEOUT=60

# call a function that creates a file
LOG_FILE_NAME="/dir/something.txt"
create_a_file(LOG_FILE_NAME)

until [ ! -f ${LOG_FILE_NAME} ] || [ "$TIMEOUT" == "0" ]
do
    echo "Slept 1 second while waiting for file creation"
    sleep 1
    ((TIMEOUT--))
done

for file in $(ls -tr /dir/*.txt); do
    #something
done

大多数情况下都可以,但是有时我得到: ls: cannot access /dir/*.txt: No such file or directory

1 个答案:

答案 0 :(得分:0)

  

ls:无法访问/dir/*.txt:没有此类文件或目录

这意味着<div class ="all-text"> <div align="center" ><b>TITLE</b></div> <br><br> <div class ="lista-principal"> <!-- div da lista ordenada principal --> <ol> <!-- Lista ordenada principal --> <!-- Inicio do ITEM 1 da lista principal P1 --> <li> <span>TITULO</span> <ol> <li> But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?" </li> </ol> </li> <!-- Fim do ITEM 1 da lista principal P1 --> <!-- Inicio do ITEM 2 da lista principal P1 --> <li> <span>OBJETO</span> <ol> <li> At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. </li> <li> At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. </li> <li> At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. </li> </ol> </li> <!-- Fim do ITEM 2 da lista principal P1 --> </ol> <!-- FIM da lista ordenada principal --> 中没有*.txt个文件。 (当glob与任何内容都不匹配时,shell将文字星号传递给/dir/,就像您写了ls一样。)

问题是循环条件。它不应该有ls '/dir/*.txt'

!

(但是,如果超时达到0,您仍然会看到相同的错误。)

顺便说一句,最好使用until [ -f ${LOG_FILE_NAME} ] || [ "$TIMEOUT" == "0" ] 以避免引用问题。

[[