简体   繁体   中英

Regarding Unix shell script

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

for name in "$infile"{BBSCGG_,BCT_,ACL_,ASC}*
do
  ....
done
#!/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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM