Why does script not recognize file extension?

2019-02-21 06:16发布

My script

#!/bin/bash

cp *.ats /home/milenko/procmt

mycd() {
  cd /home/milenko/procmt
}

mycd

EXT=ats
for i in *; do
    if [ "${i}" != "${i%.${EXT}}" ];then
        ./tsmp -ascii i
    fi
done

But

milenko@milenko-HP-Compaq-6830s:~/Serra do Mel/MT06/meas_2016-07-13_20-22-00$ bash k1.sh


./tsmp: handling 1 files ************************************** total input channels: 1
the name of your file does not end with ats ... might crash soon

main (no rda) -> can not open i for input, exit


./tsmp: handling 1 files ************************************** total input channels: 1
the name of your file does not end with ats ... might crash soon

main (no rda) -> can not open i for input, exit

When I go to procmt directory and list files

milenko@milenko-HP-Compaq-6830s:~/procmt$ ls *.ats
262_V01_C00_R000_TEx_BL_2048H.ats  262_V01_C00_R086_TEx_BL_4096H.ats  262_V01_C02_R000_THx_BL_2048H.ats
262_V01_C00_R000_TEx_BL_4096H.ats  262_V01_C01_R000_TEy_BL_2048H.ats  262_V01_C03_R000_THy_BL_2048H.ats

What is wrong with my script?

标签: linux bash shell
1条回答
Fickle 薄情
2楼-- · 2019-02-21 06:41

If I understand correctly this should work for you:

dest='/home/milenko/procmt'

cp *.ats "$dest"

cd "$dest"

for i in *.ats; do
     ./tsmp -ascii "$i"
done

There is no need to loop through all files when you're only interested in .ats files. Your mycd function is just doing cd so you can avoid that as well.

查看更多
登录 后发表回答