I have a script getInfo.sh I made in Linux that collects information about the user, date, number of users logged into current environment, and many other features.
I could easily outside of the file have something where it is ./getInfo.sh > results.txt.
However I would like this to happen automatically within the script.
If I were to include the ./getInfo.sh > results.txt within the script itself it would create an infinite loop as each time it hits the ./getInfo.sh it will re-run the script and create an infinite loop.
Any alternatives here?
exec 2>&1>results.txt &as the first command in your script.echo "something important" >> newfile- you can pass the output file as a parameter or write to a default name if no argument is provided.exec 2>&1>& results.txtas the first command in your script. That will redirect all output (errors included), toresults.txt. If you want errors messages still be printed to console, use this insteadexec 1>& results.txt.