1

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

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

I am using korn shell. I dont want any hidden files also. Do i need to put the above two lines in the begining of shell script
you set them before your script
0
for fspec in `dir $FILESRAW` ; do

Comments

0

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"

1 Comment

using just ls would leave out hidden files.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.