• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

写消耗的内存一定量的用户定义的时间bash shell脚本

用户头像
it1352
帮助1

问题说明

我想写消耗了用户定义的时间嵌入式设备上的内存量高一个bash shell脚本。我该怎么做它没有使用数组?

I am trying to write a bash shell script that consumes a high amount of RAM on an embedded device for a user defined time. How do I do it without using arrays ?

正确答案

#1

即使传统猛砸阵列不支持,它仍然有可能创建使用内置在特定的壳评估命令类似数组变量。

Even if traditional Bash arrays are not supported, it may still be possible to create array-like variables using the eval command built into the particular shell.

以下示例脚本是基于一些脚本我在一个嵌入式Linux项目使用 BusyBox的什么时候。 BusyBox的使用 Almquist外壳 (也称为外壳,灰,SH),它不支持数组。

The following example script is based on some scripting I did when using BusyBox in an embedded Linux project. BusyBox uses the Almquist shell (also known as A Shell, ash, and sh), which does not support arrays.

#!/bin/ash

for index in 1 2 3 4 5; do
    value=$(($index * 1024))
    eval array$index=\"array[$index]: $value\"
done

for i in 1 3 5; do
    eval echo \$array$i
done

<子>注意使用评估

输出:

array[1]: 1024
array[3]: 3072
array[5]: 5120

根据特定的情况下,类似于以下就足够了脚本。


Depending on your particular scenario, a script similar to the following may suffice.

#!/bin/ash

echo "Provide sleep time in the form of NUMBER[SUFFIX]"
echo "   SUFFIX may be 's' for seconds (default), 'm' for minutes,"
echo "   'h' for hours, or 'd' for days."
read -p "> " delay

echo "begin allocating memory..."
for index in $(seq 1000); do
    value=$(seq -w -s '' $index $(($index   100000)))
    eval array$index=$value
done
echo "...end allocating memory"

echo "sleeping for $delay"
sleep $delay

在我简短的测试,这个脚本消耗〜570M到575M〜物理内存 * 进行5分钟的指定时间段。

In my brief testing, this script consumed ~570M to ~575M physical memory* for the specified time period of 5 minutes.

*在不同的测试中使用监控顶部和memprof节目

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcckekk
系列文章
更多 icon
同类精品
更多 icon
继续加载