-1

I have 3 strings:

p001="57 59 13 30 96 45 24 71 33 15 40"
p002="57 7 13 11 80 44 47 50 84 86 64"
p003="57 7 13 11 50 44 42 50 84 86 64"

And based on the mcount (001,002, or 003), I want ptemp to be replaced. But I don't know how to "dynamically" create a new variable...

for mcount in {1..3}; do
    pad_m=`printf "%03d" $mcount`
    sed "s|PTEMP|p{pad_m}|; s|MTEMP|'${pad_m}'| " ./template.sh > .model.sh

This leads to error.

How can I do this?

Excerpt of template.sh

```
mnum=MTEMP

for pvar in PTEMP; do #Cycles through parameter files
    pad_p=`printf "%03d" $pvar`

    for svar in sq1 sq99; do
        state_file=${mnum}/${svar}
        ./model.exe ${state_file}

done 
done
```
maximusdooku
  • 5,242
  • 10
  • 54
  • 94
  • 1
    What is `template.sh`. How does it look like? What do you like to be replaced? the original and the replacement files? and didn't your previous question was marked to recommend you to use double-quotes for the `sed` replacement strings? – Inian Nov 29 '18 at 08:37
  • template.sh is just a file where I have `ptemp` as a variable. It's too long to be pasted here....Also, I did use " " - and it's working for me now. Thanks! So, I can do `s|PTEMP|{p001}|` and it works. But I need to be able to pick corresponding to the `mcount`...I guess I am trying to mimic a switch functionality. Sorry, I am not very conversant in bash. – maximusdooku Nov 29 '18 at 08:40
  • @Inian I have added parts of the template.sh file. Basically I am looking to give it the loop counter information. – maximusdooku Nov 29 '18 at 08:52
  • See [Dynamic variable names in Bash](https://stackoverflow.com/q/16553089/4154375). – pjh Nov 29 '18 at 19:52

1 Answers1

1

Although I've not fully understood what you want to do, my assumption is as follows:

  • To loop over the elements of p001, p002, or p003, whose number is indexed by mcount which ranges in {1..3}.
  • To execute ./model.exe based on the generated loop variables.

Then you could say something like below without generating the .model.sh script.

#!/bin/bash

p001="57 59 13 30 96 45 24 71 33 15 40"
p002="57 7 13 11 80 44 47 50 84 86 64"
p003="57 7 13 11 50 44 42 50 84 86 64"

for mcount in {1..3}; do
    pad_m=$(printf "%03d" $mcount)
    mnum=$pad_m

    varname="p$pad_m"   # will hold "p001", "p002" or "p003"
    p=${!varname}       # indirect variable referencing
    for pvar in $p; do
        pad_p=$(printf "%03d" "$pvar")
        # echo "$pad_p" # for debugging
        for svar in sq1 sq99; do
            state_file="${mnum}/${svar}"
            ./model.exe "$state_file"
            # echo "$state_file" # for debugging
        done
    done
done

If my assumption is incorrect, please let me know.
BR.

tshiono
  • 21,248
  • 2
  • 14
  • 22
  • Thank you much. I wanted to know about indirect variable referencing and didn't know what it was called.. Mods have gone insane in SO that they expect beginners of any language to not what terms to look for. – maximusdooku Nov 29 '18 at 17:27
  • 1
    Good to know I could be of help. FYI, note that the variable `p` is a *copy* of the original variable(s) and the modification to `p` is not reflected to the originals. If you need to modify them (I'm not sure if it is required in your case) try: `printf -v "$varname" FORMAT VAULE` or `declare "$varname"=VALUE`. – tshiono Nov 30 '18 at 03:40