Bash script too many arg's

I have a simple bash script on RHEL that works fine when there is one file but when it finds multiple files of same condition I get too many args error.

for i in *
do
        if [ ! -f "$i" ];then
                echo "no files to move"
                exit 0
        fi
         if [ "$i" = *02.DAT ]; then
                mv $i $CD/somefolder/
        fi

Its system issue whats the output of this command

getconf ARG_MAX

Actually, I think this is the problem:

if [ "$i" = *02.DAT ]; then

*02.DAT does not do what you think it does here. If there's any filenames in the current folder matching it, it expands into a list of those names.

I believe you intended this:

if [[ "$i" == *02.DAT ]] ; then

double-equal in double brackets wildcards against $i instead of the current directory.

you guys are great. Corona your modification has it working now. Just so I can learn something what are the extra brackets and equals doing?

Just because of my interest this is what the max is set to 2621440

Another way would be:

for i in *02.DAT
do
    if [ -f "$i" ]; then
        mv -- "$i" "$CD/somefolder/"
    else
        echo "no files to move"
    fi
done

Some more info

Bash Pitfalls

[Difference between [ and [

Actualy, with the use of *02.dat one could drop the check for its existence, as it should only be listed because it exists.
Thus it could be like:

for f in *02.dat;do mv "$f" "$CD/somefolder/"|| echo "Move failed with code: $?";done

hth

Single are an old-fashioned syntax which is kept for compatibility. [] does all the same things and more. == is an extended syntax which allows glob-matching like you do.

Actualy, with the use of *02.dat one could drop the check for its existence, as it should only be listed because it exists.
Thus it could be like:

for f in *02.dat;do mv "$f" "$CD/somefolder/"|| echo "Move failed with code: $?";done

hth

thanks everyone. I'll check out the two links. I try to learn when I can and hope to help where I can. As always extremely helpful site.

In that case, the or `||' is not necessary since `mv' is going to show in stderr the failure.
Also, there's the possibility that *02.dat does not give any proper result to the for loop.