History inside a Bash Loop
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I'm using bash to execute complex scripts having for and wile loops.
I activate the history in the script using :
set -o history -o histexpand
But if I execute the following script:
#!/bin/bash
set -o history -o histexpand
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
history
I can see that 'history' does not track commands inside the for loop :
[scripts]$ ~/test2.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
1 for i in 1 2 3 4 5; do echo "Welcome $i times"; done
2 history
Is there a way to track the commands inside the for loop also ? In this present sample, I would love to get the following result :
[scripts]$ ~/test2.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
1 for i in 1 2 3 4 5; do echo "Welcome $i times"; done
2 echo "Welcome $i times"
3 echo "Welcome $i times"
4 echo "Welcome $i times"
5 echo "Welcome $i times"
6 echo "Welcome $i times"
7 history
bash command-history
add a comment |Â
up vote
0
down vote
favorite
I'm using bash to execute complex scripts having for and wile loops.
I activate the history in the script using :
set -o history -o histexpand
But if I execute the following script:
#!/bin/bash
set -o history -o histexpand
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
history
I can see that 'history' does not track commands inside the for loop :
[scripts]$ ~/test2.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
1 for i in 1 2 3 4 5; do echo "Welcome $i times"; done
2 history
Is there a way to track the commands inside the for loop also ? In this present sample, I would love to get the following result :
[scripts]$ ~/test2.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
1 for i in 1 2 3 4 5; do echo "Welcome $i times"; done
2 echo "Welcome $i times"
3 echo "Welcome $i times"
4 echo "Welcome $i times"
5 echo "Welcome $i times"
6 echo "Welcome $i times"
7 history
bash command-history
2
May I know your concern for doing this. do you want to track your script flow while executing?
â msp9011
Sep 5 at 11:05
Of course : I would like to display the last executed command when there is an error, event if the error was inside a loop of my script...
â Richard
Sep 5 at 12:38
Because I'm not in development phase anymore and the script is executed every 5 minutes. and I would like to send by e-mail a functional error report, displaying the command that did not work... And this command work certainly 99% of the time...
â Richard
Sep 5 at 13:16
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm using bash to execute complex scripts having for and wile loops.
I activate the history in the script using :
set -o history -o histexpand
But if I execute the following script:
#!/bin/bash
set -o history -o histexpand
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
history
I can see that 'history' does not track commands inside the for loop :
[scripts]$ ~/test2.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
1 for i in 1 2 3 4 5; do echo "Welcome $i times"; done
2 history
Is there a way to track the commands inside the for loop also ? In this present sample, I would love to get the following result :
[scripts]$ ~/test2.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
1 for i in 1 2 3 4 5; do echo "Welcome $i times"; done
2 echo "Welcome $i times"
3 echo "Welcome $i times"
4 echo "Welcome $i times"
5 echo "Welcome $i times"
6 echo "Welcome $i times"
7 history
bash command-history
I'm using bash to execute complex scripts having for and wile loops.
I activate the history in the script using :
set -o history -o histexpand
But if I execute the following script:
#!/bin/bash
set -o history -o histexpand
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
history
I can see that 'history' does not track commands inside the for loop :
[scripts]$ ~/test2.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
1 for i in 1 2 3 4 5; do echo "Welcome $i times"; done
2 history
Is there a way to track the commands inside the for loop also ? In this present sample, I would love to get the following result :
[scripts]$ ~/test2.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
1 for i in 1 2 3 4 5; do echo "Welcome $i times"; done
2 echo "Welcome $i times"
3 echo "Welcome $i times"
4 echo "Welcome $i times"
5 echo "Welcome $i times"
6 echo "Welcome $i times"
7 history
bash command-history
bash command-history
edited Sep 5 at 17:56
Rui F Ribeiro
36.8k1273117
36.8k1273117
asked Sep 5 at 10:50
Richard
11
11
2
May I know your concern for doing this. do you want to track your script flow while executing?
â msp9011
Sep 5 at 11:05
Of course : I would like to display the last executed command when there is an error, event if the error was inside a loop of my script...
â Richard
Sep 5 at 12:38
Because I'm not in development phase anymore and the script is executed every 5 minutes. and I would like to send by e-mail a functional error report, displaying the command that did not work... And this command work certainly 99% of the time...
â Richard
Sep 5 at 13:16
add a comment |Â
2
May I know your concern for doing this. do you want to track your script flow while executing?
â msp9011
Sep 5 at 11:05
Of course : I would like to display the last executed command when there is an error, event if the error was inside a loop of my script...
â Richard
Sep 5 at 12:38
Because I'm not in development phase anymore and the script is executed every 5 minutes. and I would like to send by e-mail a functional error report, displaying the command that did not work... And this command work certainly 99% of the time...
â Richard
Sep 5 at 13:16
2
2
May I know your concern for doing this. do you want to track your script flow while executing?
â msp9011
Sep 5 at 11:05
May I know your concern for doing this. do you want to track your script flow while executing?
â msp9011
Sep 5 at 11:05
Of course : I would like to display the last executed command when there is an error, event if the error was inside a loop of my script...
â Richard
Sep 5 at 12:38
Of course : I would like to display the last executed command when there is an error, event if the error was inside a loop of my script...
â Richard
Sep 5 at 12:38
Because I'm not in development phase anymore and the script is executed every 5 minutes. and I would like to send by e-mail a functional error report, displaying the command that did not work... And this command work certainly 99% of the time...
â Richard
Sep 5 at 13:16
Because I'm not in development phase anymore and the script is executed every 5 minutes. and I would like to send by e-mail a functional error report, displaying the command that did not work... And this command work certainly 99% of the time...
â Richard
Sep 5 at 13:16
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
The "history" is a history of "previously typed" commands.
From the GNU manual:
"When the -o history option to the set builtin is enabled ... the shell provides access to the command history, the list of commands previously typed."
So, it does neither substitute shell variables, nor does it save loop iterations.
For this you could use bash debugging by calling your script so:
bash -x myscript > to-my-log-file
Then, in case of a crash, you can see what happened in the log file (provided the system had had time to flush the output buffer).
ok, thank you for your answer. It was also my first analysis, but I asked the question in case someone had a workaround...
â Richard
Sep 5 at 17:45
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
The "history" is a history of "previously typed" commands.
From the GNU manual:
"When the -o history option to the set builtin is enabled ... the shell provides access to the command history, the list of commands previously typed."
So, it does neither substitute shell variables, nor does it save loop iterations.
For this you could use bash debugging by calling your script so:
bash -x myscript > to-my-log-file
Then, in case of a crash, you can see what happened in the log file (provided the system had had time to flush the output buffer).
ok, thank you for your answer. It was also my first analysis, but I asked the question in case someone had a workaround...
â Richard
Sep 5 at 17:45
add a comment |Â
up vote
1
down vote
The "history" is a history of "previously typed" commands.
From the GNU manual:
"When the -o history option to the set builtin is enabled ... the shell provides access to the command history, the list of commands previously typed."
So, it does neither substitute shell variables, nor does it save loop iterations.
For this you could use bash debugging by calling your script so:
bash -x myscript > to-my-log-file
Then, in case of a crash, you can see what happened in the log file (provided the system had had time to flush the output buffer).
ok, thank you for your answer. It was also my first analysis, but I asked the question in case someone had a workaround...
â Richard
Sep 5 at 17:45
add a comment |Â
up vote
1
down vote
up vote
1
down vote
The "history" is a history of "previously typed" commands.
From the GNU manual:
"When the -o history option to the set builtin is enabled ... the shell provides access to the command history, the list of commands previously typed."
So, it does neither substitute shell variables, nor does it save loop iterations.
For this you could use bash debugging by calling your script so:
bash -x myscript > to-my-log-file
Then, in case of a crash, you can see what happened in the log file (provided the system had had time to flush the output buffer).
The "history" is a history of "previously typed" commands.
From the GNU manual:
"When the -o history option to the set builtin is enabled ... the shell provides access to the command history, the list of commands previously typed."
So, it does neither substitute shell variables, nor does it save loop iterations.
For this you could use bash debugging by calling your script so:
bash -x myscript > to-my-log-file
Then, in case of a crash, you can see what happened in the log file (provided the system had had time to flush the output buffer).
answered Sep 5 at 15:19
nst0022
1062
1062
ok, thank you for your answer. It was also my first analysis, but I asked the question in case someone had a workaround...
â Richard
Sep 5 at 17:45
add a comment |Â
ok, thank you for your answer. It was also my first analysis, but I asked the question in case someone had a workaround...
â Richard
Sep 5 at 17:45
ok, thank you for your answer. It was also my first analysis, but I asked the question in case someone had a workaround...
â Richard
Sep 5 at 17:45
ok, thank you for your answer. It was also my first analysis, but I asked the question in case someone had a workaround...
â Richard
Sep 5 at 17:45
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466987%2fhistory-inside-a-bash-loop%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
May I know your concern for doing this. do you want to track your script flow while executing?
â msp9011
Sep 5 at 11:05
Of course : I would like to display the last executed command when there is an error, event if the error was inside a loop of my script...
â Richard
Sep 5 at 12:38
Because I'm not in development phase anymore and the script is executed every 5 minutes. and I would like to send by e-mail a functional error report, displaying the command that did not work... And this command work certainly 99% of the time...
â Richard
Sep 5 at 13:16