

Create Script from Bash History
source link: https://www.devdungeon.com/content/create-script-bash-history
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Create Script from Bash History
Submitted by NanoDano on Wed, 11/21/2012 - 11:04
The script program (bsdutils) will output the whole terminal output to a file. This can be helpful for logging and creating scripts out of history.
The approach here is to output the command history to a file and then manipulate the file a bit to get what we want.
history 3 > script.sh
This will output the last 3 commands to script.sh. Note that the actual history command will become the most recent event in history, and will be output in the file. This can be edited out manually or using this command:
head -n -1 script.sh > script.sh.tmp && mv script.sh{.tmp,}
The head command will remove the last line and update the file. The last step is to remove the line numbers from the beginning of the lines. Do that with sed:
sed -i "s/^ *[0-9]* //g" script.sh
Finally, ensure script.sh is executable with:
chmod +x script.sh
The file could be tweaked to include the shebang #!/bin/bash and make sure there are no extra white spaces in the file. These commands could all be packaged together in their own script in order to easily create scripts from history. An example usage could be:
hist2script 4 myscript.sh
Advertisement
Recommend
-
14
Bash script to perform super-fast file transfer using netcat and tar Aug 5, 2013 Updated 06-Aug-2013 : Script updated to transfer multiple files in single command [
-
14
Bash scripts. Almost anyone needs to write one sooner or later. Almost no one says “yeah, I love writing them”. And that’s why almost everyone is putting low attention while writing them. I won’t try to make you a Bash expert (since...
-
20
Send mail with a BASH Shell Script Posted: 2007-06-10 - Last updated: 2019-06-05 Tagged
-
11
Bash Script 的好習慣這篇給了一份 bash script 用的 tempalte,但更重要的反而是裡面提到的 best practice:「Minimal safe Bash script template」。首先是不...
-
14
How to write a backup script with Bash Rob Marti Feb 26, 2021 8 Minute Read Linux System...
-
17
Vim bash-completion This repository contains a bash-completion file for vim. The following features are curently supported: auto completion of remote files (vim scp://$host//files) basic argum...
-
11
如何编写bash completion script 创建静态补全很简单,只需要使用 complete 的 -W (wordlist) 选项为脚本注册补全的单词列表就行了。 比如,假设有一个 myscript 脚本,可以接参数...
-
6
Running a Custom Bash Script in fastlane Fastlane is a mobile application automation tool. It furnishes an extensive library of actions that aids you in packaging up...
-
6
Simplify a BASH script design advertisements I have need to execute a command in a script an arbitrary number of times with associated arbitra...
-
5
How I set up my new Next.js projects with a handy bash script Jul 9 Originally published at
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK