Linux 需要高效且可能是一到两个衬垫解决方案来替换空间

Linux 需要高效且可能是一到两个衬垫解决方案来替换空间,linux,bash,Linux,Bash,使用以下bash脚本从命令列表中查找命令的索引 #!/bin/bash cmdlist="cmd1,cmd2,cmd,cmd24,cmd25,cmd4,cmd10,cmd9,cmd000001,cmdxyz" cmdlist="${cmdlist//,/ }" #To Replace , with space in array list cmdlist="${cmdlist//cmd/ }" #To get index of command echo $cmdlist //Added

使用以下bash脚本从命令列表中查找命令的索引

 #!/bin/bash
cmdlist="cmd1,cmd2,cmd,cmd24,cmd25,cmd4,cmd10,cmd9,cmd000001,cmdxyz"
cmdlist="${cmdlist//,/ }" #To Replace , with space in array list    
cmdlist="${cmdlist//cmd/ }" #To get index of command

echo $cmdlist //Added for clarification

for  a in $cmdlist
do  
    if [[ $a == *[^[:digit:]]* ]] || [[ $a -gt 50 ]] 
    then
        printf "invalid command index : %s\n" $a
    else
        printf "command index : %s\n" $a
    fi
done
正如您所看到的,我是如何从命令列表中提取命令索引的,但在某些情况下它失败了(当然失败了)。因此,我们需要进行如下验证:

1) 在列表中,若参数是
cmd
,那个么它将被跳过,而不是替换空格,因为参数长度必须大于3

2) 在列表中,若参数为
cmd0001
,则也将跳过,而不是替换空格,因为参数长度必须小于或等于5,大于3

在上面的验证之后,我通过为..loop获取
,获取临时数组,然后比较每个参数并进行验证,然后存储在临时数组中,最后将临时数组复制到原始数组中来实现。所以这个过程太长了

有人知道面糊的解决方案吗

cmdlist=“${cmdlist//cmd/}”#获取命令的索引

如果条件
[[length-gt 3]]&&[[length-le 5]]
匹配,则命令仅替换目标参数中的空格

注意:我们已经有了使用
for..loop
的解决方案

更新:为我想要的内容添加了更多细节

我从脚本中得到了输出

command index : 1
command index : 2
command index : 24
command index : 25
command index : 4
command index : 10
command index : 9
command index : 000001
invalid command index : xyz
但是我想要这个

command index : 1
command index : 2
invalid command index : cmd
command index : 24
command index : 25
command index : 4
command index : 10
command index : 9
command index : cmd000001
invalid command index : cmdxyz
因此,基本上保留不在验证范围内的参数,并将其标记为无效索引(无需用空格代替
cmd
string)

更多更新:再次添加更多细节,以明确我想要什么

在我的脚本(请参阅上面修改的脚本)中的
for..loop
之前添加了一个
echo
语句,它给我这样的输出

1 2 24 25 4 10 9 000001 xyz
但是我想要

1 2 cmd 24 25 4 10 9 cmd000001 cmdxyz
表示保留参数,如果它违反了验证(如我的列表中所示)。第三个参数是
cmd
。它违反了条件,因为它的长度不大于
3
。现在查看列表中的最后两个参数
cmd000001,cmdxyz
,它违反了条件,因为它的长度大于
5
。有效参数的长度必须大于
3
&&
小于或等于
5


希望这能澄清我的想法。

根据您的更新进行编辑:

获取数组中的值,并在循环中检查这些值是否符合要求的条件:

cmdlist="cmd1,cmd2,cmd,cmd24,cmd25,cmd4,cmd10,cmd9,cmd000001,cmdxyz"
IFS=, read -a arr <<< "$cmdlist"

for a in "${arr[@]}"
do
    v="${a/cmd/}"
    if ((v > 50)) || ((v <= 0))
    then
        printf "invalid command index : %s\n" $a
    else
        printf "command index : %s\n" $v
    fi
done

旧答案:

不要试图用空格等替换
,而是将逗号分隔的字符串读入数组。操纵数组以获取包含所需字符串的数组

$ IFS=, read -a arr <<< "$cmdlist"
$ foo=(${arr[@]/cmd/})
$ for i in "${foo[@]}"; do echo $i; done
1
2
24
25
4
10
9
000001
xyz
$
产生:

command index : 1
command index : 2
command index : 24
command index : 25
command index : 4
command index : 10
command index : 9
command index : 000001
invalid command index : xyz

注意:带前导零的数字(如示例中所示)将被视为八进制,并可能产生意外结果:


+1,但是请注意,您可以在
bash
中强制使用base10,方法是在数字前面加上
10#
[[10#0024-gt 22]]和&echo more | | echo small more
=>
morer
。此外,单括号表示法也不能解释为八进制:
[0024-gt 22]&&echo morer | | echo minger morer
=>
morer
。可能是您没有正确理解我。请看我编辑的问题。您尝试的内容我得到了确切的结果,但它违反了验证。您的输出中的
命令索引:000001
?而且不仅仅是
print
目的,而且
cmdlist
实际上也被修改了。@Jayesh考虑到编辑,我对这个问题理解得不够透彻。我想这会给你足够的时间来做一些小的修改来满足你的要求。@devnull谢谢你的尝试…很抱歉你还没有澄清我的问题,让我再次编辑这个问题。当然它会重定向你。
for a in "${foo[@]}"
do  
    if [[ $a == *[^[:digit:]]* ]] || [[ $a -gt 50 ]] 
    then
        printf "invalid command index : %s\n" $a
    else
        printf "command index : %s\n" $a
    fi
done
command index : 1
command index : 2
command index : 24
command index : 25
command index : 4
command index : 10
command index : 9
command index : 000001
invalid command index : xyz
$ [[ 0024 -gt 22 ]] && echo greater || echo smaller
smaller