FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 02-24-2012, 11:12 AM
Johnny Rosenberg
 
Default Using history command in bash script

Seems not to be possible.

~$ cat > pa
#!/bin/bash

history
history
history
<Ctrl+d>
~$ chmod +x pa
~$ pa
~$

So there seem to be no history entries available when I run history
from a bash script. Why is this?

I also tried to use the ~/.bash_history file, but it doesn't seem like
events are added to it for each command executed, since at least a
couple of ten commands are missing at the end. Seems like things are
added to the .bash_history file ”now and then”, I'm not sure how
often. How can I get around this? Where are the latest history lines
recorded before they are added to the actual .bash_history file?

I'm trying to make a bash script that creates an alias for my last
command and save it in my .bash-aliases file (which is launched from
the .bashrc file), so I need some way to know what my latest command
was.


Kind regards

Johnny Rosenberg
ジョニー・*ーゼンバーグ

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-24-2012, 11:15 AM
Alan Pope
 
Default Using history command in bash script

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 24/02/12 12:12, Johnny Rosenberg wrote:

> I'm trying to make a bash script that creates an alias for my last
> command and save it in my .bash-aliases file (which is launched
> from the .bashrc file), so I need some way to know what my latest
> command was.
>

You may not know but "!!" already is an alias for the last command:-

alan@deep-thought:~$ cowsay moo
_____
< moo >
-----
^__^
(oo)\_______
(__) )/
||----w |
|| ||
alan@deep-thought:~$ !!
cowsay moo
_____
< moo >
-----
^__^
(oo)\_______
(__) )/
||----w |
|| ||

Cheers,
- --
Alan Pope
Engineering Manager

Canonical - Product Strategy
+44 (0) 7973 620 164
alan.pope@canonical.com
http://ubuntu.com/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJPR39QAAoJEMx6UFtfvV4wBAwH/05ziK+8RCmvh8z8ic0lpsMz
uIBOUBY6vRVBrt/t8143cF9iPqSyjXtvx/pQynewmWBESir4aZPZWGCtMD9Bmzax
KvgcWRbDdaYJzFTogNH5zedmsQWYCVbslvpeQNPY6DMY3w1M+E CdwBLHnLgP+RZs
Lsn7ECrbCNS7IHznkox5LYRjbONSK7aZGb7HRbUCi9gBtlvgBc xWE219/E0V7kQF
yvNnH318J3nE1lvK43NVQmHWPuYjf4mXKYymTugUVPnRmdK+Ky oDaubTwxzwxTft
gN3YgjpnCgKnttUTWUkbkbnrRZvRtIDjGM5ZGLwePVai3TeiXf jong08pBUktdw=
=osAV
-----END PGP SIGNATURE-----

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-24-2012, 11:56 AM
Johnny Rosenberg
 
Default Using history command in bash script

2012/2/24 Alan Pope <alan.pope@canonical.com>:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 24/02/12 12:12, Johnny Rosenberg wrote:
>
>> I'm trying to make a bash script that creates an alias for my last
>> command and save it in my .bash-aliases file (which is launched
>> from the .bashrc file), so I need some way to know what my latest
>> command was.
>>
>
> You may not know but "!!" already is an alias for the last command:-

Yes, but it doesn't seem like I can use it in a script, can I? At
least I failed.


Kind regards

Johnny Rosenberg
ジョニー・*ーゼンバーグ


