2016-06-07 195 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 \{\} \; 

(未驗證碼)