gpt4 book ai didi

linux - 如何打印数组的所有偶数位置?

转载 作者:太空宇宙 更新时间:2023-11-04 06:06:09 24 4
gpt4 key购买 nike

我想用 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 "...

最佳答案

获取请求结果的解决方案

命名脚本: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

关于linux - 如何打印数组的所有偶数位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30323121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com