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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Subshell and sed

  • 23-03-2018 12: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: 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, 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