【问题标题】:Check multiple files having size greater than zero in KornShell在 KornShell 中检查大小大于零的多个文件
【发布时间】:2018-10-18 08:40:58
【问题描述】:

下面是一个简单的脚本,用于确定所有文件是否存在并且大小是否大于零。从here 我知道'-s' 用于该任务。

if [ -s ${file1} && -s ${file2} && -s ${file3}]; then
   echo "present"
   echo "Perform analysis"
else
   echo "not present";

   echo "Value of file1: `-s ${file1}`"
   echo "Value of file2: `-s ${file2}`"
   echo "Value of file3: `-s ${file3}`"
   echo "skip";
fi

文件位于与脚本相同的路径上。我检查了文件名,它是正确的。我收到以下错误:

./temp.ksh[]: [-s: not found [No such file or directory]
not present
./temp.ksh[]: -s: not found [No such file or directory]
Value of file1:
./temp.ksh[]: -s: not found [No such file or directory]
Value of file2:
./temp.ksh[]: -s: not found [No such file or directory]
Value of file3:

我似乎无法找出上面有什么问题。这是 KornShell 特有的吗?我只能使用 KSH。

【问题讨论】:

  • 右方括号之间需要一个额外的空格。 if [ -s ${file1} && -s ${file2} && -s ${file3} ];
  • 我尝试添加额外的空间。现在错误是 ./temp.ksh[]: [: ']' missing Between.... echo 至少应该可以工作。

标签: linux shell ksh file-handling


【解决方案1】:

参考this问题的答案。错误是在 if 语句中使用 [ ] 而不是 [[ ]] 因为 [[ ]] 可以解释 && 但 [ ] 不能。

【讨论】:

    【解决方案2】:

    您似乎误解了test 命令,它也写为[ Conditional Expression ]。条件表达式可以写在test 命令中,但不能用作可执行语句。

    所以不要这样做

    echo "Value of file1: `-s ${file1}`"
    

    但是

    echo "Value of file1: $( [[ -s ${file1} ]] && echo 0 || echo 1)"
    

    另外,-s 不返回大小,而是检查大小是否为零作为返回码。

    此外,test 命令不知道&&(如Ankit Chauhun's answer 所述)。

    所以不要这样做

    if [ -s ${file1} && -s ${file2} && -s ${file3} ]; then
    

    但是任何一个

    if [ -s ${file1} ] && [ -s ${file2} ] && [ -s ${file3} ]; then
    if [[ -s ${file1} && -s ${file2} && -s ${file3} ]]; then
    

    您可能对以下内容感兴趣:

    if [[ -s ${file1} && -s ${file2} && -s ${file3} ]]; then
       echo "present"
       echo "Perform analysis"
    else
       echo "not present";
    
       stat --printf="Value of file1: %s" "$file1"
       stat --printf="Value of file2: %s" "$file2"
       stat --printf="Value of file3: %s" "$file3"
       echo "skip";
    fi
    

    【讨论】:

    • 我按照你的建议做了。但是 echo 输出是空白的,对于 if 语句,它是 : ./temp.ksh[]: [: ']' missing 。我还打印了文件名并尝试使用相同的名称,我能够看到文件的内容。
    • 现在我在 echo 中得到 0,这是您在 But do 之后建议的。但是对于 if 语句,它仍然给我在上面的评论中提到的同样的错误。
    【解决方案3】:

    其他答案对我来说看起来不错,但我必须尝试一下才能看到。 这对我有用:

    file1=a.txt
    file2=b.txt
    file3=c.txt
    
    if [[ -s ${file1} && -s ${file2} && -s ${file3} ]]
    then
        echo "present"
        echo "Perform analysis"
    else
        echo "not present";
    
        for name in ${file1} ${file2} ${file3}
        do
            if [[ ! -s ${name} ]]
            then
                date > ${name}  # put something there so it passes next time
                echo "just created ${name}"
            fi
        done
        echo "skip";
    fi
    

    我将它放入一个名为 checkMulti.sh 的文件中并得到以下输出:

    $ ksh checkMulti.sh
    not present
    just created a.txt
    just created b.txt
    just created c.txt
    skip
    $ ksh checkMulti.sh
    present
    Perform analysis
    $ rm a.txt
    $ ksh checkMulti.sh
    not present
    just created a.txt
    skip
    $ ksh checkMulti.sh
    present
    Perform analysis
    

    使用单括号 [ 在 ksh 88 中消失了。我建议这些天总是使用双 [[。

    另外,请检查以确保括号前后以及破折号之前有空格。我只是试图重现您的错误并得到(这是错误的):

    $ if [[-s fail]]   #<<< missing space before -s and after filename
    > then
    > echo yes
    > fi
    -ksh: [[-s: not found [No such file or directory]
    $
    

    但如果我输入所需的空间(并创建一个文件),我会得到:

    $ date > good  # put something in a file
    $ if [[ -s good ]]
    > then
    >     echo yes
    > fi
    yes
    $
    

    【讨论】:

      猜你喜欢
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 2012-02-09
      相关资源
      最近更新 更多