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.
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
However, it appears as if both $LINE and $ISLINEINFILE are both cleared upon finishing the do loop. How can I avoid this?
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.
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 thelastpipe
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:The variable would still be lost after that block though.
cat
being unnecessary:sed
orgawk
as suggested by John1024.See also https://unix.stackexchange.com/a/144137/2594 for standard compliance details.
Using shell
If we want to make just the minimal change to your code to get it working, all we need to do is switch the input redirection:
In the above, we changed
cat "$file" | while do ...done
towhile do...done<"$file"
. With this one change, thewhile
loop is no longer in a subshell and, consequently, shell variables created in the loop live on after the loop completes.Using sed
I believe that the whole of your script can be replaced with:
The above adds line
foobar
to the end of each file that doesn't already have a line that matches^foobar$
.The above shows
file*
as the final argument to sed. This will apply the change to all files matching the glob. You could list specific files individually if you prefer.The above was tested on GNU sed (linux). Minor modifications may be needed for BSD/OSX sed.
Using GNU awk (gawk)
Like the sed command, this can tackle multiple files all in one command.