I'm writing a bash script that requires several pieces of user input, primarily directory paths and file names. The program will create the specified directories and files.
When writing the directory path, I request the absolute path - ~/DIR/..., or $PATH/DIR/... or ./DIR/... are not allowed. Directory paths should only be provided in the form of /DIR/DIR/..., file paths in the form /DIR/DIR/.../filename, and file names in the form of filename. I also want to assure the user has not added any command-line options (for example, if I run sudo, I want to assure that the user has not appended -u anywhere in their statement) to their input.
However, I'm not entirely certain of the best way to sanitize their input, since directory paths and filenames may contain dashes, underscores, spaces, periods, and alphanumeric characters. Any other character should be detected.
User input is gathered through read. I'm using an Ubuntu system, so as long as it complies with Ubuntu shell then it'll work. Assume that only the default system packages are installed.
Multiple options are acceptable to handle individual sections of validation (e.g. using -- to treat following data as a parameter and not a command [suggested by Siguza], and then another option for handling directory paths).
Absolute paths are required for correct mapping of directory structure. Any other format of path would cause the program to incorrectly interpret the structure when matched or appended to other paths, due to the functions I'm performing on the strings of paths.
Thank you.
What worked: I used the accepted answer, using realpath in some situations and readlink, and using -- to interpret the following text as parameters. If you want to allow relative paths, then use the result of readlink or realpath as your path. If you want to disallow relative paths, then compare the original string to the result of readlink or realpath.
--option, which makes them treat all following parameters as arguments, and not options/flags. Apart from that, enclose all variables in double quotes to avoid expansion.fooutility" might be answered with, "oh, there's a specialcheck_path_for_fooutility that can do that safety-check for you.)mkdir,cd, andcpdon't have any restrictions on the filenames they accept; what made you think they did? Re: "I am matching them against static full paths": What kind of comparison do you need? Note that two paths can both be absolute, and be completely different, yet point to the same file. So, please put aside your mistaken scruples, and edit your question to add the relevant details about what you actually need.while readis thus quite buggy: Filenames with backslash literals have them stripped (unless doubled up); trailing whitespace is removed by thereadcommand itself if IFS isn't cleared; and if an entry does end in a backslash,readtakes it to mean that the line after it is part of the same entry!