How to check that passed parameters all have the same extension?

$ ls
monkey.txt
banana.csv
tree.txt
$ myscript monkey.txt tree.txt
All extensions ARE alike.
$ myscript *txt
All extensions ARE alike.
$ myscript monkey.txt banana.csv
All extensions are NOT alike.
$ myscript *
All extensions are NOT alike.

My brain has given up; what's the simplest shell script needed to produce the above output, i.e. iterate through positional parameters, comparing their file extensions for similarity?

I know it's wrong, but here's my thought process:

if [ $# -gt 1 ]; then
        for (( i=1; i<$#; i++ )); do
                if [ "$i##*.}" != "$i+1##*.}" ]; then
                        echo "All extensions are NOT alike."
                        exit 1
                fi
        done
fi
echo "All extensions ARE alike."

Thanks in advance...

---------- Post updated at 01:21 PM ---------- Previous update was at 12:00 PM ----------

Nearly there! Now I just need it to work when files are without an extension, too (currently this would say they are NOT alike):

if [ $# -gt 1 ]; then
        for (( i=1; i<$#; i++ )); do
                j=`expr $i + 1`
                if eval [ '${'${i}'##*.}' != '${'${j}'##*.}' ]; then
                        echo "All extensions are NOT alike."
                        exit 1
                fi
        done
fi
echo "All extensions ARE alike."

---------- Post updated at 01:50 PM ---------- Previous update was at 01:21 PM ----------

Huzzah! Messy, but functional:

if [ $# -gt 1 ]; then
        for (( i=1; i<$#; i++ )); do
                if [ "$(eval echo '${'$i'}' | sed 's/^[^\.]*//')" != "$(eval echo '${'$((i+1))'}' | sed 's/^[^\.]*//')" ]; then
                        echo "All extensions are NOT alike."
                        exit 1
                fi
        done
fi
echo "All extensions ARE alike."
#!bin/ksh or bash or ...
[ "$#" -lt 1 ] && echo "need args" >&2 && exit 1

IFS="."
token=($1)
cnt=${#token[*]}
((last=cnt-1))
shift
while [ $# -gt 0 ]
do
        mytoken=($1)
        mycnt=${#mytoken[*]}
        ((mylast=mycnt-1))
        ((cnt==1 && mycnt != 1)) && echo "All extensions are NOT alike." && exit 2
        [ "${mytoken[$mylast]}" != "${token[$last]}" ] && echo "All extensions are NOT alike." && exit 3
        shift
done

echo "All extensions ARE alike"

you can try for extension check

#!/bin/sh
for i in `echo $*`
do
echo $i |awk -F. '{print $2}'|sort >>text
done
cat text|uniq -c |awk 'END{ {if(NR > 1)print "diff"
else print "same"}}'
rm -f text

if u want to check for the file name replace $2 with $1.
This script can me made shorter if we use array and count the var also..:confused: .. for better result.

Thanks both.

Posix - your suggestion prompted the following which is much easier to follow than my previous attempt(s) - thanks!

if [ `for file in $@; do
                echo ${file} | sed 's/^[^\.]*//'
        done | uniq -c | wc -l` -gt 1 ]; then
                echo "All extensions are NOT alike."
                exit 1
        else
                echo "All extensions ARE alike."
fi

Are you tested example
1.txt 1.1.txt 2.txt
All those have same extension.

Good point, kshji, but we use multiple "extensions" and hence require the filename to be stripped at the first "."

For your example, a slight alteration would suffice: :slight_smile:

if [ `for file in $@; do
                echo ${file##*.}
        done | uniq -c | wc -l` -gt 1 ]; then
                echo "All extensions are NOT alike."
                exit 1
        else
                echo "All extensions ARE alike."
fi

The following is a sh solution that should be portable to most reasonably posix-compliant shells, and which should use nothing but sh built-ins to do its job.

With 0 or 1 argument, the result is always a match. With two or more, all extensions must match. The extension for "file.1.2" is ".1.2" and not ".2". All filenames with absent extensions match each other (which is the same semantic as that used by the echo|sed|uniq|wc pipeline in your earlier solution).

e=extension of current filename
set_e=function to set e
ext=extension which all filenames must match
f=current filename

#!/bin/sh

set_e() {
    e=${1#*.}
    [ "$e" = "$1" ] && e=''
}

set_e "$1"
ext=$e

for f; do
    set_e "$f"
    [ "$ext" = "$e" ] || exit 1
done
exit 0

You can then use the exit status to echo or proceed accordingly (or you can just insert the echo statements into the code, if you like).

Regards,
Alister

Here is my code, in sh

#!/bin/sh

x=${1##*.}
#for i in $*; do
#Modified per alister's tip
for i; do
    [ $x != ${i##*.} ] && {
        echo "All extensions are NOT alike."
        exit 1
    }
done
echo "All extensions ARE alike."

exit 0

That will break if there are any filenames with IFS characters (space/tab/newline by default). Instead, use one of the following:

for i; do

or

for i in "$@"; do

Regards,
Alister

Thanks for the tip :wink:

A warning about shift, I think Alister first mentioned it:

If you shift through all of your positional parameters, they are all gone.
From bash documentation:

You can use bracket notation to step thru the positional parameters without destroying them. one way:

while [[ $i -le $# ]]
do
 echo \$${i}
 i=$(( $i + 1 ))
done

Good point, jim. I've edited my solution a few times. First iteration shifted once. Then it did not shift. Then the shift creeped back in. Now it's back out. I wasn't considering that this could be part of a larger script and viewed the shift as inconsequential.

Alister

---------- Post updated at 02:58 PM ---------- Previous update was at 02:39 PM ----------

Unless that's a newer shell feature with which I am not familiar, I think what you're attempting requires some eval magic. Perhaps something like:

while [[ $((++i)) -le $# ]]
do
    eval echo \${$i}
done