RCP
RCP

Reputation: 374

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.

    #!/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??

Upvotes: 2

Views: 617

Answers (2)

Amit Bhardwaj
Amit Bhardwaj

Reputation: 333

Another easy way to achieve this is by parsing the file from the command you know you want to run using awk or sed.

Say you want to run every command after this line echo $uptime

awk method:

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

sed method:

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.

Upvotes: 0

Jiri Valenta
Jiri Valenta

Reputation: 540

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.

Upvotes: 4

Related Questions