close

[shell script]check file exist use regex / process file

早上zz 在討論一個問題,
他想排除ls 出來的file list 的某些檔案,
使用的是regex。

jar_file="./ggyy-zz.jar"

if [ $jar_file == "~/ggyy-*.jar" ]; then
echo "ok"
fi

filenames=`ls -a`
if [ -e ./ggyy-*.jar ] ; then
echo 'zzz '
fi

for fn in $filenames
do
echo $fn
if [ $fn != "ggyy-*.jar" ] ; then
echo 'zzz '$fn
fi
done

echo 'zzzzzzz\n'

for fn in $filenames
do
if [[ "$fn" =~ ggyy-(.*).jar ]] ; then
echo 'zzz '$fn
fi
done

最後這個寫法才是正確的。
另外也可以在ls那邊就解決問題,
使用,

ls -a | grep -v -e "ggyy\*.jar"
在ls階段就解決這個問題

ref
bash regex =~ case insensetive, possible? - The UNIX and Linux Forums
http://www.unix.com/shell-programming-scripting/110312-bash-regex-case-insensetive-possible.html
Bash in-process regular expressions
http://aplawrence.com/Linux/bash-regex.html


arrow
arrow
    文章標籤
    shell if file exist
    全站熱搜
    創作者介紹
    創作者 peicheng 的頭像
    peicheng

    FLASHC

    peicheng 發表在 痞客邦 留言(0) 人氣()