>
> alan@deep-thought:~$ cowsay moo
> *_____
> < moo >
> *-----
> * * * * * ^__^
> * * * * *(oo)\_______
> * * * * * *(__) * * * )/
> * * * * * * * *||----w |
> * * * * * * * *|| * * ||
> alan@deep-thought:~$ !!
> cowsay moo
> *_____
> < moo >
> *-----
> * * * * * ^__^
> * * * * *(oo)\_______
> * * * * * *(__) * * * )/
> * * * * * * * *||----w |
> * * * * * * * *|| * * ||
>
> Cheers,
> - --
> Alan Pope
> Engineering Manager
>
> Canonical - Product Strategy
> +44 (0) 7973 620 164
> alan.pope@canonical.com
> http://ubuntu.com/
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.11 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iQEcBAEBAgAGBQJPR39QAAoJEMx6UFtfvV4wBAwH/05ziK+8RCmvh8z8ic0lpsMz
> uIBOUBY6vRVBrt/t8143cF9iPqSyjXtvx/pQynewmWBESir4aZPZWGCtMD9Bmzax
> KvgcWRbDdaYJzFTogNH5zedmsQWYCVbslvpeQNPY6DMY3w1M+E CdwBLHnLgP+RZs
> Lsn7ECrbCNS7IHznkox5LYRjbONSK7aZGb7HRbUCi9gBtlvgBc xWE219/E0V7kQF
> yvNnH318J3nE1lvK43NVQmHWPuYjf4mXKYymTugUVPnRmdK+Ky oDaubTwxzwxTft
> gN3YgjpnCgKnttUTWUkbkbnrRZvRtIDjGM5ZGLwePVai3TeiXf jong08pBUktdw=
> =osAV
> -----END PGP SIGNATURE-----
>
> --
> ubuntu-users mailing list
> ubuntu-users@lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-24-2012, 11:59 AM
Johnny Rosenberg
 
Default Using history command in bash script

Den 24 februari 2012 13:12 skrev Johnny Rosenberg <gurus.knugum@gmail.com>:
> Seems not to be possible.
>
> ~$ cat > pa
> #!/bin/bash
>
> history
> history
> history
> <Ctrl+d>
> ~$ chmod +x pa
> ~$ pa
> ~$
>
> So there seem to be no history entries available when I run history
> from a bash script. Why is this?
>
> I also tried to use the ~/.bash_history file, but it doesn't seem like
> events are added to it for each command executed, since at least a
> couple of ten commands are missing at the end. Seems like things are
> added to the .bash_history file ”now and then”, I'm not sure how
> often. How can I get around this? Where are the latest history lines
> recorded before they are added to the actual .bash_history file?
>
> I'm trying to make a bash script that creates an alias for my last
> command and save it in my .bash-aliases file (which is launched from
> the .bashrc file), so I need some way to know what my latest command
> was.
>
>
> Kind regards
>
> Johnny Rosenberg
> ジョニー・*ーゼンバーグ

About that .bash_history file:
At the moment, my history is 620 entries. If I look in the
.bash_history file, the last command there match entry 527 of the
history command's output. Where are the 93 missing entries? Can I
force bash to write those to the .bash_history file?


Kind regards

Johnny Rosenberg
ジョニー・*ーゼンバーグ

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-24-2012, 12:22 PM
Bert Swart
 
Default Using history command in bash script

On Fri, Feb 24, 2012 at 01:59:39PM +0100, Johnny Rosenberg wrote:
> Den 24 februari 2012 13:12 skrev Johnny Rosenberg <gurus.knugum@gmail.com>:
> > Seems not to be possible.
> >
> > ~$ cat > pa
> > #!/bin/bash
> >
> > history
> > history
> > history
> > <Ctrl+d>
> > ~$ chmod +x pa
> > ~$ pa
> > ~$
> >
> > So there seem to be no history entries available when I run history
> > from a bash script. Why is this?
> >
> > I also tried to use the ~/.bash_history file, but it doesn't seem like
> > events are added to it for each command executed, since at least a
> > couple of ten commands are missing at the end. Seems like things are
> > added to the .bash_history file ”now and then”, I'm not sure how
> > often. How can I get around this? Where are the latest history lines
> > recorded before they are added to the actual .bash_history file?
> >
> > I'm trying to make a bash script that creates an alias for my last
> > command and save it in my .bash-aliases file (which is launched from
> > the .bashrc file), so I need some way to know what my latest command
> > was.
> >
> >
> > Kind regards
> >
> > Johnny Rosenberg
> > ジョニー・*ーゼンバーグ
>
> About that .bash_history file:
> At the moment, my history is 620 entries. If I look in the
> .bash_history file, the last command there match entry 527 of the
> history command's output. Where are the 93 missing entries? Can I
> force bash to write those to the .bash_history file?
Yes, set history -a in your .bashrc
>
>
> Kind regards
>
> Johnny Rosenberg
> ジョニー・*ーゼンバーグ
>
> --
> ubuntu-users mailing list
> ubuntu-users@lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-24-2012, 05:20 PM
Johnny Rosenberg
 
