bash:特殊字符检测(&'''“〜/ | [] $ *;)通过if; then

时间:2015-09-18 14:55:08

标签: bash character detection

在这个问题上破坏了我好几天,我已经尝试了很多解决方案。每种方法都在某种程度上起作用(在此处搜索stackoverflow),但每种方法都无法获取大多数特殊字符。 (而且我真的不喜欢`'“[{如果检查的字符)

基本上我想检测其中一个字符是否在文件名中(&`'''〜/ | \ [$ $;;)如果用户选择了一个文件并且我将进行错误对话和在代码中退出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?

3 个答案:

答案 0 :(得分:0)

尝试类似:

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

答案 1 :(得分:0)

你很亲密;你只需要逃避shell将特别处理的某些字符。除非您打算匹配空格字符,否则必须省略这些空格。

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

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

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

如果您只是想逃避模式和正则表达式元字符,则可以将%q格式与printf一起使用。

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

答案 2 :(得分:0)

谢谢大家,但除了将其限制为a-Z和0-9之外什么也没做到目前为止,但是我必须走另一条路并允许特定角色。

如果有人想要,我会在这里发布答案。

我早上已经尝试了几件事(嘿,我永远无法解决像夜间编码这样的问题)并且有所作为。

这是:

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