ksh(!93) for loop with wildcard and empty directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh(!93) for loop with wildcard and empty directory
# 1  
Old 08-11-2011
ksh(!93) for loop with wildcard and empty directory

I'm sure this is by design, but using something like
Code:
for f in dir/*
do echo $f
done


produces unexpected (to me) results if run against an empty directory. I'd have expected it to not execute the loop, but it actually calls it with f set to 'dir/*'.

Now I know that I'm trying to protect against it:
Code:
for f in dir/*
do [ -f $f ] && #do something


I've got paranoid though, that if a file is created between the for statement and the file test, the test will succeed and the loop will execute with f = 'dir/*'. This is likely to cause interesting results Smilie

The only solution I can think of is checking the value of f:
Code:
[ "$f" != "dir/*" ] && #do something


but it seems so inelegant, is there a better way?

NB Using an early version of ksh (88?) on Solaris

# 2  
Old 08-11-2011
Code:
find DIR | while read f; do
...
done

# 3  
Old 08-11-2011
That would do it Smilie

Cheers
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

Waiting for wildcard filename to exists in while loop

Hi Experts, We are developing a script which will wait for the trigger file(with datetime in the trigger file name). But the problem is when I use 'while' loop to wait for the file, it waits for the filename with wilcard in it that is wait for 'Trigger*.done' file. :eek: Below is the script ... (4 Replies)
Discussion started by: Amey Joshi
4 Replies

2. Shell Programming and Scripting

Wildcard search in if loop

Practice folder contains many files and im interested in extracting file which starts with abc* ghi* xyz* . I need to do variety of operations for different files. if file starts with xyz* then i need to move to some destination otherwise some other destination. I am not able to make wildcard... (15 Replies)
Discussion started by: kumaar1986
15 Replies

3. Shell Programming and Scripting

find command with wildcard directory

I want to look if there is any file inside a specific directory which was modified before 2 days. I wrote the find command, but the problem is there is one directory and that is a random directory generated by unix, so not sure on how to code for that on the find command. find... (5 Replies)
Discussion started by: srini0603
5 Replies

4. Shell Programming and Scripting

Change directory with wildcard in FTP server

Hi , I am looking for a command to change directory in FTP server with wildcard specified. Basically this is what i am trying. localserver# ftp remoteserver ftp> ls 41000_42000 42000_43000 ftp> cd 41* 550 CWD failed. '41*' : no such file or directory. Could anyone please let me know... (6 Replies)
Discussion started by: thavamaniraja
6 Replies

5. Shell Programming and Scripting

WildCard serach for files in a directory

Hi i have a requirement to search for all files in a directory. the files will start as file1_*,file2_*,file3_* where the wild card character is a timestamp. so on a particular day i may get files like file1_1103120042 file1_1102010345 file2_1101093423 file3_1103120042... (3 Replies)
Discussion started by: Sgiri1
3 Replies

6. Shell Programming and Scripting

check empty directory !!!

I need to check if a directory is empty using an if condition in the pseudocode below if ; then else although i looked at a few forums on this topic, I left feeling a little unclear and i could not use the command successfully what can i substitute in the if conditon above,... (2 Replies)
Discussion started by: allah_waris45
2 Replies

7. Shell Programming and Scripting

How do I tell if a directory is empty?

To see if a directory is has anything in it, I do this: if ; then # do something fi But surely there is a more easy-to-read and elegant way. Isn't there? (6 Replies)
Discussion started by: KenJackson
6 Replies

8. Shell Programming and Scripting

Setting the right directory as non-empty

Hi guys, I've been trying to get this part of my script to work for ages and now it's getting me annoyed!! So i thought a fresh group of eyes could see where i'm going wrong! The idea is getting a directory and all its files moved to a temp folder, but similar to the rm -ir command where it... (0 Replies)
Discussion started by: olimiles
0 Replies

9. UNIX for Dummies Questions & Answers

rmdir a non-empty directory.

hi, i understand that rmdir will only remove direcotry when it is empty but are there options which will also remove non-empty directories? i went to man rmdir but only find the option -p? i am on solaris. thanks (2 Replies)
Discussion started by: yls177
2 Replies

10. UNIX for Dummies Questions & Answers

Find wildcard .shtml files in wildcard directories and removing them- How's it done?

I'm trying to figure out how to build a small shell script that will find old .shtml files in every /tgp/ directory on the server and delete them if they are older than 10 days... The structure of the paths are like this: /home/domains/www.domain2.com/tgp/ /home/domains/www.domain3.com/tgp/... (1 Reply)
Discussion started by: Neko
1 Replies
Login or Register to Ask a Question