2016-06-07 197 views
1

我正在编写一个脚本,用于查找每个目录下的所有.gz文件。下面的代码在终端中工作,但在执行shell脚本时崩溃。Bash shell脚本:f:找不到命令

#!/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 

起初,我试图运行该脚本不“做”,这导致

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

加入后做,我发现了以下错误:

f: command not found 

如何解决这个问题? THX

+2

使用http://www.shellcheck.net/和它的'为... in'不'如果... in' – user3439894

+0

andlrc,'如果f $中( find。-name“* .gz”)**不是'if'_statement_的正确形式**!看看[7.1。如果介绍](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html)和[有条件的(计算机编程)](https://en.wikipedia.org/wiki/Conditional_(计算机程序设计)) – user3439894

+0

@andlrc,我从来没有见过与OP中的_keyword_“if”一起使用的_keyword_“in”,也没有在我之前评论的两个链接中使用。除非你可以提供一个有效_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _声图,我肯定愿意倾斜,但是我无法在$(find。-name“* .gz”)中看到任何正确的'if if f'作为'if'_statement_的正确和恰当的形式! – user3439894

回答

0

另一种方式来处理,这可能是有一个更高级的使用find命令的,趁着谓词和-exec选项,它可以运行每个匹配文件的命令遇到的,如:

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

(未验证码)