Issue
I would like to accomplish the following via bash/sed:
#1. set i = 1 and j = 34
#2. Employ sed to modify my script (myscript.txt) using the values of i and j from step #1
#3. run a program that uses myscript.txt
#4. Employ sed again to change myscript.txt to its original values
#5. wait 5s
#6. increment i and j by 34 and repeat steps #2, #3, #4 and #5.
What I have gotten so far is that the code launches the program only once. Any suggestions?
j = 1
for i in {1..1021..34}; do
echo "Number: $i"
j=$(($i+33))
echo "Number: $j"
cd "/path_to_my_script/"
sed -i "s/local min = 123456789/local min = ${i}/g" myscript.txt &&
sed -i "s/local max = 123456789/local max = ${j}/g" myscript.txt &&
run program using myscript.txt && sleep 5s &&
sed -i "s/local min = ${i}/local min = 123456789/g" myscript.txt &&
sed -i "s/local max = ${j}/local max = 123456789/g" myscript.txt
done
Solution
Based on the great help provided the the StackOverflow community, I have been able to solve the problem. The final script can be represented by:
cd "/path_to_my_script/"
j = 1
for i in {1..1021..34}; do
echo "Number: $i"
j=$(($i+33))
echo "Number: $j"
sed -e "s/local min = 123456789/local min = ${i}/g" original_script.txt > script_to_run.txt
sed -i "s/local max = 123456789/local max = ${j}/g" script_to_run.txt
run program using script_to_run.txt & sleep 5s
done
Source of the problem: I was using a trial version of a program that expired a few days ago. To use it, I had to use faketime. After purchasing the full version, there was no need for faketime and the code worked perfectly. Besides, the code has been changes, since I wanted exactly how Bodo stated below: "start program in the background without waiting and at the same time sleep for 5 seconds"
Answered By - Jacob Answer Checked By - Terry (WPSolving Volunteer)