小编bla*_*net的帖子

在Bash中,您如何查看字符串是否不在数组中?

我试图这样做而不添加额外的代码,例如另一个for循环.我可以创建将字符串与数组进行比较的积极逻辑.虽然我想要负逻辑并且只打印不在数组中的值,但实质上这是为了过滤掉系统帐户.

我的目录中有文件,如下所示:

admin.user.xml 
news-lo.user.xml 
system.user.xml 
campus-lo.user.xml
welcome-lo.user.xml
Run Code Online (Sandbox Code Playgroud)

如果该文件在目录中,这是我用来做积极匹配的代码:

#!/bin/bash

accounts=(guest admin power_user developer analyst system)

for file in user/*; do

    temp=${file%.user.xml}
    account=${temp#user/}
    if [[ ${accounts[*]} =~ "$account" ]]
    then
        echo "worked $account";
    fi 
done
Run Code Online (Sandbox Code Playgroud)

在正确的方向上的任何帮助将不胜感激,谢谢.

bash shell scripting

13
推荐指数
1
解决办法
2万
查看次数

标签 统计

bash ×1

scripting ×1

shell ×1