如何打印阵列的所有偶数位置?

时间:2015-05-19 10:32:43

标签: linux bash

我想用bash launch_script_even.sh one two three four five six seven eight执行Bash脚本。它需要将one two three four five six seven eight放入数组中并仅打印two four six eight。(数组的偶数位置)

控制台中的结果:

Launch_script_even
arguments:8
two four six eight
echo "name of script is $0"
echo "arguments:$#"
echo "$@"
echo "...

1 个答案:

答案 0 :(得分:0)

获取请求结果的解决方案

名称脚本:launch_script_even

#!/bin/bash
echo "name of script is $0"
echo "arguments:$#"
args=("$@")
for (( i=1; i<=$#; i++ )); do          
    if (( i % 2 == 0 )); then
        echo ${args[$i - 1]}
    fi
done

通话:bash launch_script_even一二三四有五六七八。

结果:

name of script is launch_script_even.sh
arguments:8
two
four
six
eight