Find the answer to your Linux question:
Results 1 to 2 of 2
I need to write a script with nested EOF's to avoid generating potentially very large numbers of intermediate files and cannot work out how to do this, rough code follows; ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2011
    Posts
    1

    bash nested EOF


    I need to write a script with nested EOF's to avoid generating potentially very large numbers of intermediate files and cannot work out how to do this, rough code follows; any ideas greatly appreciated

    #!/bin/sh

    outer_command <<OUTER_EOF
    #!/bin/sh
    inner_command <<INNER_EOF
    start of inner_script contents
    .
    .
    .
    end of inner_script contents
    INNER_EOF
    OUTER_EOF

  2. #2
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    First, my opinion is, if you need nested "Here Files", you are almost certainly going about solving your problem the wrong way. Or if you are sure this is the ONLY way to solve the problem, you are better of NOT using bash. Try Perl or Python instead.

    However, to answer your question, I would do it like this
    Code:
    #!/bin/bash
    
    cat <<-__SUB_COMMAND0__
    Executed within level 0
    __SUB_COMMAND0__
    
    bash <<-__LEVEL1__
    
    cat <<-__SUB_COMMAND1__
    Executed within level 1
    __SUB_COMMAND1__
    
    bash <<-__LEVEL2__
    
    cat <<-__SUB_COMMAND2__
    Executed within level 2
    __SUB_COMMAND2__
    
    __LEVEL2__
    __LEVEL1__
    What this does is creates a child bash process with the input of the child process coming from the "Here File". Once you have established the bash sub-process, you can "cat" information to sub-sub-processes like grep or whatever.

    However, as you can see, this quickly turns into spaghetti code and is very difficult to read and debug.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •