【问题标题】:Bash shell script: f: command not foundBash shell 脚本:f:找不到命令
【发布时间】:2016-10-07 13:27:36
【问题描述】:

我正在编写一个脚本来查找每个目录下的所有 .gz 文件。下面的代码在终端中工作,但在执行 shell 脚本时崩溃。

#!/bin/bash

# Find all directories in Output directory
dirs=($(find ~/Documents/MainDir/$(date +%Y-%m-%d)/Output -type d)) && wait

# Concatenate all .gz files in a dir, unzip gzip & remove unwanted files
for dir in $dirs
do
    if f in $(find . -name "*.gz")
    then
        cd $dir; cat *.gz > output.gz && gunzip -d output.gz && find . -type f -not -name 'output' | xargs rm
    fi
done

起初我试图在没有“do”的情况下运行脚本,结果

syntax error near unexpected token `if'
`if f in $(find . -name "*.gz")'

添加后出现以下错误:

f: command not found

如何解决这个问题?谢谢

【问题讨论】:

  • 使用shellcheck.netfor ... in 而不是if ... in
  • andlrc,if f in $(find . -name "*.gz") 不是if 声明的正确形式!看看7.1. Introduction to ifConditional (computer programming)
  • @andlrc,我从来没有见过 keyword in 在 OP 中与 keyword if 一起使用,也没有它是否用于我之前评论的两个链接中的任何一个。除非您可以提供有效的 boolean 条件 if 语句,该语句也使用 keyword in,我可以在其中进行测试...然后我有支持我以前的cmets。我当然愿意倾斜,但是我看不到 if f in $(find . -name "*.gz") 中的任何正确内容是 if statement 的正确或正确形式!
  • wait 在第一行没有做任何有用的事情(除非之前启动了后台作业并且未显示);在命令替换完成之前,分配不会完成,并且不涉及后台作业。

标签: bash syntax-error sh


【解决方案1】:

另一种处理方法是更高级地使用 find 命令,利用谓词和 -exec 选项,它可以为遇到的每个匹配文件运行一个命令,例如:

rm $dir/output.gz
find $dir -name "*.gz" -a ! -name "output.gz" -exec cat \{\} > $dir/output.gz && rm \{\} \;

(未验证代码)

【讨论】:

    猜你喜欢
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 2013-11-10
    • 2015-07-01
    • 2016-09-27
    • 1970-01-01
    相关资源
    最近更新 更多