Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Subshell and sed

Options
  • 23-03-2018 1:44pm
    #1
    Registered Users Posts: 46


    Hi all, I have a small piece of a script here that is supposed to remove prefixes from variables based on the contents of a table stored in a tmp file

    The issue arises when I cat the table and pipe it into the while loop, as it creates a subshell the data won't be returned.

    Searching online the advice to use 'here strings' is valid but still the output is blank.
    <<< "$(echo "$line")"

    ************************************
    cat $ZXDSA | while read line; do
    p1=$(echo $n1 | sed "s|^\\$line ||g")
    p2=$(echo $n2 | sed "s|^\\$line ||g")
    done

    echo $p1
    echo $p2
    ************************************
    Can anybody explain the process of how to return a value without losing the data within the loop.


Comments

  • Registered Users Posts: 883 ✭✭✭Keplar240B


    Are you aware of the shellcheck tool?

    I don't have time to debug right now but Shellcheck offers some hints

    https://www.shellcheck.net/

    Line 2:
    cat $ZXDSA | while read line; do
    ^-- SC2086: Double quote to prevent globbing and word splitting.
    ^-- SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.
    ^-- SC2162: read without -r will mangle backslashes.

    Line 3:
    p1=$(echo $n1 | sed "s|^\\$line ||g")
    ^-- SC2030: Modification of p1 is local (to subshell caused by pipeline).
    ^-- SC2001: See if you can use ${variable//search/replace} instead.
    ^-- SC2154: n1 is referenced but not assigned.
    ^-- SC2086: Double quote to prevent globbing and word splitting.

    Line 4:
    p2=$(echo $n2 | sed "s|^\\$line ||g")
    ^-- SC2030: Modification of p2 is local (to subshell caused by pipeline).
    ^-- SC2001: See if you can use ${variable//search/replace} instead.
    ^-- SC2154: n2 is referenced but not assigned.
    ^-- SC2086: Double quote to prevent globbing and word splitting.

    Line 7:
    echo $p1
    ^-- SC2031: p1 was modified in a subshell. That change might be lost.
    ^-- SC2086: Double quote to prevent globbing and word splitting.

    Line 8:
    echo $p2
    ^-- SC2031: p2 was modified in a subshell. That change might be lost.
    ^-- SC2086: Double quote to prevent globbing and word splitting.


  • Registered Users Posts: 1,931 ✭✭✭Zab


    If you use a redirect instead of a pipe it should work:
    while read line; do
    p1=$(echo $n1 | sed "s|^\\$line ||g")
    p2=$(echo $n2 | sed "s|^\\$line ||g")
    done < $ZXDSA
    
    echo $p1
    


  • Registered Users Posts: 46 TaytoMan69


    To get around the issue, I ended up using a here string. Success!

    while read line; do
    p1=$(sed "s|^\\$line ||g" <<< $s1)
    p2=$(sed "s|^\\$line ||g" <<< $s2)
    done <<< "$table"


Advertisement