Hi expert,
I'm a tester, I want to write a function with bash to collect log which the command I run. the log contains the "shell prompt--->
root@intel_5500_server:, and the last whole command in the shell ----> such as "cd /root/;ls |grep .sh"
to be clear it should be sommething like
root@intel_5500_server:~#cat my.log
root@intel_5500_server:~# cd /root/;ls |grep .sh
test.sh
test.sh~
vhost_libvirt.sh
vhost_libvirt.sh~
xx.sh
something like below I want a function like collect_log
#!/bin/bash
LOG=mylog
define a function named collect_log-------------->here
cd /root/;ls |grep .sh
collect_log
lspci |grep 0000
collect_log
then I can get the log like
root@intel_5500_server:~#cat mylog
root@intel_5500_server:~# cd /root/;ls |grep .sh
test.sh
test.sh~
vhost_libvirt.sh
vhost_libvirt.sh~
xx.sh
root@intel_5500_server:~# lspci -nn |grep 05:00
05:00.0 Ethernet controller [0200]: Intel Corporation 82571EB Gigabit Ethernet Controller [8086:105e] (rev 06)
05:00.1 Ethernet controller [0200]: Intel Corporation 82571EB Gigabit Ethernet Controller [8086:105e] (rev 06)
1)I don't how to get "root@intel_5500_server:~#"
2)I don't know how to get "cd /root/;ls |grep .sh"
can some one help me, Is it possible to make it as a function
vbe
December 19, 2012, 9:17am
2
van12:/tmp/testsamples $ cat titi |grep "#"|awk -F'#' '{print $1}'
root@intel_5500_server:~
van12:/tmp/testsamples $ cat titi |grep "#"|awk -F'#' '{print $2}'
cd /root/;ls |grep .sh
van12:/tmp/testsamples $
No,you misunderstand what I say, I modified my question.
Yoda
December 19, 2012, 9:52am
4
How about:-
{
set -v
cd /root/;ls |grep .sh
lspci |grep 0000
} > collect_log
---------- Post updated at 08:52 ---------- Previous update was at 08:45 ----------
Or you can use script
command to make typescript of terminal session.
How about:-
{
set -v
cd /root/;ls |grep .sh
lspci |grep 0000
} > collect_log
---------- Post updated at 08:52 ---------- Previous update was at 08:45 ----------
Or you can use script
command to make typescript of terminal session.
I don't see "root@intel_5500_server:~#" and the command which run in the log it doesn't match what I want
Yoda
December 19, 2012, 10:10am
6
Did you try using script
command? It will record everything printed on your screen including your shell prompt.
I will try it, but set -v seems not make it.it's strange
---------- Post updated at 07:18 AM ---------- Previous update was at 07:14 AM ----------
seems "script" can't be used in the function in my script
Yoda
December 19, 2012, 10:21am
8
It won't display the shell prompt. But it will display the commands and results. So if you want shell prompt as well, then using script
is the best option.
Here is how you can use script
command:-
script collect_log
This will log all results into the file collect_log
. This will open a sub-shell and records all information through this session. The script ends when the forked shell exits. I hope this helps.
It won't display the shell prompt. But it will display the commands and results. So if you want shell prompt as well, then using script
is the best option.
Here is how you can use script
command:-
script collect_log
This will log all results into the file collect_log
. This will open a sub-shell and records all information through this session. The script ends when the forked shell exits. I hope this helps.
Oh, but it seems useless for me, I just want to make the function in my script.
#!/bin/bash
LOG=mylog
define a fuction collect_log ---------> here
cd /root/;ls |grep .sh
collect_log
lspci |grep 0000
collect_log
binlib
December 19, 2012, 2:48pm
10
script is the right approach, but if you don't want to use it, then run bash in interactive mode with option "-i" and read all the commands through stdin.
echo 'cd /root/;ls |grep .sh
lspci |grep 0000' | bash -i > mylog 2>&1
# or
bash -i < file-with-commands > mylog 2>&1