在bash编码中,line3是来自xyz/symlink_ path s.txt的路径。
while read -r line3
do
if [[ $(ls -lh $line3 | grep zzz.exe | grep '[8-9][0-9][0-9][MG]') -ne 0 ]]
then
echo $line3 >> xyz/size_list.txt
exit 1
fi
done < xyz/symlinks_paths.txt脚本将引发以下错误。(h.sh是脚本名。)
h.sh: line 20: [[: -r--r--r-- 1 syncmgr GX 838M Dec 1 21:55 zzz.txt: syntax error in expression (error token is "r--r-- 1 syncmgr GX 838M Dec 1 21:55 zzz.txt")发布于 2018-02-20 05:58:17
这里的问题是,您正在尝试解析ls的输出。这总是一个坏主意。有关为什么会出现这种情况的解释,请参见为什么*不*解析ls?。
如果您想要一个文件的大小,那么使用stat。例如:
minsize=$(( 800 * 1024 * 1024 ))
# alternatively, if you have `numfmt` from GNU coreutils, delete the line above
# and uncomment the following line:
#minsize=$(echo 800M | numfmt --from=iec)
while read -r line3 ; do
if [ "$(stat -L --printf '%s' "$line3")" -gt "$minsize" ]; then
echo "$line3" >> xyz/size_list.txt
fi
done < xyz/symlinks_paths.txt注意:我在上面使用了stat's -L (又名--dereference)选项,因为输入文件名意味着它中列出的文件名可能是符号链接。没有-L,stat不会跟随符号链接,它会打印符号链接本身的大小。
如果希望将文件大小与文件名一起打印到输出文件中,则while循环将更类似于以下内容:
while read -r line3 ; do
fsize=$(stat -L --printf '%s' "$line3")
if [ "$fsize" -gt "$minsize" ]; then
fsize=$(echo "$fsize" | numfmt --to=iec)
printf "%s\t%s\n" "$fsize" "$line3" >> xyz/size_list.txt
fi
done < xyz/symlinks_paths.txthttps://unix.stackexchange.com/questions/425308
复制相似问题