Check if string could be a path in FS on linux and windows

Viewed 696

I'm trying to write a bash script in which i have to read a string input by user. I need this string to be allowed to be appropriate path (branch of parent directories and the target file or directory) that can be appropriately accessed on both linux and windows.

It's about directories' names and files' names. I need them to fit both linux and windows requirements. I know that using bash 4.x (maybe 4 is not determinative) in linux allows to create file with whatever name that contain whatever characters, but i can have problems accessing such file.

So far, i know that:

  • unlike linux, windows can't access file that has colon in its name
  • unlike windows, there could be problems in linux accessing file that has exclamation sign in its name
  • windows doesn't allow name that contains only spaces
  • both linux and windows doesn't allow "." and ".." names
  • windows doesn't allow name that contains only dots

etc.

Is there, say POSIX standard or some rules or something that fits both linux and windows requirements? Which characters are allowed on both and what are exceptions for any?

Also, i'm having trouble to check if a string is a path that fits. I supposed that i can use alphanumeral characters, underscore, hyphen, round brackets, tilde, spaces, dots. I also assume that the path should start with slash and not to end with slash.

I tried regexs like these and they are not working as i want them to:

[[ ! "$path" == *['!'@#\$%^\&*+]* ]]
[[ "$path" == [a-zA-z0-9_.\ \(\)~\/-]* ]]
[[ "$path" =~ ^[a-zA-z0-9_\ -]+$ ]]

I just don't get all peculiarities of bash regex.

So, what are requirements and what is the better way to verify them?

1 Answers
Related