0

I want to retrieve the file from the INFILE directory which are begining with the file names prefix "BBSCGG_" or "BCT_" or "ACL_" or "ASC" and do the processing inside the for loop

INFILE=/ext/test/fil1/

for infile name in file prefix

...  if [[ -f ${fspec} ]] ; then

            processing logic

     else
            processing logic
done  

how can i do it

4 Answers 4

2
for name in "$infile"{BBSCGG_,BCT_,ACL_,ASC}*
do
  ....
done
6
  • If there are no prefix files in the directory i want to display a warning. how can i do it?
    – Arav
    Mar 30, 2010 at 1:59
  • for loop will exit with failed status if no files are in the directory
    – Arav
    Mar 30, 2010 at 2:00
  • I ran the program you pasted. It's not working. The input files are in the directory but not picking
    – Arav
    Mar 30, 2010 at 2:17
  • Because there's a typo: substitute $infile with $INFILE. Mar 30, 2010 at 2:37
  • Brace expansion works in bash and csh. Apparently ghostdog74 gave a command to make it work on ksh, but I don't know enough about it to advise you on that. You can use a sentinel to detect whether or not any files were processed. Set it to 0 before the loop, and then to 1 inside the loop. If it's still 0 after the loop then no files have been processed if nullglobs (or whatever the equivalent for ksh is) is on. Mar 30, 2010 at 2:40
1

You may want to take a look at the "find" command too if subdirectories exist. Check this out first.

0
1
#!/bin/ksh

flag=0
set -o braceexpand
for file in {BBSCGG_,BCT_,ACL_,ASC_}*
do
  if [ -f "$file" ];then
     # do your stuff if there are files
     flag=1
  fi
done
if [ "$flag" -eq 0 ];then
    echo "warning. empty"
fi
4
  • If there are no prefix files in the directory i want to display a warning. how can i do it? for loop will exit with failed status if no files are in the directory
    – Arav
    Mar 30, 2010 at 2:00
  • you can use a flag or you can check the directory for files first. see edit
    – ghostdog74
    Mar 30, 2010 at 2:06
  • I ran the program you pasted. It's not working. The input files are in the directory but not picking. i did a echo $file after the do statement and i am getting the below output. Not sure why it's not working. Also If there is no files also it's going inside the folder and setting the flag. I am using ksh so i cant do the shopt command that you mentioned in earlier posT. I am not sure why you want to do cd $infile before checking files ./test.sh {BBSCGG_,BCT_,ACL_,ASC_}*
    – Arav
    Mar 30, 2010 at 2:17
  • then you make a test for file inside your loop. if no files are found with those patterns, the flag won't be set.
    – ghostdog74
    Mar 30, 2010 at 3:20
0
ls -1 $INFILE/{BBSCGG_,BCT_,ACL_,ASC}* |while read FILE; do
  # $FILE holds full pathname of each prefixed file.
  # mmk go ...
done

If you want all files in the tree under $INFILE then use find rather than ls:

find $INFILE -name BBSCGG_\* -o \
-name BCT_\* -o \
-name ACL_\* -o \
-name ASC\* |while read FILE; do
  # kthx
done
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.