Bash script - $(ls expr) return double file name with one finishing with '.'

Hello.

This small part of my script

#!/bin/bash
SRCDIR=/root/.kde4/share/config
for MY_FILE in  $(ls $SRCDIR/kate*) ; do
    echo "$MY_FILE"
done

give :

So for the moment I have modified my script like this :

#!/bin/bash
SRCDIR=/root/.kde4/share/config
for MY_FILE in  $(ls $SRCDIR/kate*) ; do
    if [[ "$MY_FILE" == $SRCDIR/kate* ]] ; then
        if [[ "$MY_FILE" == $SRCDIR/kate*. ]] ; then
            echo .
        else
            echo "Keep this : $MY_FILE"
        fi
    fi
done

which give

But there is surely a best method.

You can remove one if ....:slight_smile:

#!/bin/bash
SRCDIR=/root/.kde4/share/config
for MY_FILE in  $(ls $SRCDIR/kate*) ; do
    if [[ "$MY_FILE" == $SRCDIR/kate*. ]] ; then
            echo .
        else
            echo "Keep this : $MY_FILE"
        fi    
done

[LEFT]Thank you.
But my question is rather how to prevent $(ls expr) from returning file name with '.' at the end.
[/LEFT]

ok try this.. No need to use for loop also...:slight_smile:

ls $SRCDIR/kate* | grep -v "\.$"
1 Like

Great. That do the job.

don't need ls. never use for on ls.

for file in "$SRCDIR"/kate*; do
    [[ $file = *. ]] && continue
    # ...
done

or use extglob

shopt -s extglob
for file in "$SRCDIR"/kate!(*.); do
   # ...
done
1 Like