Default Using history command in bash script

2012/2/24 Bert Swart <bertswart@chello.nl>:
> On Fri, Feb 24, 2012 at 01:59:39PM +0100, Johnny Rosenberg wrote:
>> Den 24 februari 2012 13:12 skrev Johnny Rosenberg <gurus.knugum@gmail.com>:
>> > Seems not to be possible.
>> >
>> > ~$ cat > pa
>> > #!/bin/bash
>> >
>> > history
>> > history
>> > history
>> > <Ctrl+d>
>> > ~$ chmod +x pa
>> > ~$ pa
>> > ~$
>> >
>> > So there seem to be no history entries available when I run history
>> > from a bash script. Why is this?
>> >
>> > I also tried to use the ~/.bash_history file, but it doesn't seem like
>> > events are added to it for each command executed, since at least a
>> > couple of ten commands are missing at the end. Seems like things are
>> > added to the .bash_history file ”now and then”, I'm not sure how
>> > often. How can I get around this? Where are the latest history lines
>> > recorded before they are added to the actual .bash_history file?
>> >
>> > I'm trying to make a bash script that creates an alias for my last
>> > command and save it in my .bash-aliases file (which is launched from
>> > the .bashrc file), so I need some way to know what my latest command
>> > was.
>> >
>> >
>> > Kind regards
>> >
>> > Johnny Rosenberg
>> > ジョニー・*ーゼンバーグ
>>
>> About that .bash_history file:
>> At the moment, my history is 620 entries. If I look in the
>> .bash_history file, the last command there match entry 527 of the
>> history command's output. Where are the 93 missing entries? Can I
>> force bash to write those to the .bash_history file?
> Yes, set history -a in your .bashrc

Failed.
All that did was to add entries to the .bash_history file when the
history -a command was run. After that, no more entries are added, at
least not for every time they are executed.

I also tried to run history -a from my shell script, but history seems
to be blocked in a bash script.



Kind regards

Johnny Rosenberg
ジョニー・*ーゼンバーグ

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-25-2012, 06:01 PM
Bert Swart
 
Default Using history command in bash script

On Fri, Feb 24, 2012 at 07:20:20PM +0100, Johnny Rosenberg wrote:
> 2012/2/24 Bert Swart <bertswart@chello.nl>:
> > On Fri, Feb 24, 2012 at 01:59:39PM +0100, Johnny Rosenberg wrote:
> >> Den 24 februari 2012 13:12 skrev Johnny Rosenberg <gurus.knugum@gmail.com>:
> >> > Seems not to be possible.
> >> >
> >> > ~$ cat > pa
> >> > #!/bin/bash
> >> >
> >> > history
> >> > history
> >> > history
> >> > <Ctrl+d>
> >> > ~$ chmod +x pa
> >> > ~$ pa
> >> > ~$
> >> >
> >> > So there seem to be no history entries available when I run history
> >> > from a bash script. Why is this?
> >> >
> >> > I also tried to use the ~/.bash_history file, but it doesn't seem like
> >> > events are added to it for each command executed, since at least a
> >> > couple of ten commands are missing at the end. Seems like things are
> >> > added to the .bash_history file ”now and then”, I'm not sure how
> >> > often. How can I get around this? Where are the latest history lines
> >> > recorded before they are added to the actual .bash_history file?
> >> >
> >> > I'm trying to make a bash script that creates an alias for my last
> >> > command and save it in my .bash-aliases file (which is launched from
> >> > the .bashrc file), so I need some way to know what my latest command
> >> > was.
> >> >
> >> >
> >> > Kind regards
> >> >
> >> > Johnny Rosenberg
> >> > ジョニー・*ーゼンバーグ
> >>
> >> About that .bash_history file:
> >> At the moment, my history is 620 entries. If I look in the
> >> .bash_history file, the last command there match entry 527 of the
> >> history command's output. Where are the 93 missing entries? Can I
> >> force bash to write those to the .bash_history file?
> > Yes, set history -a in your .bashrc
>
> Failed.
Sorry, my mistake. Try adding to .bashrc:
shopt -s histappend
PROMPT_COMMAND='history -a'

