Write output from bash linux, in a file

1

I need to do some testing on the linux command line, but doing all the testing is very time-consuming, can anyone help me, if I can, write code in shell script to run all tests, and write output from bash in a txt file?

    
asked by anonymous 27.10.2017 / 18:08

1 answer

1

Everything you manually run in the shell is "shell script code". Therefore, create a script file containing the execution of your tests and execute it by redirecting its output to the desired file:

test.sh file:

#/bin/bash

#script de teste...
echo "$(date '+%F %T') Executando teste1.sh";
./teste1.sh;
echo "$(date '+%F %T') teste1.sh finalizado";

#teste de trecho de codigo...
echo "$(date '+%F %T') Executando teste de for loop...";
for i in {1..100}; do
   echo $i;
done;
echo "$(date '+%F %T') for loop finalizado";

#teste de execucao de ferramenta de sistema...
echo "$(date '+%F %T') Efetuando coletas de vmstat";
vmstat 1 3;
echo "$(date '+%F %T') vmstat coletado";

In the example above I include informational messages with the start and end date and time of each step. The steps are hypothetical, you can include the commands you want, remembering that teste1.sh does not exist at all, and was only included as an example of arbitrary script execution.

Do not forget to mark your script as an executable:

chmod +x testes.sh;

When you run tests.sh , redirect your output to your log file, both standard and error output, to make sure you have all the information about the executed commands:

./testes.sh >> saida.txt 2>&1

This works because all the commands encapsulated in tests.sh are directed to standard output, so you just need to redirect the output of tests.sh to where you think best . So, make sure you're not redirecting unwanted individual test output to other files.

    
27.10.2017 / 18:42