【发布时间】: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.net 是
for ... in而不是if ... in -
andlrc,
if f in $(find . -name "*.gz")不是if声明的正确形式!看看7.1. Introduction to if 和Conditional (computer programming) -
@andlrc,我从来没有见过 keyword
in在 OP 中与 keywordif一起使用,也没有它是否用于我之前评论的两个链接中的任何一个。除非您可以提供有效的 boolean 条件if语句,该语句也使用 keywordin,我可以在其中进行测试...然后我有支持我以前的cmets。我当然愿意倾斜,但是我看不到if f in $(find . -name "*.gz")中的任何正确内容是ifstatement 的正确或正确形式! -
wait在第一行没有做任何有用的事情(除非之前启动了后台作业并且未显示);在命令替换完成之前,分配不会完成,并且不涉及后台作业。
标签: bash syntax-error sh