2016-09-26 64 views
-1

我有一个数组是这样的:检查数组内容

declare -A ignoreList='(
     [21]="1" 
     [22]="1" 
     [25]="1" 
     [53]="1" 
     [80]="1" 
     [143]="1" 
     [587]="1" 
     [990]="1" 
     [993]="1" 
     [443]="1" 
     [2008]="1" 
     [10011]="1" 
     [30033]="1" 
     [41144]="1")' 

现在我要检查,如果在阵列ignoreList变量$ SSH_PORT。

的变量$ SSH_PORT是这样的: SSH_PORT = “443”

如果我试试这个:

if [[ -v ignoreList[$SSH_PORT] ]]; then 
        echo "$SSH_PORT is known" 
        exit 1 
       else 
        echo "$SSH_PORT is not known" 
        exit 1 
       fi 

我得到这个终端:

443 is not known 

但端口443是众所周知。

所以我尽量 SSH_PORT = “500”(不知道)

并运行该脚本:

500 is not known 

他DIT比不上

如果我试试这个:

if [[ ${ignoreList[$SSH_PORT]} == 1 ]] ; then 
echo In the list. 

fi

它也没有工作

我的Bash的版本是4.3

〜#的bash --version GNU的bash,版本42年3月4日(1)-release(x86_64的未知-Linux的GNU) 版权所有(C)2013 Free Software Foundation,Inc. 许可证GPLv3 +:GNU GPL版本3或更高版本http://gnu.org/licenses/gpl.html

这是免费软件;你可以自由改变和重新分配它。 在法律允许的范围内,不存在任何担保。

+1

什么在'$ SSH_PORT'? – 123

+0

这是一个变种包含至极:SSH_PORT = “500” –

回答

0

对于关联数组。

if [[ ${ignoreList[$SSH_PORT]} == 1 ]] ; then 
    echo In the list. 
fi 

对于数组:

found=0 
for ignore in "${ignoreList2[@]}" ; do 
    if [[ $ignore == $SSH_PORT ]] ; then 
     found=1 
     break 
    fi 
done 
if ((found)) ; then 
    echo In the list. 
fi 

测试脚本:

#!/bin/bash 
declare -A ignoreList='(
     [21]="1" 
     [22]="1" 
     [25]="1" 
     [53]="1" 
     [80]="1" 
     [143]="1" 
     [587]="1" 
     [990]="1" 
     [993]="1" 
     [443]="1" 
     [2008]="1" 
     [10011]="1" 
     [30033]="1" 
     [41144]="1")' 

ignoreList2=(21 22 25 53 80 143 587 990 993 443 2008 10011 30033 41144) 

for SSH_PORT in 22 23 ; do 
    echo $SSH_PORT 
    if [[ ${ignoreList[$SSH_PORT]} == 1 ]] ; then 
     echo In the list. 
    else 
     echo Not in the list. 
    fi 

    found=0 
    for ignore in "${ignoreList2[@]}" ; do 
     if [[ $ignore == $SSH_PORT ]] ; then 
      found=1 
      break 
     fi 
    done 
    if ((found)) ; then 
     echo In the list. 
    else 
     echo Not in the list. 
    fi 
done 
+0

出毛病 [代码] 声明-A ignoreList ='( \t \t [21] = “1” \t \t [22] = “1” \t \t [25] = “1” \t \t [53] = “1” \t \t [80] = “1” \t \t [143] = “1” \t \t [587] = “1” \t \t [990] =“1” \t \t [993] = “1” \t \t [443] = “1” \t \t [2008] = “1” \t \t [10011] = “1” \t \t [30033] = “1” \t \t [41144] =“1”)' if [[$ {ignoreList [$ SSH_PORT]} == 1]];然后 \t \t \t \t \t \t echo“$ {error} SSH端口$ SSH_PORT不可用,请选择另一个! | AWK '{打印的strftime( “[%H:%M:%S] |”),$ 0}' \t \t \t \t \t出口1 \t \t \t \t \t别的 \t \t \t \t \t回声 “是?” \t \t \t \t \t出口1 \t \t \t \t \t \t \t \t \t音响[/代码] –

+0

@ Dr.JoeBeer:'$ SSH_PORT'是空的。 – choroba

+0

如果我打印var $ SSH_PORT我​​得到的价值。如果我输入SSH_PORT =“500”,我可以回显$ SSH_PORT并获得500,如果我tye 443我得到443 –

0

开始在bash 4.3,你可以使用-v操作。

$ [[ -v ignoreList[21] ]] && echo present 
present 
$ [[ -v ignoreList[23] ]] && echo present 

随着SSH_PORT一个值,你可以使用

if [[ -v ignoreList[$SSH_PORT] ]]; then 
    echo "$SSH_PORT is known" 
else 
    echo "$SSH_PORT is not known" 
fi 
+0

如果我使用这个,我可以使用: if [$ {SSH_PORT} =='21'] || [$ {SSH_PORT} =='25'] || [$ {SSH_PORT} =='53'] || [$ {SSH_PORT} =='80'] || [$ {SSH_PORT} =='143'] || [$ {SSH_PORT} =='443'] || [$ {SSH_PORT} =='587'] || [$ {SSH_PORT} =='990'] || [$ {SSH_PORT} =='993'] || [$ {SSH_PORT} -gt 1024] || [$ {SSH_PORT} -le 0];然后 我想使用一个数组来获得代码紧凑 –

+0

它没有工作:/我使用它,但他不检查我编辑我的问题 –

+0

你使用'bash' 4.3或更高版本? – chepner