0
if [[ unix = unix ]]
then
echo "they are same" > a.txt
else 
echo "they are not same" > b.txt
a.txt>>b.txt>c.txt
mail -s "comparison" [email protected] < /home/c.txt

I just want the body of the mail to state either they are same or they are not same, but I'm not getting the expected output.

One of the files, either a.txt or b.txt is reported as command not found.

1
  • what is it that you are expecting a.txt>>b.txt>c.txt to do? are you trying to concatenate a.txt and b.txt into a new file called c.txt?
    – cas
    Dec 27, 2017 at 10:11

2 Answers 2

1

It's not clear to me what your intent is in your script but I will answer your primary question from the title, "How to append multiple files into single file?"...

Redirection operators (>, >>, <, etc.) don't work on files alone. They work with the inputs and outputs of commands. foo > bar doesn't mean "overwrite bar with the contents of foo". It means "run the command foo and overwrite bar with its output". And foo >> bar means "run the command foo and append its output to the current contents of bar". Also, you can't chain these. foo > bar > baz doesn't mean anything.

If you want to concatentate the contents of multiple files and append the result to another file then you'll want to use >> operator in conjunction with the cat command:

cat [OPTION]... [FILE]...

So, using your filenames, to append both a.txt and b.txt to c.txt you can do this:

cat a.txt b.txt >> c.txt

And since you asked about something similar in a comment, these two commands are equivalent to the single line version:

cat a.txt >> c.txt
cat b.txt >> c.txt
5
  • Thanks much, its working out for me...But i have a small query..If i want the content of the file to be appended to the next line of the file while doing multiple append, pls let me know how to do that..right now im getting multiple append output in the same line... Dec 27, 2017 at 10:16
  • > overwrites a file, >> appends to it.
    – cas
    Dec 27, 2017 at 10:21
  • @VimalKumar Not sure I understand your question. Are you saying that when you append a file containing banana to a file containing apple then the result is applebanana (i.e. a single line)? This would only happen if the file you're appending to did not have a newline at the end which is not common. How did the file you are appending to get generated?
    – B Layer
    Dec 27, 2017 at 10:47
  • yes thats how im getting the output as applebannana, but i need the output as apple in one line and bannana in second line seperately Dec 27, 2017 at 12:08
  • You need to figure out why it's getting created without the newline. Nothing I discuss in my answer is related to that. But as a workaround you could do echo "" >> file to append a newline before you append the other stuff.
    – B Layer
    Dec 27, 2017 at 12:11
0

That can be simplified a lot:

compare='not '
[[ unix = unix ]] && compare=''
echo "they are ${compare}the same" | mail -s "comparison" [email protected]

This sets not (with a trailing space) as the default value for variable $compare. if the two strings are the same, it sets $compare to be an empty string. Then prints the message with the embedded variable and mails it.

2
  • if [[ unix = unix ]] then echo "they are same" > a.txt else echo "they are not same" > b.txt if [[ db = db ]] then echo "they are same" > c.txt else echo "they are not same" > d.txt cat a.txt b.txt c.txt d.txt >> e.txt mail -s "comparison" [email protected] < /home/e.txt im getting the output as they are same they are same.. but i want the output as they are same they are same Dec 27, 2017 at 10:23
  • what? that makes no more sense than your original script. apart from the faulty use of redirection operators, you're still trying to use 3 temporary files when none are actually needed - all you need is one variable.
    – cas
    Dec 27, 2017 at 10:25

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .