Why does my variable set in a do loop disappear? (unix shell)

后端 未结 2 1158
清歌不尽
清歌不尽 2021-01-24 10:39

This part of my script is comparing each line of a file to find a preset string. If the string does NOT exist as a line in the file, it should append it to the end of the file.<

2条回答
  •  感情败类
    2021-01-24 11:32

    Why does my variable set in a do loop disappear?

    It disappears because it is set in a shell pipeline component. Most shells run each part of a pipeline in a subshell. By Unix design, variables set in a subshell cannot affect their parent or any already running other shell.

    How can I avoid this?

    There are several ways:

    • The simplest is to use a shell that doesn't run the last component of a pipeline in a subshell. This is ksh default behavior, e.g. use that shebang:

      #!/bin/ksh

      This behavior can also be bash one when the lastpipe option is set:

      shopt -s lastpipe

    • You might use the variable in the same subshell that set it. Note that your original script indentation is wrong and might lead to the incorrect assumption that the if block is inside the pipeline, which isn't the case. Enclosing the whole block with parentheses will rectify that and would be the minimal change (two extra characters) to make it working:

    STRING=foobar
    cat "$FILE" | ( while read LINE
        do
            if [ "$STRING" == "$LINE" ]; then
                export ISLINEINFILE="yes"
            fi
        done
        if [ ! "$ISLINEINFILE" == yes ]; then
            echo "$LINE" >> "$FILE"
        fi
        )

    The variable would still be lost after that block though.

    • You might simply avoid the pipeline, which is straigthforward in your case, the cat being unnecessary:

    STRING=foobar
    while read LINE
    do
        if [ "$STRING" == "$LINE" ]; then
            export ISLINEINFILE="yes"
        fi
    done < "$FILE"
    if [ ! "$ISLINEINFILE" == yes ]; then
        echo "$LINE" >> "$FILE"
    fi

    • You might use another argorithmic approach, like using sed or gawk as suggested by John1024.

    See also https://unix.stackexchange.com/a/144137/2594 for standard compliance details.

提交回复
热议问题