简体   繁体   English

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

[英]Start bash script from specific line ,when i run it for the second time

Is there anything that i can use to make my bash script start's from specific line when i run it for the second time. 当我第二次运行它时,有什么可以使我的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

when i run it for the second time it has to start fom line host=$(hostname -I) , is there anything to do this?? 当我第二次运行它时必须启动fom行host = $(hostname -I) ,有什么可以做的吗?

Personally I would not overcomplicate it and touch a file somewhere on the filesystem after the one-time commands are executed. 就个人而言,一次执行命令后,我不会太复杂,也不会触摸文件系统上某个位置的文件。

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

# the rest of the script

Make sure you touch the file somewhere where it will not get deleted accidentally. 确保在不会意外删除文件的地方触摸该文件。

Another easy way to achieve this is by parsing the file from the command you know you want to run using awk or sed. 另一种简单的方法是通过使用awk或sed从您想运行的命令中解析文件。

Say you want to run every command after this line echo $uptime 假设您要在此行echo $uptime之后运行每个命令

awk method : awk方法

awk '/\$uptime/{y=1;next}y' filename.sh | bash

sed method : sed方法

sed '1,/\$uptime/d' filename.sh | bash

These will start running all the lines after the mentioned string found. 这些将在找到提到的字符串之后开始运行所有行。

Note: This will exclude the line which contains the search string, so you can pass the search string from n-1 line if you want to run all lines from nth line. 注意:这将排除包含搜索字符串的行,因此,如果要从第n行开始运行所有行,则可以从n-1行传递搜索字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM