wim*_*wim 7 math shell busybox dash-shell
我正在编写一个shell脚本,它可以在我的本地上工作/bin/sh(在Ubuntu 13.04上冲刺),但我非常需要在一个愚蠢的盒子上运行它,因为对变量的操作我得到一个错误:
$((n2 - n1 + 1))
Run Code Online (Sandbox Code Playgroud)
不起作用,我收到如下错误:
syntax error: you disabled math support for $((arith)) syntax
Run Code Online (Sandbox Code Playgroud)
我不太了解sh那里,但我认为这件事是忙碌的.我怎么能在这个哑壳上做数学?
编辑小程序列表
~ # busybox --list
[
arp
ash
cat
chgrp
chmod
chown
chroot
chvt
clear
cmp
cp
cut
date
dd
deallocvt
df
dmesg
du
echo
env
false
find
freeramdisk
ftpget
ftpput
grep
gunzip
gzip
hexdump
hwclock
ifconfig
ln
losetup
ls
md5sum
mkdir
mkfifo
mknod
mkswap
more
mount
mv
nslookup
ping
ping6
ps
pwd
renice
reset
rm
rmdir
route
seq
sh
sha1sum
sha256sum
sleep
sort
swapoff
swapon
switch_root
sync
tar
taskset
tee
telnet
test
tftp
time
top
touch
true
umount
uname
uniq
uptime
usleep
vconfig
vi
wget
whoami
yes
Run Code Online (Sandbox Code Playgroud)
针对您的问题的另一个具体解决方案 ( n2 - n1 + 1) 基于seq、sort -nr和uniq -u(符合 POSIX 标准)。
foo()
{
{
seq 1 "$2"
seq 0 "$1"
} \
| sort -n \
| uniq -u \
| grep -n "" \
| sort -nr \
| { read num; echo "${num%:*}"; }
}
$ foo 100 2000
1901
Run Code Online (Sandbox Code Playgroud)