首页 > 解决方案 > 多个“。”的外壳文件扩展名

问题描述

我需要提取目录中文件的文件扩展名,但是文件格式如下,

ab10001i.39400.8163.txt
ab10001i.39400.0433.txt
ab10001i.39400.24433.txt
ab10001i.39400.90631.TXT

我尝试了以下脚本,但没有给我预期的结果..有什么建议吗?谢谢你!!

for file in "$SEARCH_DIR"/*; do
  clmFile=`basename "$file"`
  lastpart=``echo -n ${clmFile%.*} | tail -c4``
  echo "TEST THIS THIS " $lastpart
  if [ ${extension} == "TXT" ]; then
     echo "TXT FILE"
  elif [ ${extension} == "txt" ]; then
     echo "txt FILE"
  fi
done

标签: shellunix

解决方案


找到了解决办法。。

lastpart=`echo -n ${clmFile%} | tail -c4

这将提取如下输出

txt
txt
txt
TXT

我的输入文件

ab10001i.39400.8163.txt
ab10001i.39400.0433.txt
ab10001i.39400.24433.txt
ab10001i.39400.90631.TXT

这是一个好的脚本练习吗?还有其他建议或建议吗?


推荐阅读