I am relatively new with bash and I wonder if it is possible to put a condition inside a cat in bash
cat > /root/file <<EOL
A line of text
Some other text
EOL
Now i want to put an if condition inside the cat command.For e.g
if [condition] = something then Some other text is written to file.
Thanks in advance......
Not with cat specifically. But you don't have to write everything at once; use append redirection operator >>.
echo "A line of text" > /root/file
if [ "$FOO" = bar ]
echo "Another line of text" >> /root/file
fi
Or equivalently with cat:
cat > /root/file <<EOL
A line of text
EOL
if [ "$FOO" = bar ]
cat >> /root/file <<EOL
Another line of text
EOL
fi
No. cat is not a programming language, and bash is not responsible for interpreting (most of) the data sent to it. Consider using something like awk instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With