日期:2014-05-16  浏览次数:20710 次

写了一个sh脚本,执行的时候出现如下语法错误,怎么解决
脚本信息:
文件名:filestat.sh
文件内容:
if [ $# -ne 1 ];
then
  echo $0 basepath;
  echo
fi
path=$1

declare -a statarray;

while read line;
do
  ftype=`file -b "$line"`
  let statarray["$ftype"]++;
done<< (find $path -type f -print)

echo===============file types and counts===================
for ftype in "${!statarray[@]}";
do
  echo $ftype:${statarray["$ftype"]}
done


运行结果:
[zhang@localhost temp]# ./filestat.sh
./filestat.sh basepath

./filestat.sh: line 14: syntax error near unexpected token `('
./filestat.sh: line 14: `done<< ( find $path -type f -print );'

------解决方案--------------------
done<< (find $path -type f -print)
这样用比较奇怪吧,先把结果保存到一个文件,再重定向试试。
------解决方案--------------------
<<需要的是文件描述符
要想将find $path -type f -print结果read,使用for line in $(find $path -type f -print);do xxxx;done
------解决方案--------------------
正解
for line in `find $path -type f -printf`
do
XXX
done


探讨

谢谢,但是我想知道,这样做的错误如何改正引用:
done<< (find $path -type f -print)
这样用比较奇怪吧,先把结果保存到一个文件,再重定向试试。