1

I am trying to loop through a set of files in a directory and do certain activities on them. When i execute the script with ksh it works as expected. But when i execute it with ./ it fails with error saying too many arguments. Please help with this.

ScriptName : Script1

When exec with ksh Script1 argument1 it is working as expected. But when exec'd with ./Script1 argument1 is throwing too many arguments error.

#!/bin/bash
cd ../SrcFiles

if [ -e *$1* ]
then
for file in *$1*
do
echo $file
if [ $1 == "XXX" ]
then
echo $file >> ABC_"$1".txt
elif [ $1 == "YYY" ]
then
echo $file >>  ABC_"$1".txt
else
echo $file >>  ABC_"$1".txt
fi
done
else
#createadummyfile
fi

1 Answers1

1

Here:

if [ -e *$1* ]

-e expects a single argument. The glob expansion is likely finding more than one file. If you want to check if there are any files matching, use an array to hold the glob expansion:

files=(~(N)*"$1"*)
if (( ${#files[@]} == 0)); then
    # no files found
else
    # do stuff to matching files
    for file in "${files[@]}"; do ...
fi

works with ksh93. The ~(N) part instructs ksh that if no files match, do not keep the pattern as the output (similar to bash's nullglob option)

glenn jackman
  • 238,783
  • 38
  • 220
  • 352