views:

71

answers:

3

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
+3  A: 

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
ghostdog74
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
Arav
you set them before your script
ghostdog74
Thanks a lot for the info
Arav
A: 
for fspec in `dir $FILESRAW` ; do
Amit Kumar
Thanks a lot fro the info
Arav
A: 

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"
Patrick
using just `ls` would leave out hidden files.
ghostdog74
thanks, I've added some discussion of hidden files.
Patrick
Thanks a lot fro the info
Arav