Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am working on a Cygwin Bash shell script which amongst other things, reads the names of some directories and files from an input data file. I want the script to be able to flag whether the data read is a valid Unix filename, or valid Unix path, as appropriate.
I am aware that the test command can be used to check that a specified string is the name of a file which exists, a directory which exists, etc. But, in this case, the files and directories will often not exist. Does anyone know of a command or utility which indicates whether an input parameter is a valid filename or directory name?

What's your definition of a valid Unix file name or Unix path if it doesn't exist? The choices are limitless.
The following does the trick if the object in data.file does exist:
#!/bin/ksh
while read line
do
[ -f $line ] && echo "it's a file"
[ -d $line ] && echo "it's a directory"
done data.file
# there's a less than sign between done and data.fileRegards,
Nails

I guess if the file/dir didn't already exist, you could always create an empty file using that name and then delete it.

Thanks for the suggestions. Re: the question about what is a valid filename, I can say that some filenames considered INvalid for the purposes of this script would be anything that cannot be created, e.g.:
this?would*be*invalid
So I could do a touch on the filename/dirname and see if that returns an error code. And/Or I could simply search the string for special characters. I was just wondering if Unix provided a dedicated utility to do this, but perhaps not.

Unfortunately you CAN create a file named this?would*be*invalid. Maybe what you need is a test for undesirable filenames, e.g. those containing unprintable or wildcard characters or multiple blanks, or more than say 30 characters long.

Yes - the function to do this is going to use pathchk first to confirm Unix isn't unhappy with the filename. This will pretty much be a formality, as Unix allows just about anything. After passing this check, the function will 'manually' look for any 'special' characters, such as wildcards, shell redirection characters, etc. It seems surprising that there is no built-in utility for this...if anyone from the Free Software Foundation reads this posting, I would like to suggest it could be a very handy validation utility for future releases of XFree86.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |