0

Sorry to be a pain, but I'm not sure how I can loop values from an outside file, into my bash script as variables. I have three variable names in my bash script:

$TAGBEGIN
$TAGEND
$MYCODE

In a separate varSrc.txt file, I have several variables:

@ a - Some marker
tagBegin_a='/<!-- Begin A -->/'
tagEnd_a='/<!-- End A -->/'
code_a=' [ some code to replace in between tags ] '

@ b - Some marker
tagBegin_b='/<!-- Begin B -->/'
tagEnd_b='/<!-- End B -->/'
code_b=' [ some code to replace in between tags ] '

@ c - Some marker
...

I need my bash script to be able to loop through each "@ marker"* section and perform a function:

source varSrc.txt

$TAGBEGIN
$TAGEND
$MYCODE
...
sed '           
    '"$TAGEND"' R '"$MYCODE"'
    '"$TAGBEGIN"','"$TAGEND"' d
' -i $TARGETDIR

Note: sed code logic (not quoting mess) courtesy of Glenn J.

I need some kind of looping logic like:

for (var i = 0; i <= markers in varSrc.txt ; i++) {
// set bash vars equal to varSrc values
$TAGBEGIN=  $tagBegin_i 
$TAGEND= $tagEnd_i
$MYCODE= $code_i

// run the 'sed' replace command
    sed '           
        '"$TAGEND"' R '"$MYCODE"'
        '"$TAGBEGIN"','"$TAGEND"' d
    ' -i $TARGETDIR

}

Is this something that can be feasibly done in a bash script and is this a good approach? Any suggestions, pointers or guidance is very, very appreciated!

*(which I don't think is a real marker I can use)

12
  • Your other file, notably, is not in bash syntax. Bash variable assignments are foo=bar, not $foo=bar or $foo =bar. Commented Nov 13, 2015 at 16:16
  • Oops, I'll fix that. (Sorry, too many variables floating around in my head...) Commented Nov 13, 2015 at 16:23
  • Also, @ isn't valid comment syntax usable for a marker. Commented Nov 13, 2015 at 16:24
  • 1
    ...if these items are "something to fix", then my entire answer was predicated on an assumption (that you needed to parse non-bash syntax) that may not be true. Please be more careful when asking questions in the future. Commented Nov 13, 2015 at 16:25
  • 1
    Don't blame me for that quoting hell. I suggested this Commented Nov 13, 2015 at 18:02

3 Answers 3

2

[Answering the question as amended]

There's no need use use, iterate over, or think about markers at all. Leave them out.

source varSrc.txt
for beginVar in "${!tagBegin_@}"; do  # Iterate over defined begin variable names
  endVar=tagEnd_${var#tagBegin_}      # Generate the name of the end variable
  codeVar=code_${var#tagBegin_}       # Generate the name of the code variable
  begin=${!beginVar}                  # Look up the contents of the begin variable
  end=${!endVar}                      # Look up the contents of the end variable
  code=${!codeVar}                    # Look up the contents of the code variable

  sed -e "$end R $code" -e "$begin,$end d" -i "$file"
done
Sign up to request clarification or add additional context in comments.

5 Comments

When I ran this, I got a "sed: "-e expression #2, char 55: unexpected ,' " error message. Is my varSrv.txt code_a` variable messing something up? I just put a string value in there for testing (e.g. code_a="This is a test A.").
@arl, ...in general, I'm telling you how to loop over variables in bash; I'm not telling you anything about sed.
Woah, you're fast. I put the '/<!-- ... -->/' in my declarations, too. I'm just trying to narrow the issue down. I'll research sed some more (and those darn single vs double quotes). Thank you, Charles Duffy!
err. bash -x yourscript, that is; will do a great deal of good to make it clear where the issue is, in any event.
@arl, the important thing to keep in mind about quotes, btw, is that syntactic quotes are strictly for the shell's use. sed has no way of telling whether you used single or double quotes in any given place; it's simply a matter of telling the shell how to behave when parsing and running expansions.
2

[Answers original, pre-amended question]

source only works if your input file is valid bash syntax; it isn't. Thus, you'll need to parse it yourself, something like the following:

begin= end= code=
while IFS= read -r; do
  case $REPLY in
    @*)
      # we saw a marker; process all vars seen so far
      [[ $begin && $end && $code ]] || continue # do nothing if we have no vars seen
      sed -e "$end R $code" -e "$begin,$end d" -i "$file"
      ;;
    '$TAGBEGIN='*) begin=${REPLY#'$TAGBEGIN='} ;;
    '$TAGEND='*)   end=${REPLY#'$TAGEND='} ;;
    '$MYCODE='*)   code=${REPLY#'$MYCODE='} ;;
  esac
done <varSrc.txt

Comments

0

What you can do is export your variables in your second file an the execute the script within your current environment (with a dot before the script) to get the variable names/markers you can parse the file and search for an $ or @

3 Comments

There's no need to parse the file if the input is modified to be able to be sourced -- bash incorporates tools for for searching through the list of defined variables for variable names with a known prefix.
Also, if it's sourced in, there's no need to export anything; the variables can be accessed as native shell variables without needing to be copied to the environment.
...so, I'd just ask the shell to list variables starting with tagBegin_, and assume that tagEnd_ and tagCode_ variables with similar suffixes will also exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.