Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在bash中修改子shell循环中的全局变量_Bash_Shell_Loops_Scripting_Subshell - Fatal编程技术网

在bash中修改子shell循环中的全局变量

在bash中修改子shell循环中的全局变量,bash,shell,loops,scripting,subshell,Bash,Shell,Loops,Scripting,Subshell,我的目标是测量当前系统负载的百分比,然后将LED闪烁该百分比一秒钟。为此,我有一个函数“get_load”,它在一个while循环中测量一秒钟的平均负载,并将其作为类似于get_load&的子shell运行。get_load函数还在每次迭代后定义一个全局LASTLOAD变量。之后,我有一个翻转led功能,它打开led,休眠1-LASTLOAD秒,关闭它,然后休眠1-LASTLOAD秒(总计应为1) 问题是,全局LASTLOAD变量似乎从未更新,或者至少没有传播到flip_led循环中 我的问题是

我的目标是测量当前系统负载的百分比,然后将LED闪烁该百分比一秒钟。为此,我有一个函数“get_load”,它在一个while循环中测量一秒钟的平均负载,并将其作为类似于
get_load&
的子shell运行。get_load函数还在每次迭代后定义一个全局LASTLOAD变量。之后,我有一个翻转led功能,它打开led,休眠1-LASTLOAD秒,关闭它,然后休眠1-LASTLOAD秒(总计应为1)

问题是,全局LASTLOAD变量似乎从未更新,或者至少没有传播到flip_led循环中

我的问题是如何在子shell中运行循环,但子shell是否会影响初始脚本中的全局变量

这是剧本本身

readonly LEDTRIGGER="/sys/class/leds/led0/trigger"
readonly LEDBRIGHTNESS="/sys/class/leds/led0/brightness"

LASTLOAD=0
ISRUNNING=1

function get_load() {
    # Define all local variables used in function
    local user1 sys1 idle1 user2 sys2 idle2 u s i load

    while [[ $ISRUNNING -eq 1 ]]; do
        # Get initial usage
        read -r user1 sys1 idle1 \
            <<<"$(grep 'cpu ' /proc/stat | awk '{print $2" "$4" "$5}')"
        sleep 1
        # Get usage after a second has passed
        read -r user2 sys2 idle2 \
            <<<"$(grep 'cpu ' /proc/stat | awk '{print $2" "$4" "$5}')"

        u=$(echo "scale=4;$user2-$user1" | bc)
        s=$(echo "scale=4;$sys2-$sys1" | bc)
        i=$(echo "scale=4;$idle2-$idle1" | bc)

        # Return the average usage over the last second rounded
        load=$(echo "scale=4;($u+$s)*100/($u+$s+$i)" | $BC)
        # loadint=$(echo "($load+0.5)/1" | $BC)
        percent=$(echo "scale=4;$load/100" | $BC)

        # echo "load: $load, loadint: $loadint, percentage: $percent"
        # return "$loadint"
        # echo "$percent"
        LASTLOAD=$percent
        echo "$LASTLOAD"
    done
}

function flip_led() {
    while [[ $ISRUNNING -eq 1 ]]; do
        local remainder
        remainder=$(echo "scale=2;1-$LASTLOAD" | $BC)
        echo 1 | sudo tee $LEDBRIGHTNESS >/dev/null
        echo "led on for $LASTLOAD, off for $remainder"
        $SLEEP "$LASTLOAD"
        echo 0 | sudo tee $LEDBRIGHTNESS >/dev/null
        $SLEEP "$remainder"
    done
}

echo none | sudo tee $LEDTRIGGER >/dev/null
echo 0 | sudo tee $LEDBRIGHTNESS >/dev/null

get_load &
flip_led &

readonly LEDTRIGGER=“/sys/class/led/led0/trigger”
只读LED亮度=“/sys/class/led/led0/brightness”
LASTLOAD=0
ISRUNNING=1
函数get_load(){
#定义函数中使用的所有局部变量
本地用户1 sys1 idle1用户2 sys2 idle2 u s i加载
当[[$ISRUNNING-等式1]]运行时;执行以下操作
#获得初始使用
read-r user1 sys1 idle1\

请显示您的脚本。@MarkSetchell已经完成了,对不起!实际上所有的shell变量都是全局的,在术语的正常意义上:对同一进程中的任何函数都可见。您询问的是进程间通信,这完全是另一回事。
如何在子shell中运行循环,但子shell会影响全局变量在初始脚本中?
通过进程间通信的任何方式。哦,这正是我想要的,谢谢你为我指明了正确的方向!什么是将此标记为已解决的最佳方式?