POSIX Shell Programming Challenge - Programming On Unix

Users browsing this thread: 3 Guest(s)
mort
Members
Hi,

I thought it would be fun to have a small shell programming challenge on here. The challenge is to iterate over all the files in a directory and print their path, without looking at the internet for guidance. We will then try to come up with ways in which proposed solutions are incorrect.

Here are the criteria:
  • Iterate over all the files in the directory "test" (assume it exists) and print their names
  • Include hidden files, but exclude "." and ".."
  • All legal file names must be supported, so the only characters you may assume are not in a file name are "/" and the 0 byte
  • Don't be recursive, directories should be treated as files
  • The body of the loop must be in the same process as the rest of the script
  • You may only use POSIX shell syntax, no bashisms

Here's an example:

Code:
for f in test/*; do
    echo "$f"
done

And some possible critiques:
  • The solution only considers visible files, it wouldn't see a file prefixed with "."
  • The solution doesn't work if there are no (non-hidden) files in the directory; the loop will end up printing "test/*"

Looking forwards to seeing what solutions and non-solutions y'all come up with 🤓

EDIT: The idea behind the challenge is to end up with a shell loop over all the file names in a directory; printing the file names is just an example use case. Imagine replacing "echo" with some shell function or anything else. Also, it's not important whether the file name is prefixed with the path to the directory or not, because adding or removing a path prefix is trivial and not the point of the challenge.


Messages In This Thread
POSIX Shell Programming Challenge - by mort - 12-04-2021, 10:36 AM
RE: POSIX Shell Programming Challenge - by movq - 12-04-2021, 10:49 AM
RE: POSIX Shell Programming Challenge - by s0kx - 12-04-2021, 11:47 AM
RE: POSIX Shell Programming Challenge - by s0kx - 12-04-2021, 01:31 PM
RE: POSIX Shell Programming Challenge - by jkl - 12-04-2021, 01:34 PM
RE: POSIX Shell Programming Challenge - by sth - 12-04-2021, 01:36 PM
RE: POSIX Shell Programming Challenge - by mort - 12-04-2021, 01:52 PM
RE: POSIX Shell Programming Challenge - by mort - 12-04-2021, 02:35 PM
RE: POSIX Shell Programming Challenge - by movq - 13-04-2021, 03:34 PM
RE: POSIX Shell Programming Challenge - by s0kx - 16-04-2021, 02:50 AM
RE: POSIX Shell Programming Challenge - by s0kx - 01-05-2021, 04:56 AM