如何标志着POSIX SH数组? [英] How to mark an array in POSIX sh?

查看:317
本文介绍了如何标志着POSIX SH数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

而在一个shell脚本取代外部命令,我用了一个数组摆脱awk的 NF的

While replacing external commands in a shell script, I used an array to get rid of awk's NF.

现在,因为我从庆典搬到了POSIX sh的,我不能让数组标明正确的:

Now, since I moved from bash to POSIX sh, I cannot get the array marked right:

#!/bin/bash
export RANGE="0 1 4 6 8 16 24 46 53"
RANGE=($RANGE)
echo arrayelements: $((${#RANGE[@]}))
LAST=$((${#RANGE[@]}-1))
echo "Last element(replace NF): ${RANGE[$LAST]}"

# ./foo
arrayelements: 9
Last element(replace NF): 53

我使用OpenBSD的,sh和它完全相同的大小与KSH相同。
上述更改为 / bin / sh的,似乎以下不工作:

set -A "$RANGE"
set -- "$RANGE"

我怎么能实现上面的脚本 / bin / sh的? (请注意,如果你调用的bash与正常工作 - POSIX ,这不是我期待已久。)

How could I realise the above script in /bin/sh? (Note that it works fine if you invoke bash with --posix, that's not what I look for.)

推荐答案

数组是不是 POSIX的一部分 SH 规范

Arrays are not part of the POSIX sh specification.

有其他各种方式找到的最后一个项目。一对夫妇的可能性:

There are various other ways to find the last item. A couple of possibilities:

#!/bin/sh
export RANGE="0 1 4 6 8 16 24 46 53"
for LAST_ITEM in $RANGE; do true; done
echo "Last element(replace NF): $LAST_ITEM"

#!/bin/sh
export RANGE="0 1 4 6 8 16 24 46 53"
LAST_ITEM="${RANGE##* }"
echo "Last element(replace NF): $LAST_ITEM"

这篇关于如何标志着POSIX SH数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