Bash output limited to echo only

3.2k views Asked by At

I am writing a bash script to handle by backups. I have created a message function controller that uses functions to handle email, log and output.

So the structure is as:

message_call(i, "This is the output")

Message Function

-> Pass to email function --> Build email file

-> Pass to log function --> Build log file

-> Pass to echo function (custom) --> Format and echo input dependent on $1 as a switch and $2 as the output message

When I echo I want nice clean output that only consists of messages passed to the echo function, I can point all output /dev/null but I am struggling to limit all output except for the echo command.

Current output sample:

craig@ubuntu:~/backup/functions$ sudo ./echo_function.sh i test
+ SWITCH=i
+ INPUT=test
+ echo_function
+ echo_main
+ echo_controller i test
+ '[' i == i ']'
+ echo_info test
+ echo -e '\e[32m\e[1m[INFO]\e[0m test'
[INFO] test
+ echo test
test
+ '[' i == w ']'
+ '[' i == e ']'

Above I ran the echo function alone and the output I want is on line 10, all other output in the sample I don't want.

2

There are 2 answers

2
djanderson On BEST ANSWER

If you have the line set -x in your script, comment it out. If not, try adding set +x at the top of your script.

0
Eric Renouf On

If you want to hide all the output from everything except what you're explicitly doing in your echo function you could do something like this:

exec 7>&1 # save a copy of current stdout
exec >/dev/null # redirect everyone else's stdout to /dev/null
ls # output goes to /dev/null
echo My Message >&7 # output goes to "old" stdout