我真的很喜欢bash脚本,我正在学习。我有一个脚本,我做了一个单线程
after=d_20141123_0437; before=d_20141124_0440; for a in $(hadoop fs -ls -R /path/to/directory/d_2014112* | grep -oE '/(.*)') ; do ; if { test "$a" \> "$after" && test "$a" \> "$before" ; } || ; test "$a" = "$after" || test "$a" = "$before" ; then ; echo "$a" ; if ; done
这是将命令转换为单行之前的命令:
after=d_20141123_0437
before=d_20141124_0440
for a in * ; do
if { test "$a" \> "$after" && test "$a" \< "$before" ; } ||
test "$a" = "$after" || test "$a" = "$before" ; then
echo "$a"
fi
done
我尝试运行此命令,但收到错误:
-bash: syntax error near unexpected token `;'
我似乎无法弄清楚如何解决这个问题,因为我不熟悉bash脚本。我想知道是否有人可能是另一双眼睛来帮我解决这个错误
答案 0 :(得分:1)
在BASH中尝试:
after=d_20141123_0437; before=d_20141124_0440; while read -r a; do if { test "$a" \> "$after" && test "$a" \> "$before" ; } || test "$a" = "$after" || test "$a" = "$before"; then echo "$a"; fi ; done < <(hadoop fs -ls -R /path/to/directory/d_2014112* | grep -oE '/(.*)')
答案 1 :(得分:1)
当我看一下单行时,只需在空格上分割,就会有比多行版本更不同的逻辑。具体来说,您对文件名($ a)的测试是在&#39;之间进行的。 $ before和$ after不同,
after=d_20141123_0437;
before=d_20141124_0440;
for a in $(hadoop fs -ls -R /path/to/directory/d_2014112* | grep -oE '/(.*)') ; do
;
if { test "$a" \> "$after" && test "$a" \> "$before" ; } || ; test "$a" = "$after" || test "$a" = "$before" ; then ;
echo "$a" ;
if ;
done
具体来说,有一个&#39 ;;&#39; (分号)在第二次出现之前测试&#39;。
您可以将此测试重写为
if [ "$a" \< "$before" ] ; then next; fi
if [ "$a" \> "$after" ] ; then next; fi
echo "$a" ;