-1

I edited the post and I follow your advice, I splitted my work in more files.

I want to replace a text block inside a file but after a delimiter

<-FROM NEXT LINE->
code to be replaced
code to be replaced
code to be replaced
<-TO THE LINE ABOVE->

Because I collect online data, time by time the data is different, so the code that have to be replaced is different too.

This's the portion of the script:

#!/bin/bash

set -x

########## CONFIGURATIONS ##########
live_data_url='https://www.somedata.com/api/url'

live_data_list_file=~/jjjjjjjj
file_to_modify=~/kkkkkkk
########## CONFIGURATIONS ##########


########## FUNCTIONS ##########
function upgrade() {
    echo "Downloading/Upgrading traker data ..."

    wget -O $live_data_list_file $live_data_url
    if [[ $? -ne 0 ]]; then
        echo "I can't download the data, I'll use a static one"
        exit 9
    fi

    echo "Downloading/Upgrading done."
}
########## FUNCTIONS ##########

upgrade

sed -i -ne '/STARTINGPOINT+1LINE/ {p; r $live_data_list_file' -e ':a; n; /ENDINGPOINT-1LINE/ {p; b}; ba}; p' $file_to_modify

The goal is to upgrade the block inside the $file_to_modify section:

<-FROM NEXT LINE->
code to be replaced
code to be replaced
code to be replaced
<-TO THE LINE ABOVE->

so I searched a bit and I found a good starting point, but I don't know how to modify it. Is not a requisite the use of sed, maybe exist a better way to do this Actually I've 2 difficulty 1. I need to start after the line that contain <-FROM NEXT LINE-> and I need to stop the line above <-TO THE LINE ABOVE-> 2. The expansion of $live_data_list_file inside the sed phase is not working, so I can't take data from file.

Do you have a better idea to do this?

Jorman Franzini
  • 329
  • 1
  • 3
  • 17
  • Possible duplicate of [Could replacing a bash script with a new version cause a running instance of the script to fail](https://stackoverflow.com/questions/4754592/could-replacing-a-bash-script-with-a-new-version-cause-a-running-instance-of-the) – jeremysprofile Sep 06 '19 at 22:30
  • Possible duplicate of [Bash script that edits itself](https://stackoverflow.com/questions/3168402/bash-script-that-edits-itself) – David C. Rankin Sep 06 '19 at 22:30
  • 1
    I don't see any other than malicious use cases for this. If you really plan to use this for application development, you need to find a way to separate code and data. `json` for example is a lightweight data exchange format. – hek2mgl Sep 06 '19 at 22:33
  • I can understand the risk. But I ask to a more capable guys: is so bad even if this is called at the very end just before the script exit? – Jorman Franzini Sep 06 '19 at 22:41
  • [edit] your question so the description of what you want to do (modify from "<-FROM NEXT LINE->" to "<-TO THE LINE ABOVE->") matches the sample input you provide (currently contains neither of those lines) and post the exact expected output given that sample input. – Ed Morton Sep 07 '19 at 15:02
  • It's hard to understand why you're considering this, by the way, as it seems like either a) reading the contents of live_data_list_file if it's actually just values to set your variables to or b) executing live_data_list_file if it's really code, would be a far more clear, simple, obvious, and less error-prone approach. So please include sample contents of live_data_list_file too and explain why using it to modify the running script is the best approach to solving whatever problem it is you're trying to solve. – Ed Morton Sep 07 '19 at 15:23
  • @EdMorton The original purpose is to have only one file that auto-update itself before exit, but seems that is not a good idea. So I split the script, the main script, under cron, have static data in case of failure. The main script download the `live_data_list_file` then if not exist create a 2nd script and cron it, so the 2nd script is the one that modify the static data inside the 1st script, taking data from `live_data_list_file` – Jorman Franzini Sep 07 '19 at 20:43

1 Answers1

1

Instead of storing the data in your script, leave the live data file alone. That is, download the data to $live_data_list_file, have wget download to a temporary file, and, if wget succeeds, then copy the temporary over the live data file. Also, then you don't edit that file elsewhere - it must be left pristine for the next run in case wget fails.

Perhaps something like this:

function upgrade() {
    echo "Downloading/Upgrading tracker data ..."

    # grab the current data into a temporary file
    wget -O ${live_data_list_file}.temp $live_data_url
    if [[ $? -ne 0 ]]; then
        echo "I can't download the data, I'll use the last one"
    else
        # succeeded, so the temp file is our real file.
        mv ${live_data_list_file}.temp ${live_data_list_file}
    fi

    echo "Downloading/Upgrading done."
}

upgrade()

# don't modify ${live_data_list_file} in the rest of your code - use another temporary file if you need to modify it.
# and just use ${live_data_list_file}, not the data "inside" your code
Tanktalus
  • 21,664
  • 5
  • 41
  • 68
  • thanks for your reply, '$live_data_list_file' is already alone, and is upgraded day by day, my idea is, if the script works like always, just before exit, update the script with the new data. It's so a bad idea? If I understood, your idea is to create a third file in order to use this file? What about if the wget don't work at the very 1st start? – Jorman Franzini Sep 06 '19 at 22:47
  • @JormanFranzini - yes, create an extra one. And what if wget doesn't work your very first start as is? That static data you're using today, just use that as your first seed for the live data. (Updated with example) – Tanktalus Sep 07 '19 at 13:51
  • Thanks, I edited the original question. Like all said, modify one runnins script is not so good, so I created an extra script to modify the main script. Do you have some idea on how to replace all text inside a delimited range? – Jorman Franzini Sep 07 '19 at 20:46