Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put a condition inside a cat in bash

Tags:

bash

cat

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......

like image 798
user2650277 Avatar asked Nov 23 '25 12:11

user2650277


2 Answers

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
like image 93
Amadan Avatar answered Nov 25 '25 09:11

Amadan


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.

like image 28
Ignacio Vazquez-Abrams Avatar answered Nov 25 '25 11:11

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!