bash: 特殊字符检测 ( & ` ´ ' " ~ / | \ [ ] $ * ; ) 通过 if;then

bash: special character detection ( & ` ´ ' " ~ / | \ [ ] $ * ; ) via if;then

我为此苦恼了好几天,我已经尝试了很多解决方案。每种方法都在某种程度上起作用(在 Whosebug 上搜索此处),但每种方法都无法提取大部分特殊字符。 (而且我真的不擅长 ` ' " [ { 用于 if 检查的字符)

基本上我想检测这些字符之一是否在文件名中 ( & ` ´ ' " ~ / | \ [ ] $ * ; ) 如果用户选择了一个文件,我将进行错误对话并且代码中的 exit 0。

我目前正在使用这个:

STARTUPFILE="$(/usr/bin/osascript -e "tell application \"System Events\" to activate" -e "tell application \"System Events\" to set thefile to choose file with prompt \"choose file \"" -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")")"


FILENAME=$(basename "$STARTUPFILE") # via osascript input dialogue

if [[ $FILENAME = *[( & ` ´  ' " ~ / | \ [ ] $ * ; )]* ]]; then

除了只允许 a-Z 和 0-9 之外,检测所有这些的最佳方法是什么?

试试这样的东西:

if echo "$FILENAME" | grep -qe '^[a-zA-Z0-9]*$'; then
    echo "ok"
else
    echo "no"
fi

你很亲密;您只需要转义 shell 会特别对待的某些字符。 space 必须省略,除非您还打算匹配 space 字符。

if [[ $FILENAME = *[\(\&\`´\'\"~/\|\[]$*\;\)]* ]]; then

将模式存储在变量中会更简单:

pattern=$'*[(&`´\'"~/|\[]$*;)]*'
if [[ $FILENAME = $pattern ]]; then

如果您只是想转义模式和正则表达式元字符,您也许可以将 %q 格式与 printf.

一起使用
$ some_regex='(foo|bar)*[a&]'
$ printf '%q\n' "$some_regex"
\(foo\|bar\)\*\[a\&\]

谢谢大家,但到目前为止,除了将它限制为 a-Z 和 0-9 之外没有任何其他工作,但我必须反过来并允许特定字符。

如果有人需要,我会post在这里回答。

我在早上尝试了一些东西(嘿,我永远无法解决像晚上编码这样的问题)并得到了一些工作。

是这个:

if [[ $FILENAME = *['&''#''$''['']''\/''\'':''|'';''{''}''´''`''"''*''~'"'"]* ]]; then

我不得不用 ' 转义几乎每个字符,除了在末尾用双 " " 转义 '。也正如 chepner 提到的,/ 转换为 : 在 OSX 中,因此检查它是通过 / 和 : 完成的,因此两者都在代码中。

目前只有这个方法有效。

写了一点shell用于检查一些文件名:

if [[ $FILENAME = *['&''#''$''['']''\/''\'':''|'';''{''}''´''`''"''*''~'"'"]* ]]
then
    echo FILENAME = "$FILENAME"  >> "/Users/zero/Desktop/text.txt"
    echo FILENAME does not pass character filter >> "/Users/zero/Desktop/text.txt"
    echo " " >> "/Users/zero/Desktop/text.txt"
else
    echo FILENAME = "$FILENAME"  >> "/Users/zero/Desktop/text.txt"
    echo FILENAME passes character filter >> "/Users/zero/Desktop/text.txt"
    echo " " >> "/Users/zero/Desktop/text.txt"
fi

给出以下日志输出:

FILENAME = _PI   something normal even with () quotes or - + _ .iso
FILENAME passes character filter

FILENAME = _PI  _ something.iso
FILENAME passes character filter

FILENAME = _PI  - something.iso
FILENAME passes character filter

FILENAME = _PI  ! something.iso
FILENAME passes character filter

FILENAME = _PI  ? something.iso
FILENAME passes character filter

 FILENAME = _PI  + something.iso
FILENAME passes character filter

FILENAME = _PI  # something.iso
FILENAME does not pass character filter

FILENAME = _PI ' something.iso
FILENAME does not pass character filter

FILENAME = _PI " something.iso
FILENAME does not pass character filter

FILENAME = _PI [ something.iso
FILENAME does not pass character filter

FILENAME = _PI ] something.iso
FILENAME does not pass character filter

FILENAME = _PI { something.iso
FILENAME does not pass character filter

FILENAME = _PI } something.iso
FILENAME does not pass character filter

FILENAME = _PI * something.iso
FILENAME does not pass character filter

FILENAME = _PI : something.iso
FILENAME does not pass character filter

FILENAME = _PI \ something.iso
FILENAME does not pass character filter

FILENAME = _PI & something.iso
FILENAME does not pass character filter

FILENAME = _PI ` something.iso
FILENAME does not pass character filter

FILENAME = _PI ´ something.iso
FILENAME does not pass character filter

FILENAME = _PI | something.iso
FILENAME does not pass character filter

FILENAME = _PI ~ something.iso
FILENAME does not pass character filter

FILENAME = _PI $ something.iso
FILENAME does not pass character filter