First command appends history instead of overwriting, second should write
history after each command. Untested, I don't use it > All that did was to add
entries to the .bash_history file when the
> history -a command was run. After that, no more entries are added, at
> least not for every time they are executed.
>
> I also tried to run history -a from my shell script, but history seems
> to be blocked in a bash script.
Correct, history can only be run from interactive shells. On non-interactive
shell, such as your script, history doesn't return anything.
>
>
> Kind regards
>
> Johnny Rosenberg
> ジョニー・*ーゼンバーグ
>
> --
> ubuntu-users mailing list
> ubuntu-users@lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-26-2012, 12:27 AM
Johnny Rosenberg
 
Default Using history command in bash script

2012/2/25 Bert Swart <bertswart@chello.nl>:
> On Fri, Feb 24, 2012 at 07:20:20PM +0100, Johnny Rosenberg wrote:
>> 2012/2/24 Bert Swart <bertswart@chello.nl>:
>> > On Fri, Feb 24, 2012 at 01:59:39PM +0100, Johnny Rosenberg wrote:
>> >> Den 24 februari 2012 13:12 skrev Johnny Rosenberg <gurus.knugum@gmail.com>:
>> >> > Seems not to be possible.
>> >> >
>> >> > ~$ cat > pa
>> >> > #!/bin/bash
>> >> >
>> >> > history
>> >> > history
>> >> > history
>> >> > <Ctrl+d>
>> >> > ~$ chmod +x pa
>> >> > ~$ pa
>> >> > ~$
>> >> >
>> >> > So there seem to be no history entries available when I run history
>> >> > from a bash script. Why is this?
>> >> >
>> >> > I also tried to use the ~/.bash_history file, but it doesn't seem like
>> >> > events are added to it for each command executed, since at least a
>> >> > couple of ten commands are missing at the end. Seems like things are
>> >> > added to the .bash_history file ”now and then”, I'm not sure how
>> >> > often. How can I get around this? Where are the latest history lines
>> >> > recorded before they are added to the actual .bash_history file?
>> >> >
>> >> > I'm trying to make a bash script that creates an alias for my last
>> >> > command and save it in my .bash-aliases file (which is launched from
>> >> > the .bashrc file), so I need some way to know what my latest command
>> >> > was.
>> >> >
>> >> >
>> >> > Kind regards
>> >> >
>> >> > Johnny Rosenberg
>> >> > ジョニー・*ーゼンバーグ
>> >>
>> >> About that .bash_history file:
>> >> At the moment, my history is 620 entries. If I look in the
>> >> .bash_history file, the last command there match entry 527 of the
>> >> history command's output. Where are the 93 missing entries? Can I
>> >> force bash to write those to the .bash_history file?
>> > Yes, set history -a in your .bashrc
>>
>> Failed.
> Sorry, my mistake. Try adding to .bashrc:
> shopt -s histappend
> PROMPT_COMMAND='history -a'
>
> First command appends history instead of overwriting, second should write
> history after each command. Untested, I don't use it

Thanks, I'll try that tomorrow.

>> All that did was to add
>> entries to the .bash_history file when the
>> history -a command was run. After that, no more entries are added, at
>> least not for every time they are executed.
>>
>> I also tried to run history -a from my shell script, but history seems
>> to be blocked in a bash script.

> Correct, history can only be run from interactive shells. On non-interactive
> shell, such as your script, history doesn't return anything.

I wonder why. Can't see an obvious reason for that.


Kind regards

Johnny Rosenberg
ジョニー・*ーゼンバーグ

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 

Thread Tools




All times are GMT. The time now is 09:47 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org