Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何在shell脚本变量中检测空格_Linux_Shell - Fatal编程技术网

Linux 如何在shell脚本变量中检测空格

Linux 如何在shell脚本变量中检测空格,linux,shell,Linux,Shell,e、 gstring=“test” 我希望在字符串中找到任何空格后,它应该回显错误并退出else进程。您可以使用grep作为: string="test test test" if ( echo "$string" | grep -q ' ' ); then echo 'var has space' exit 1 fi 有不止一种方法可以做到这一点;使用参数展开 你可以这样写: if [ "$string" != "${string% *}" ]; then

e、 g
string=“test”


我希望在字符串中找到任何空格后,它应该回显错误并退出else进程。

您可以使用
grep
作为:

string="test test test"
if ( echo "$string" | grep -q ' ' ); then
        echo 'var has space'
        exit 1
fi

有不止一种方法可以做到这一点;使用参数展开 你可以这样写:

if [ "$string" != "${string% *}" ]; then
     echo "$string contains one or more spaces";
fi

对于纯Bash解决方案:

function assertNoSpaces {
    if [[ "$1" != "${1/ /}" ]]
    then
        echo "YOUR ERROR MESSAGE" >&2
        exit 1
    fi
}

string1="askdjhaaskldjasd"
string2="asjkld askldja skd"

assertNoSpaces "$string1"
assertNoSpaces "$string2" # will trigger error
“${1/}”
删除输入字符串中的任何空格,如果没有空格,则与原始字符串相比应该完全相同

请注意
“${1//}”
周围的引号-这可确保考虑前导/尾随空格

要匹配多个字符,可以使用定义要匹配的模式-
“${1/[\\.]/}”

更新 更好的方法是使用进程内表达式匹配。它可能会稍微快一点,因为没有进行字符串操作

function assertNoSpaces {
    if [[ "$1" =~ '[\. ]' ]]
    then
        echo "YOUR ERROR MESSAGE" >&2
        exit 1
    fi
}
有关
=~
运算符的更多详细信息,请参阅和

该操作符是在Bash版本3中引入的,因此如果您使用的是较旧版本的Bash,请小心

更新2 关于评论中的问题:

如果用户输入,如何处理代码 比如“asd\”在双引号中的意思 …我们能处理吗

上面给出的函数应该与任何字符串一起工作,因此它将取决于如何从用户处获取输入

假设您正在使用
read
命令获取用户输入,您需要注意的一件事是,默认情况下,反斜杠被视为转义字符,因此它不会像您预期的那样工作。e、 g

read str              # user enters "abc\"
echo $str             # prints out "abc", not "abc\"
assertNoSpaces "$str" # no error since backslash not in variable
要解决此问题,请使用
-r
选项将反斜杠视为标准字符。有关详细信息,请参阅

read -r str           # user enters "abc\"
echo $str             # prints out  "abc\"
assertNoSpaces "$str" # triggers error

case
语句在此类情况下非常有用:

case "$string" in 
    *[[:space:]]*) 
        echo "argument contains a space" >&2
        exit 1
        ;; 
esac

处理前导/尾随空格

我在处理路径时遇到了一个非常类似的问题。我选择依赖shell的参数展开,而不是专门寻找一个空间。不过,它不会检测前端或后端的空间

function space_exit {
  if [ $# -gt 1 ]
  then
    echo "I cannot handle spaces." 2>&1
    exit 1
  fi
}

双括号内的
=
运算符可以匹配通配符

if [[ $string == *' '* ]]

grep:非法选项--q用法:grep-hblcnsviw模式文件@rupali:您可以将
if
语句更改为
if(echo“$string”| grep'>/dev/null);然后
看看这个:这样做有效,但不会删除开头和结尾的空格。。。如果string=“testtest”没有抛出错误对不起,我的错。固定的。引用它将保留边缘周围的空格。它有效。。你能解释一下吗?还有,用\代替空格怎么样?如果要检测\,请使用
${1/\\/}
。或者,如果您需要同时检测这两个字符或多个字符,请参阅正则表达式的更新部分。是的..我已经尝试过了,但您知道它适用于除例如string=“test\”之外的所有字符。您可以细化吗?这也不会识别开始和结束处的空格例如string=“testtest”注意,对于bash和类似的shell,在等号周围不能有空格。