Linux 当我第二次运行bash脚本时,从特定行启动它

Linux 当我第二次运行bash脚本时,从特定行启动它,linux,bash,scripting,Linux,Bash,Scripting,当我第二次运行bash脚本时,有什么东西可以让它从特定的行开始 #!/bin/bash #installed jq by using sudo apt-get install jq # return indices and os,process a=$(curl -XGET 'http://localhost:9200/_nodes/node01/stats/os,process?pretty=1') #return just jvm b=$(curl -XGET 'http://lo

当我第二次运行bash脚本时,有什么东西可以让它从特定的行开始

    #!/bin/bash
#installed jq by using sudo apt-get install jq
# return indices and os,process
a=$(curl -XGET 'http://localhost:9200/_nodes/node01/stats/os,process?pretty=1')

#return just jvm
b=$(curl -XGET 'http://localhost:9200/_nodes/node01/stats/jvm?pretty=1')
c=$(curl -XGET 'http://localhost:9200/_nodes/node01/stats/indices?pretty=1')

d=$(curl -XGET 'http://localhost:9200/_nodes/node01/stats/thread_pool?pretty=1')

e=$(curl -XGET 'http://localhost:9200/_cluster/stats?pretty=1')

f=$(curl -XGET 'http://localhost:9200/_cat/nodes?v&h=uptime')
uptime=$(echo $f | cut -d' ' -f 2)
echo $uptime
echo $e >"parameters4.json"

echo $d > "parameters3.json"


echo $c > "parameters2.json"

echo $b

#uptime=$(cat servrtime.txt | grep  uptime)

host=$(hostname -I)
echo $host

当我第二次运行它时,它必须启动fom line host=$hostname-i,有什么方法可以做到这一点吗???

就我个人而言,在执行一次性命令后,我不会将它过度复杂化,并在文件系统的某个地方触摸文件

if [[ ! -f /some/file ]]; then
  # your commands that should be executed only once
  touch /some/file
fi

# the rest of the script

确保您在不会意外删除文件的地方触摸该文件。

另一种简单的方法是使用您知道要使用awk或sed运行的命令解析该文件

假设您希望在这一行echo$uptime之后运行每个命令

awk方法:

sed方法:

找到所提到的字符串后,这些将开始运行所有行


注意:这将排除包含搜索字符串的行,因此如果要从第n行运行所有行,可以从n-1行传递搜索字符串。

是否可以在某处写入文件以识别第二次运行?如果不是,脚本是否从bash shell以交互方式运行?@Florian Weimer编写要识别的文件似乎有点复杂,是的,我的脚本以交互方式运行yperhaps修改脚本以接受第一次运行脚本时传递的CLI参数。然后,仅当提供CLI arg时才有条件地运行初始代码。第二次运行脚本时,请忽略CLI参数。@RobC,我没有手动运行脚本的选项,我正在使用cron运行脚本。@zappy我不是说它不工作,但它似乎太复杂了。您需要确保env变量将在新的shell会话、系统重新启动等过程中保持不变。。如果你想以后摆脱它,你必须从你设置它的任何地方取消设置它。bashrc或类似的。在我看来,出于这个目的使用空文件要容易得多,创建/删除它比处理环境变量更容易。至少这是我的感觉:@RCP,别忘了清除那个信号量文件,为下一次运行腾出空间,如果有的话。然后单击此答案左侧的复选标记,将其标记为完成。
awk '/\$uptime/{y=1;next}y' filename.sh | bash
sed '1,/\$uptime/d' filename.sh | bash