【问题标题】:Bash for loop return null stop program?Bash for循环返回空停止程序?
【发布时间】:2014-01-22 14:16:36
【问题描述】:

我正在制作一个搜索文件并计算它们的脚本。后来,我使用这个计数值来创建备份文件。这就是它的样子:

#!/bin/sh
#LOCAL VAR PERFECTLY DEFINED



 if $estdifferent ; then 
        x=0;
        for fic in `find $OTHERDIR/liste/ -type f -name "${filename}_bak*"` ; do
        {

            filename_tab[x]=$fic
            ((x++))
        }
        echo "tableau rempli, nombre de fichier bak : "$x

此代码块在“查找”至少找到一个文件时有效,但在有 0 个文件时无效。 脚本没有到达最后一个“回声”。调试模式在以下位置停止脚本:

echo differents
differents
true
x=0
find /tech/gtr/scripts/osm/scan-configs/liste/ -type f -name 'IDF-952584-S...

为什么?

【问题讨论】:

  • /bin/sh 是什么风格和版本?这不是有效的bash 语法。 for 循环看起来像 for ... ; do ... done,而不是 { ... } 块。
  • 红帽的 Bash 4.1.2(1)。找到文件时,此“for”语法起作用:)

标签: bash for-loop int


【解决方案1】:

尝试替换:

for fic in `find $OTHERDIR/liste/ -type f -name $filename"_bak"*`;do
{
    echo $fic
    filename_tab[x]=$fic
    ((x++))
}

通过

for fic in `find $OTHERDIR/liste/ -type f -name $filename"_bak"*` ; do
    echo $fic
    filename_tab[x]=$fic
    ((x++))
done

请?

【讨论】:

  • 调试说:++ find /tech/gtr/scripts/osm/scan-configs/liste/ -type f -name find: missing argument to `-name' 但它也说,一些我的 $filename var 不为空的上一行
  • @user2564494 这应该表明您的问题。并不是 find 没有找到任何东西 - 您的模式中的 * 字符正在被 shell 看到并展开。尝试改用-name "${filename}_bak*",因为引用会阻止全局扩展。
  • 我的发现看起来像:find $OTHERDIR/liste/ -type f -name "${filename}_bak*" 中的 fic;但是同样,不管有没有 shopt -s nullglob,脚本都会在这个发现处停止。
  • @user2564494 你可能想修复你的问题中发布的代码,因为它看起来绝对不是那样的......
猜你喜欢
  • 1970-01-01
  • 2019-05-20
  • 1970-01-01
  • 1970-01-01
  • 2019-07-29
  • 2022-01-11
  • 2013-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多