Run bash with see the prints made by echo in script - linux

I got one script and I am designning a new one and the given script will be used and be called.
The thing is that the given one contains lots of "echo" prints.
Is there a way to use given script but not get those prints in the console?
Thanks

just run this,
yourscript.sh > /dev/null
It will send the output far, far away

You can also do yourscript.sh:
#!/bin/bash
exec 1>/dev/null
# rest of the script...

Related

Bash script : Storing command with spaces and arguments in variable and then executing

Been banging my head against the wall for a couple hours so time to call in the experts. Writing a small script to run some reports on one of my office's systems and I was asked to take care of a Bash script for it. The program called "auto_rep" takes various options such as "-t" to run one task (to generate one type of report) and a "-1" to exit after one task. The options are separated by spaces when running the command from command-line. The command works directly from command line but I cannot get it to work from a script...
Below is the snippet of code causing me issues:
cmd=$(auto_rep -t createfin1report -1)
echo "running ${cmd} command..."
echo
eval $cmd
The problem is when I run the script, only the "auto_rep" part of the command (from $cmd variable) is run; basically running the program without any options. And it creates tons of reports without the "-t createfin1report -1" part of the command (yikes!). Glad I only tried it on our test system.
Anyone have any tips to help me out? Is my approach way off? BTW - had tried just storing the command in a non-array (cmd="auto_rep -t createfin1report -1") and that was causing me other headache with a "command not found" errors :)...
Thanks in advance!
Save output to an array, then executing this array.
declare -a cmd
cmd=( $(auto_rep -t createfin1report -1) )
echo Executing: "${cmd[#]}"
"${cmd[#]}"
Please make sure the output is a valid command, and spaces have been correctly placed in double-quotes.

How to stop writing to log file in BASH?

I'm using this code to write all terminal's output to the file
exec > >(tee -a "${LOG_FILE}" )
exec 2> >(tee -a "${LOG_FILE}" >&2)
How to tell it to stop? If, for example, I don't want some output to get into log..
Thank you!
It's not completely clear what your goal is here, but here is something to try.
Do you know about the script utility? You can run script myTerminalOutputFile and any commands and output after that will be captured to myTerminalOutputFile. The downside is that it captures everything. You'll see funny screen control chars like ^[[5m;27j. So do a quick test to see if that works for you.
To finish capturing output just type exit and you are returned to you parent shell cmd-line.
Warning: check man script for details about the inherent funkyness of your particular OS's version of script. Some are better than others. script -k myTerminalOutputFile may work better in this case.
IHTH
You may pipe through "sed" and then tee to log file

Getting STDerr and STDout into a log file inside of a bash script

I was wondering if it is possible to get all of what is outputted from a script I have made to go to a log file if they change one of the variables in the script. Example, in the script a variable createLog=true could be set to enable logging.
I know I can do ./myscript.sh 2>&1 | tee sabs.log
But I would like to be able to simply run ./myscript.sh
and have the whole script logged in a file, as well as output to the console if the var is set to true.
Would I have to change every command in the script to accomplish this or is there a command I can execute at the beginning of the script that will output to both.
If you need more details please let me know.
Thanks!
exec without an argument lets you redirect for the remainder of the current script.
exec >log 2>&1
You can't tee within the redirect but you can display the file with a background job.
tail -f log &

Cannot redirect command output to a file (for postfix status)

I am trying to run the following command:
postfix status > tmp
however the resulting file never has any content written, and instead the output is still sent to the terminal.
I have tried adding the following into the mix, and even piping to echo before redirecting the output, but nothing seems ot have any effect
postfix status 2>&1 > tmp
Other commands work no problem.
script -c 'postfix status' -q tmp
It looks like it writes to the terminal instead to stdout. I don't understand piping to 'echo', did you mean piping to 'cat'?
I think you can always use the 'script' command, that logs everything that you see on the terminal. You would run 'script', then your command, then exit.
Thanks to another SO user, who deleted their answer, so now I can't thank, I was put on the right track. I found the answer here:
http://irbs.net/internet/postfix/0211/2756.html
So for those who want to be able to catch the response of the posfix, I used the following method.
Create a script which causes the output to go to where you wish. I did that like this:
#!/bin/sh
cat <<EOF | expect 2>&1
set timeout -1
spawn postfix status
expect eof
EOF
Then i ran the script (say script.sh) and could pipe/redirect from there. i.e. script.sh > file.txt
I needed this for PHP so I could use exec and actually get a response.

Shell script : how to output to command line?

I'm making a shell script and I want to know if it's possible to write directly to the command line when the script is executed ?
Example :
user#localhost:/home/user$./script.sh
... output
... another output
... another output
... last output
user#localhost:/home/user$I want to write here on the command line
I don't want to "echo" some text, I want to write directly at the prompt.
Thanks!
No, you can't do that. If you want user to invoke your provided command after your script is finished - why not just prompt user for confirmation?
If you just want the text to show up there, but not be able to do anything with it, you can do this.
File test.sh:
echo "Output"
./test2.sh &
File test2.sh:
echo "Output2"
Notice how the first script calls the second script with the & at the end.
In this case, "Output2" will be written to the prompt, but it can't be deleted and will have no effect on the next command at all. But if this is something you're doing to grab the user's attention, it would work.
In ksh:
print -s $(script)
will print to the command history. Wrap this in a function and you'll have something close to what you are asking for.
If you are using X environment install xclip and xdotool, then:
#!/bin/bash
your scripts....
echo -n your command to write 2>&1|xclip
xdotool click 2

Resources