Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.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
String Shell脚本:测试字符串是否包含字符(包括像';*&';和';\';)_String_Bash_Shell_Sh - Fatal编程技术网

String Shell脚本:测试字符串是否包含字符(包括像';*&';和';\';)

String Shell脚本:测试字符串是否包含字符(包括像';*&';和';\';),string,bash,shell,sh,String,Bash,Shell,Sh,在shell脚本中,我有一个函数afun,它被传递了几个参数。 我需要另一个函数来帮助我找出这些参数中是否至少有一个包含一个未知的给定字符(但它可以是任何字符,如a,9,*,\,,,(,[/code>等等,但不是空格): 建议的函数应该至少与Bash、Dash、Ash和ZSH兼容,因为我有一个脚本需要在不同的Linux发行版(Ubuntu、Alpine Linux)下运行安装在Docker容器中,我不想声明对特定shell解释器的依赖性,因为并非所有这些容器都必须安装它。下面是我的bash特定解

在shell脚本中,我有一个函数
afun
,它被传递了几个参数。 我需要另一个函数来帮助我找出这些参数中是否至少有一个包含一个未知的给定字符(但它可以是任何字符,如
a
9
*
\
[/code>等等,但不是
空格
):


建议的函数应该至少与Bash、Dash、Ash和ZSH兼容,因为我有一个脚本需要在不同的Linux发行版(Ubuntu、Alpine Linux)下运行安装在Docker容器中,我不想声明对特定shell解释器的依赖性,因为并非所有这些容器都必须安装它。

下面是我的bash特定解决方案:

#!/bin/bash
fun()
{
  not_allowed=' ' # Charater which is not allowed
  [[ "$1" =~ $not_allowed ]] && echo "Character which is not allowed found"
}

fun "TestWithoutSpace"
fun "Test with space"

下面是我建议的shell函数:

charexists() {
  char="$1"; shift
  case "$*" in *"$char"*) return;; esac; return 1
}
以下是您如何使用它:

afun() {
  # Some commands here...
  testchar=... # here I have some logic which decides what character should be tested below
  # Now, call another function to test if any of the args to "afun"
  # contain the character in var "testchar".
  # If it does, print "Found character '$testchar' !"
  charexists "$testchar" "$@" && echo "Found character '$testchar' !"
}
下面是一个简单的单元测试:

fun2test=charexists
{ $fun2test '*' 'a*b' && printf 1 ;} ; \
{ $fun2test '*' 'a' '*' '\' 'b#c|+' '\' && printf 2 ;} ;\
{ $fun2test '\' 'a' '*' '\' 'b#c|+' '\' && printf 3 ;} ;\
{ $fun2test '*' 'ab' || printf 4 ;} ; \
{ $fun2test '*' 'a' '' '/' 'b#c|+' '\' || printf 5 ;}; echo
如果所有5项测试均通过,则应打印
12345


我刚刚在Bash、Dash、Ash和ZSH下进行了测试,一切都进行得很顺利。

Bash、Dash、Ash和ZSH。这听起来像是在开玩笑。;)问题还不是很清楚。ZSH比Ash有更多的功能,但我问的是不是不合理?谢谢,我已经修复了变量名(它应该是
testchar
而不是
c
)这不是不合理的,但应该是非常困难的,IMHO.(@Elifarley,这不仅仅是“更多功能”--ash、bash和ksh都符合POSIX sh(除非bash支持
echo-e
),而zsh则故意与标准不兼容(在一些地方,其维护人员决定——事实上有一些依据——该标准强制执行糟糕的设计决策),除非在非默认posix模式下运行。这并不是说编写与zsh和posix兼容的shell兼容的脚本是不可能的,但这确实意味着它需要比通常的(ash+bash+ksh)更谨慎一些POSIX没有定义仅仅遵守POSIX.BTW,
local
的实践——如果您想要实现最大的兼容性,您将不需要。使用在我的解决方案中找到的测试用例,您建议的解决方案只打印
34
,因此它无法通过前两个测试。
charexists“$testchar”$*
--如果您的参数列表中包含,例如,
*
,则如果没有引用的
$*
,将表现不好--除非您希望将其扩展为当前目录中的文件名列表。确实!修复了,谢谢!
fun2test=charexists
{ $fun2test '*' 'a*b' && printf 1 ;} ; \
{ $fun2test '*' 'a' '*' '\' 'b#c|+' '\' && printf 2 ;} ;\
{ $fun2test '\' 'a' '*' '\' 'b#c|+' '\' && printf 3 ;} ;\
{ $fun2test '*' 'ab' || printf 4 ;} ; \
{ $fun2test '*' 'a' '' '/' 'b#c|+' '\' || printf 5 ;}; echo