Just do the (not soo fancy) but simple:
crfile() {
dir=$(dirname "$1")
mkdir -p "$dir" && touch "$1"
}
while read -r path; do
crfile "$path"
done << EOF
some.txt
./some.txt
../some.txt
.././some.txt
../other/some.txt
/some.txt
/sub/some.txt
/sub/../etc/some.txt
EOF
or if you need shortening
crfile() { mkdir -p "$(dirname "$1")" && touch "$1"; }
from the man mkdir
:
-p Create intermediate directories as required. If this option
is not specified, the full path prefix of
each operand must already exist. On the other hand, with this option specified, no error will be
reported if a directory given as an operand already exists. Intermediate directories are created with
permission bits of rwxrwxrwx (0777) as modified by the current umask, plus write and search permission
for the owner.
Using dirname
is much better as trying mangle the path using parameter substitution. read more Bash variable substitution vs dirname and basename
And yes, add the --
if you want - it is good if you expecting files or directories starting with -
...