Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Subshell and sed

  • 23-03-2018 01:44PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 887 ✭✭✭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, Registered Users 2 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, Registered Users 2 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