0
点赞
收藏
分享

微信扫一扫

量化交易之linux篇 - shell脚本 - 遍历当前目录并判断文件类型及后缀


# bin/bash!

# 第一种写法.
for file in $(ls ./);
do

if [ -f $file ] && [[ $file == *".py" ]]
then
echo $file is py file.

elif [ -f $file ] && [[ $file == *".txt" ]]
then
echo $file is txt file.

elif [ -d $file ]
then
echo $file is dir.
rm -rf $file

else
echo $file is not file \| dir.

fi

done


# 第二种写法
function file_judge {
if [ $# -ne 1 ]
then
echo param count is not 1.
exit
fi

if [[ $1 == *".py" ]];
then
echo $1 is py file.

elif [[ $1 == *".txt" ]];
then
echo $1 is txt file.

elif [[ $1 == *".sh" ]];
then
echo $1 is shell file.

else
echo $1 is other file.

fi
}


for file in $(ls ./);
do
if [ -f $file ]
then
file_judge $file

elif [ -d $file ]
then
echo $file is dir.
rm -rf $file

else
echo $file is not file \| dir.

fi

done

举报

相关推荐

0 条评论