Write linux bash exits to a file

Asked

Viewed 3,365 times

1

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

1 answer

1


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

Filing cabinet sh tests.:

#/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 have included informative 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 at first the teste1.sh does not exist, and was only included as an example of arbitrary script execution.

Don’t forget to also mark your script as executable:

chmod +x testes.sh;

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

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

This works because all commands encapsulated in sh tests. are directed to the standard output, then you just need to redirect the output from sh tests. where you find it best. So, make sure that you are not unintentionally redirecting individual test output to other files.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.