LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-17-2006, 02:29 AM   #1
sharonenoch
LQ Newbie
 
Registered: Jan 2006
Location: Chennai, India
Distribution: Fedora Core 3 & 4
Posts: 19

Rep: Reputation: 0
history command from script


How do i make the history command to run from the script
I also want to redirect the output of the history command to a file

history >> /tmp/history.txt

when executed in a script is not working. when i checked out the net it said that history command will not work in the script

is there any mechanism by which i can get the history command to execute in a script?
 
Old 03-17-2006, 02:55 AM   #2
zeitounator
Member
 
Registered: Aug 2003
Location: Montpellier, France, Europe, World, Solar System
Distribution: Debian Sarge, Fedora core 5 (i386 and x86_64)
Posts: 262

Rep: Reputation: 30
Is there a particular reason why you don't use directly the history file /home/someuser/.bash_history ?

It will be available from any script for any user.
 
Old 03-17-2006, 03:13 AM   #3
sharonenoch
LQ Newbie
 
Registered: Jan 2006
Location: Chennai, India
Distribution: Fedora Core 3 & 4
Posts: 19

Original Poster
Rep: Reputation: 0
[root@cluster74 AmiNas]# history | wc -l
620
[root@cluster74 AmiNas]# cat /root/.bash_history | wc -l
559

This is the output i got from both. There is some difference between them... why is this?
 
Old 03-17-2006, 03:49 AM   #4
zeitounator
Member
 
Registered: Aug 2003
Location: Montpellier, France, Europe, World, Solar System
Distribution: Debian Sarge, Fedora core 5 (i386 and x86_64)
Posts: 262

Rep: Reputation: 30
After some searching in the bash man page (short explanation, have a look at the man page for options and env your can set to modify this behaviour)....
1- When you start a shell, the history is initialised from the history file
2- While you run the shell, the history is stored in a readline buffer specific to this shell (this is why you can have different histories in different buffers)
3- When the shell exits, the new commands in the history buffer are appended to the history file

Knowing this, I don't see how you can manipulate or even store the current shell history from a script. The only solution I can think of is to force the current shell to dump new commands to the history file before you run your script that will manipulate the history file but this requires some action in the current shell before launching the script. See options to history command in the bash man page for this.

Good luck.
 
Old 03-17-2006, 07:29 AM   #5
sharonenoch
LQ Newbie
 
Registered: Jan 2006
Location: Chennai, India
Distribution: Fedora Core 3 & 4
Posts: 19

Original Poster
Rep: Reputation: 0
i could get the output of the history command by running the script file in this manner
"./test.sh"

instead of "sh test.sh"
 
Old 03-17-2006, 07:48 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
have you tried the 'script' command?
maybe it's what you need?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
History command amer_58 Linux - General 5 06-24-2005 12:26 PM
history clear command suguname Linux - Newbie 5 03-05-2005 09:32 AM
Command history? King4lex Linux - Newbie 4 09-03-2004 08:35 PM
history with command line compu73rg33k Linux - Software 3 02-16-2004 03:36 AM
command history log??? granny Linux - Newbie 3 02-11-2003 04:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration