将全局变量存储在shell脚本之外

时间:2016-02-23 06:47:29

标签: linux unix raspbian

我有一个包含以下问题的shell脚本: 每次我再次执行脚本时,变量CURRENT再次设置为0,但是如何在每次执行脚本后保存CURRENT?

#!/bin/sh
CURRENT=0
A='Knight rider'
B='Full color mood blobs'
C='Police Lights Single'
D='Police Lights Solid'
E='Rainbow mood'
F='Rainbow swirl'
G='Rainbow swirl fast'
H='Snake'
I='Strobe blue'

case $CURRENT in
    0) hyperion-remote --effect "$A"; CURRENT=$(( $CURRENT + 1 ));;
    1) hyperion-remote --effect "$B"; CURRENT=$(( $CURRENT + 1 ));;
    2) hyperion-remote --effect "$C"; CURRENT=$(( $CURRENT + 1 ));;
    3) hyperion-remote --effect "$D"; CURRENT=$(( $CURRENT + 1 ));;
    4) hyperion-remote --effect "$E"; CURRENT=$(( $CURRENT + 1 ));;
    5) hyperion-remote --effect "$F"; CURRENT=$(( $CURRENT + 1 ));;
    6) hyperion-remote --effect "$G"; CURRENT=$(( $CURRENT + 1 ));;
    7) hyperion-remote --effect "$H"; CURRENT=$(( $CURRENT + 1 ));;
    8) hyperion-remote --effect "$I"; CURRENT=0;;
esac

1 个答案:

答案 0 :(得分:0)

使用环境变量。

if [ -z "$CURRENT" ]; then
    CURRENT=0;
    export CURRENT;
else
    CURRENT=$CURRENT;
fi

不要忘记在脚本末尾更新$ CURRENT。