2

What im also supposed to do is if there is no arguments i am supposed to print a "help" message to the standard error device using cat. So far i can understand and get it to work using echo but my task is to do this using cat only. When i try the line cat 2> (help message) it goes to a new line where i can type anything and causes the script to not work correctly at all the only escape being ctrl + z. How can this be done using cat instead of echo? With the stderr message still being printed out as well if thats possible using only cat?

Help Message

Usage: concat FILE ...
Description: concatenates FILE(s) to standard output separating them with divider -----.

Code

#!/bin/bash
# concat script
if [[ $@ ]]
then
        for i in "$@"
        do
         cat "$i" && echo  "-----"
        done
exit 0
else
 cat 2> "Usage: concat FILE ...
Description: concatenates FILE(s) to standard output separating them with divider -----."
exit 1
fi
Bret Hasel
  • 303
  • 1
  • 11
  • Possible duplicate of [Check number of arguments passed to a Bash script](https://stackoverflow.com/questions/18568706/check-number-of-arguments-passed-to-a-bash-script) – grooveplex Feb 08 '19 at 23:59
  • Not a duplicate, Im looking to do this with only using cat specifically not echo. Also im not recieving any error message like in that post – Bret Hasel Feb 09 '19 at 00:00
  • That's why I said it's a *possible* duplicate. :) What I think you should do is check the number of arguments. If it's less than 1 (or 2), show the help message, else, run your core program logic. You don't need `cat` or `echo` for that at all. :) – grooveplex Feb 09 '19 at 00:04
  • Number of arguments are already checked though. When no arguments are there and i am using `echo` it properly displays the help message like it should. What i am TASKED to do though and MUST do is use cat in order to "print the following help message to the standard error device.". This is where my issue lies. – Bret Hasel Feb 09 '19 at 00:07
  • You can write the `for ... do ... done` loop shorter (without loop): `cat "$@" && echo "-----"` – Wiimm Feb 09 '19 at 00:08
  • Thank you william i appreciate that, that isnt my main concern here though for that section of my code is working properly when executed even if it isnt as pretty. What my confusion and question is. is using cat to print the help message to stderr because at the moment the line of code i have is not working. the line `cat 2> "Usage: concat FILE ... Description: concatenates FILE(s) to standard output separating them with divider -----."` – Bret Hasel Feb 09 '19 at 00:11
  • @BretHasel, if you can't describe a *practical* reason to use `cat` instead of `echo` (something other than pure whim), this question falls afoul of the "*practical*, answerable" requirements in https://stackoverflow.com/help/on-topic – Charles Duffy Feb 09 '19 at 01:23

2 Answers2

4

cat is used to output data from a file. To output data from a string, use echo.

2> is for redirecting stdout to a file. To point stdout to stderr, use >&2.

In all:

echo >&2 "Usage: concat FILE ...
Description: concatenates FILE(s) to standard output separating them with divider -----."

If you really want to avoid using the right tool for the job, you can rewrite it in terms of a here document (creating a temporary file that cat can read from):

cat << EOF >&2
Usage: concat FILE ...
Description: concatenates FILE(s) to standard output separating them with divider -----.
EOF
that other guy
  • 116,971
  • 11
  • 170
  • 194
2

If you want to print a message to stderr with cat, not echo, please try:

cat <<< "Usage: ..." >&2
tshiono
  • 21,248
  • 2
  • 14
  • 22