我想要一个 Unix 脚本,它将从用户那里获取多个列号并反转内容。
declare -a param="$@"
# enter 0 when exit the insert element
echo "Enter the numbers"
read n
while [ $n -ne 0 ]
do
x[$i]=`expr $n`
read n
let i++
done
#display the all array elements
echo "Array values ${x[@]}"
echo "Array values ${x[*]}"
# To find the array length
length=${#x[*]}
echo $length
Run Code Online (Sandbox Code Playgroud)
复制并粘贴我对堆栈溢出的一个极其相似的问题的回答,该回答与上面 Gnouc 的一个极其相似的回答一起发布...
_arr+=( '"${_arrev} is an actual "${array[@]}"' )
_arr+=( '"${_arrev} is created as a result"' )
_arr+=( '"of reversing the key order in"' )
_arr+=( '"this "${_arr}. It handles zsh and"' )
_arr+=( '"bash arrays intelligently by tracking"' )
_arr+=( '"shell "$ENV." quotes=fine ( i hope ) "' )
. <<REVERSE /dev/stdin ?
_arrev=( $(: $((l=${#_arr[@]}${ZSH_VERSION++1})) ; printf '"${_arr[$(('$l'-%d))]}" ' `seq 1 $l`) )
REVERSE
echo ; printf %s\\n ${_arrev}
"shell "$ENV." quotes=fine ( i hope ) "
"bash arrays intelligently by tracking"
"this "${_arr}. It handles zsh and"
"of reversing the key order in"
"${_arrev} is created as a result"
"${_arrev} is an actual "${array[@]}"
Run Code Online (Sandbox Code Playgroud)
我认为这应该处理任何可能的数组。
如果你对上面发生的事情感兴趣,我建议你先看看这里。那么也许在这里,绝对在这里,如果你有时间,在这里和这里。
在所有这些答案中,我讨论了此处文档(以及许多其他文档)的不同方面,您可以利用这些方面的优势。例如,我讨论了两次求值变量,这是在上面完成的,其中一个声明一个函数,该函数全局声明另一个"_$1"
仅用 5 或 6 行命名的函数- 其中大部分是_$1() { func body ; }
. 如果您正确使用它,它会非常方便。
关于bash
/zsh,
井之间的自动切换是另一回事,但也很简单。见这里。
所以基本上如果你可以创建一个bash
/zsh,
数组,你应该能够只使用 3 来反转它。<<...反转行。它不需要像 afor loop
那样遍历数组。
归档时间: |
|
查看次数: |
296 次 |
最近记录: |