脚本中无法缓存变量的最后 3 个值

脚本中无法缓存变量的最后 3 个值

我的目标是缓存变量的值,并将其写入历史记录三次。因此:

path=value3

cache1=value3
cache2=value2
cache3=value1

----------------

path=value4

cache1=value4
cache2=value3
cache3=value2

现在来看看脚本。实际脚本有 4350 行,所以这是一个非常简化的版本。抱歉,不可能再缩短了。

path=/library/sections/1/all

a=0
while [[ $a = 0 ]]
do

#------------------------------
#caching the requests being made.
#When a new request is made:
        #[new->1]the new value goes to cache1
        #[1->2]the value of cache1 moves to cache2
        #[2->3]cache2 moves to cache3
        #[3->gone]cache3 disappears
#basically history/log of the last 3 requests.

cache1=/library/sections/1/all
cache2=
cache3=

#moving all the values one place down and adding the newest value to cache1

sed -i "s|^cache3=\S*|cache3=$cache2|" $filelocation/test.sh
sed -i "s|^cache2=\S*|cache2=$cache1|" $filelocation/test.sh
sed -i "s|^cache1=\S*|cache1=$path|" $filelocation/test.sh
#-----------------------------

#make api request and show output

GET http://$ipplexserver:32400$path?X-Plex-Token=$plexapitoken

#-------------------------------
#the read command, showing after the api output,  to switch between the movies and series output of the api.

if [[ $path = /library/sections/2/all ]]
then
        read -rp "exit | movies: " option

elif [[ $path = /library/sections/1/all ]]
then
        read -rp "exit | series: " option
fi
#--------------------------------

#--------------------------------
#depending on the option, you either exit or switch between movies and series output.

if [[ "$option" = movies ]]
then
        path=/library/sections/1/all

elif [[ "$option" = series ]]
then
        path=/library/sections/2/all

elif [[ "$option" = exit ]]
then
        a=1
fi
#-------------------------------

done

您将获得所有电影的 api 输出。您将获得读取命令exit | series:。选择series,将更改为path所有系列的端点并使用新path值发出 api 请求。也就是说,在系列和电影端点之间切换。

我所期望的

path=/library/sections/1/all

cache1=/library/sections/1/all
cache2=
cache3=

----------------

path=/library/sections/2/all

cache1=/library/sections/2/all
cache2=/library/sections/1/all
cache3=

实际情况

path=/library/sections/1/all

cache1=/library/sections/1/all
cache2=
cache3=

----------------

path=/library/sections/2/all

cache1=/library/sections/2/all
cache2=
cache3=

----------------

path=/library/sections/1/all

cache1=/library/sections/1/all
cache2=
cache3=

它替换了 cache1 的值,而不是移动并取代它的位置。我不明白为什么!

编辑:这确实有效。但上面提到的原始脚本仍然无效。

cache1=test4
cache2=test3
cache3=test2

newvalue=test5
sed -i "s|^cache3=.*|cache3=$cache2|" $filelocation/test.sh
sed -i "s|^cache2=.*|cache2=$cache1|" $filelocation/test.sh
sed -i "s|^cache1=.*|cache1=$newvalue|" $filelocation/test.sh

-----------------

cache1=test5
cache2=test4
cache3=test3

相关内容