Skip to main content
Truncate the append file if it exists, thanks ilkkachu.
Source Link
Stephen Kitt
  • 483.6k
  • 60
  • 1.2k
  • 1.4k

If the here document should only be added if none of it is present, you can use grep:

cat <<-"EOF1" >>> myPath/myFile.append
    content...
EOF1
if ! grep -F -q -f myPath/myFile{.append,}; then
    cat myPath/myFile.append >> myPath/myFile
fi

To understand this, consider the following.

  • grep -F -q -f myPath/myFile{.append,} is expanded by the shell to grep -F -q -f myPath/myFile.append myPath/myFile.

  • The grep command searches myPath/myFile (the file to which the text should be added if necessary) for any fixed string (-F) contained in myPath/myFile.append (the file containing the text to add), reading one pattern per line (-f), and indicates whether it finds any only by its exit code, with no output (-q).

  • The result is then negated !, so that the if block’s then part is only run if grep doesn’t find anything.

If the here document should only be added if none of it is present, you can use grep:

cat <<-"EOF1" >> myPath/myFile.append
    content...
EOF1
if ! grep -F -q -f myPath/myFile{.append,}; then
    cat myPath/myFile.append >> myPath/myFile
fi

To understand this, consider the following.

  • grep -F -q -f myPath/myFile{.append,} is expanded by the shell to grep -F -q -f myPath/myFile.append myPath/myFile.

  • The grep command searches myPath/myFile (the file to which the text should be added if necessary) for any fixed string (-F) contained in myPath/myFile.append (the file containing the text to add), reading one pattern per line (-f), and indicates whether it finds any only by its exit code, with no output (-q).

  • The result is then negated !, so that the if block’s then part is only run if grep doesn’t find anything.

If the here document should only be added if none of it is present, you can use grep:

cat <<-"EOF1" > myPath/myFile.append
    content...
EOF1
if ! grep -F -q -f myPath/myFile{.append,}; then
    cat myPath/myFile.append >> myPath/myFile
fi

To understand this, consider the following.

  • grep -F -q -f myPath/myFile{.append,} is expanded by the shell to grep -F -q -f myPath/myFile.append myPath/myFile.

  • The grep command searches myPath/myFile (the file to which the text should be added if necessary) for any fixed string (-F) contained in myPath/myFile.append (the file containing the text to add), reading one pattern per line (-f), and indicates whether it finds any only by its exit code, with no output (-q).

  • The result is then negated !, so that the if block’s then part is only run if grep doesn’t find anything.

add the here-doc too, otherwise this would just paste the code itself to myPath/myFile.append
Source Link
ilkkachu
  • 148.1k
  • 16
  • 268
  • 441

If the here document should only be added if none of it is present, you can use grep:

cat <<-"EOF1" >> myPath/myFile.append
    content...
EOF1
if ! grep -F -q -f myPath/myFile{.append,}; then
    cat myPath/myFile.append >> myPath/myFile
fi

To understand this, consider the following.

  • grep -F -q -f myPath/myFile{.append,} is expanded by the shell to grep -F -q -f myPath/myFile.append myPath/myFile.

  • The grep command searches myPath/myFile (the file to which the text should be added if necessary) for any fixed string (-F) contained in myPath/myFile.append (the file containing the text to add), reading one pattern per line (-f), and indicates whether it finds any only by its exit code, with no output (-q).

  • The result is then negated !, so that the if block’s then part is only run if grep doesn’t find anything.

If the here document should only be added if none of it is present, you can use grep:

cat <<-"EOF1" >> myPath/myFile.append
if ! grep -F -q -f myPath/myFile{.append,}; then
    cat myPath/myFile.append >> myPath/myFile
fi

To understand this, consider the following.

  • grep -F -q -f myPath/myFile{.append,} is expanded by the shell to grep -F -q -f myPath/myFile.append myPath/myFile.

  • The grep command searches myPath/myFile (the file to which the text should be added if necessary) for any fixed string (-F) contained in myPath/myFile.append (the file containing the text to add), reading one pattern per line (-f), and indicates whether it finds any only by its exit code, with no output (-q).

  • The result is then negated !, so that the if block’s then part is only run if grep doesn’t find anything.

