简体   繁体   中英

Regarding UNIX Shell Script

When there is no files inside the folder the below script goes inside the for loop. Not sure what i can modify so that it doesn't go inside the for loop. Also when there is no files inside the directory exit status should be success. Wrapper script checks the exit status of the below script

     FILESRAW ="/exp/test1/folder"  .
for fspec in "$FILESRAW"/* ; do
  echo "$fspec"
  if [[ -f ${fspec} ]] ; then
       ..... processing logic
  else
     ... processing logic
  fi
done

if using bash,

you can set nullglob

shopt-s nullglob

if you have hidden files,

shopt -s dotglob

with ksh,

#!/bin/ksh
set -o noglob
for file in /path/*
do
  ....
done
for fspec in `dir $FILESRAW` ; do

To exit if $FILESRAW is empty:

[ $( ls "$FILESRAW" | wc -l ) -eq 0 ] && exit 0

If this test precedes the loop, it will prevent execution from reaching the for loop if $FILESRAW is empty.

When $FILESRAW is empty, "$FILESRAW"/* expands to "/exp/test1/folder/*", as ghostdog74 points out, you can change this behavior by setting nullglob with

shopt -s nullglob

If you want hidden files, set dotglob as well:

shopt -s dotglob

Alternately, you could use ls instead of globing. This has the advantage of working with very full directories (using a pipe, you won't reach the maximum argument limit):

ls "$FILESRAW" | while read file; do
     echo "$file"

This becomes messier if you want hidden files, since you'll need to exclude . and .. to emulate globing behavior:

ls -a "$FILESRAW" | egrep -v '^(\.|\.\.)$' | while read file; do
    echo "$file"

if you are using ksh, try putting this in front of for loop so that it won't go inside it. " set -noglob " Even I have got the same problem, but I was able to resolve it by doing this.

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