【问题标题】:Unix script - String ValidationUnix 脚本 - 字符串验证
【发布时间】:2012-12-22 07:38:33
【问题描述】:

我对 Unix 脚本 (.ksh) 非常陌生。我必须实现一个功能来检查我的参数是否在字符串数组中显示“欢迎”,例如

{"welcome","test","exit"}

逻辑类似于Java中的String.contains

任何帮助将不胜感激。

【问题讨论】:

  • ksh 还是 bash? (你有 bash 标签)
  • @Stefanos Kalantzis 它的 ksh

标签: shell unix ksh


【解决方案1】:

你可以做这样的事情。以下是bash,需要相应更改为ksh

脚本

array=(welcome test exit)
string='welcome';
for item in ${array[*]}
do
    if [[ $string =~ .*$item.* ]]
    then
        echo "It's present!"
    fi
done

输出

It's present!

要迭代传递给 shell 脚本的参数,请使用 for with empty in,默认迭代参数,或 in '$@'

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-28
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 2011-02-21
相关资源
最近更新 更多