If the here document should only be added if none of it is present, you can use grep:

cat <<-"EOF1" >> myPath/myFile.append
    content...
EOF1
if ! grep -F -q -f myPath/myFile{.append,}; then
    cat myPath/myFile.append >> myPath/myFile
fi

To understand this, consider the following.

  • grep -F -q -f myPath/myFile{.append,} is expanded by the shell to grep -F -q -f myPath/myFile.append myPath/myFile.

  • The grep command searches myPath/myFile (the file to which the text should be added if necessary) for any fixed string (-F) contained in myPath/myFile.append (the file containing the text to add), reading one pattern per line (-f), and indicates whether it finds any only by its exit code, with no output (-q).

  • The result is then negated !, so that the if block’s then part is only run if grep doesn’t find anything.

Bounty Awarded with 50 reputation awarded by Arcticooling

If the here document should only be added if none of it is present, you can use grep:

cat <<-"EOF1" >> myPath/myFile.append
if ! grep -F -q -f myPath/myFile{.append,}; then
    cat myPath/myFile.append >> myPath/myFile
fi

To understand this, consider the following.

  • grep -F -q -f myPath/myFile{.append,} is expanded by the shell to grep -F -q -f myPath/myFile.append myPath/myFile.

  • The grep command searches myPath/myFile (the file to which the text should be added if necessary) for any fixed string (-F) contained in myPath/myFile.append (the file containing the text to add), reading one pattern per line (-f), and indicates whether it finds any only by its exit codeexit code, with no output (-q).

  • The result is then negated !, so that the if block’s then part is only run if grep doesn’t find anything.

If the here document should only be added if none of it is present, you can use grep:

cat <<-"EOF1" >> myPath/myFile.append
if ! grep -F -q -f myPath/myFile{.append,}; then
    cat myPath/myFile.append >> myPath/myFile
fi

To understand this, consider the following.

  • grep -F -q -f myPath/myFile{.append,} is expanded by the shell to grep -F -q -f myPath/myFile.append myPath/myFile.

  • The grep command searches myPath/myFile (the file to which the text should be added if necessary) for any fixed string (-F) contained in myPath/myFile.append (the file containing the text to add), reading one pattern per line (-f), and indicates whether it finds any only by its exit code, with no output (-q).

  • The result is then negated !, so that the if block’s then part is only run if grep doesn’t find anything.

If the here document should only be added if none of it is present, you can use grep:

cat <<-"EOF1" >> myPath/myFile.append
if ! grep -F -q -f myPath/myFile{.append,}; then
    cat myPath/myFile.append >> myPath/myFile
fi

To understand this, consider the following.

  • grep -F -q -f myPath/myFile{.append,} is expanded by the shell to grep -F -q -f myPath/myFile.append myPath/myFile.

  • The grep command searches myPath/myFile (the file to which the text should be added if necessary) for any fixed string (-F) contained in myPath/myFile.append (the file containing the text to add), reading one pattern per line (-f), and indicates whether it finds any only by its exit code, with no output (-q).

  • The result is then negated !, so that the if block’s then part is only run if grep doesn’t find anything.

Missing 's'.
Source Link
Stephen Kitt
  • 483.6k
  • 60
  • 1.2k
  • 1.4k
Loading
Reformat and clarify, keeping the salient points of the rejected edit.
Source Link
Stephen Kitt
  • 483.6k
  • 60
  • 1.2k
  • 1.4k
Loading
Splitting one passage for ease of read and processing for newcomers to grep and this particular pattern. Added "will" just to have enough characters for edit.
Source Link
Loading
Add an explanation.
Source Link
Stephen Kitt
  • 483.6k
  • 60
  • 1.2k
  • 1.4k
Loading
Source Link
Stephen Kitt
  • 483.6k
  • 60
  • 1.2k
  • 1.4k
Loading