Most viewed Bash recipes http://code.activestate.com/recipes/langs/bash/views/2014-12-19T20:01:30-08:00ActiveState Code RecipesBash script to create a header for Bash scripts (Bash)
2011-11-02T01:57:07-07:00userendhttp://code.activestate.com/recipes/users/4179007/http://code.activestate.com/recipes/577862-bash-script-to-create-a-header-for-bash-scripts/
<p style="color: grey">
Bash
recipe 577862
by <a href="/recipes/users/4179007/">userend</a>
(<a href="/recipes/tags/auto/">auto</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/create/">create</a>, <a href="/recipes/tags/emacs/">emacs</a>, <a href="/recipes/tags/gpl/">gpl</a>, <a href="/recipes/tags/header/">header</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/script/">script</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/vim/">vim</a>).
Revision 3.
</p>
<p>This will create a header for a Bash script. It is a nice way keep a track of what your script does and when it was created, the author of the script, etc.. </p>
<p>It will open the script automatically with one of the two most popular editor out there, Vim or Emacs! It also checks to see if there is a script with the same name in the current working directory so it will not overwrite another file.</p>
<p>v0.4: I had to kick this up a notch. I took the suggestion of "dev h" to add a chance for the user to select another name for the script.</p>
<p>Please leave comments and suggestions.</p>
Obfuscation In Bash Shell. (Bash)
2014-12-19T20:01:30-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578986-obfuscation-in-bash-shell/
<p style="color: grey">
Bash
recipe 578986
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/obfuscation/">obfuscation</a>).
</p>
<p>IMO, the immense power of the shell...</p>
<p>Please let me know if there is any other human readable language that can do this...</p>
<p>The DEMO code below was an idea I formed to see how to make a bash script very difficult to hack.</p>
<p>Everything in it is made easy to read so as to see this idea working.</p>
<p>It uses bash variables ONLY and although I have used bash loops to create the variables in this
DEMO you could create your own set of variables and 'source' them to the the obfuscated code before
running the main body of the code.</p>
<p>It also goes without saying that you could obfuscate the changing of any or all the variable
allocations at any time AFTER the code runs to make it even more obfuscated and as may times as
you wish...</p>
<p>I would be seriously difficult to actually write a lsrge bash app' using this method but boy oh boy
would it be fun?!?</p>
<p>Testbed:- Macbook Pro, OSX 10.7.x and above, using default bash terminal...</p>
<p>LBNL, yeah I am aware of 'eval' but as it is obfuscated and can have as many obfuscated variables as
I wish allocated to it then why worry... ;o)</p>
<p>Enjoy finding simple solutions to often very difficult problems...</p>
<p>Bazza...</p>
Basic Linux Menu (Bash)
2010-10-23T05:40:18-07:00Jonathan Fenechhttp://code.activestate.com/recipes/users/4169413/http://code.activestate.com/recipes/577437-basic-linux-menu/
<p style="color: grey">
Bash
recipe 577437
by <a href="/recipes/users/4169413/">Jonathan Fenech</a>
(<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/basic/">basic</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/menu/">menu</a>).
</p>
<p>A basic Linux menu which can do the following: </p>
<ul>
<li>Display Files and Directory's</li>
<li>Remove Files Displayed</li>
<li>Copy Files Displayed</li>
<li>Make Directory</li>
</ul>
How to detect the Linux distribution from an init.d script (Bash)
2010-03-16T13:24:22-07:00Gui Rhttp://code.activestate.com/recipes/users/4166241/http://code.activestate.com/recipes/576676-how-to-detect-the-linux-distribution-from-an-initd/
<p style="color: grey">
Bash
recipe 576676
by <a href="/recipes/users/4166241/">Gui R</a>
(<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/redhat/">redhat</a>).
Revision 2.
</p>
<p>There is no trivial way to know what Linux you are running. Red Hat, SuSE, etc., each distribution has a different way to tell what version is installed.</p>
A Shell, Binary To Hexadecimal To Decimal Demo... (Bash)
2013-01-11T18:31:03-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578413-a-shell-binary-to-hexadecimal-to-decimal-demo/
<p style="color: grey">
Bash
recipe 578413
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/hexadecimal/">hexadecimal</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/terminal/">terminal</a>).
</p>
<p>This little DEMO may be common knowledge to the big guns but not to amateurs like myself, so......</p>
<p>This is an Apple Macbbok Shell/Terminal DEMO shell script to show how to:-</p>
<p>1) Create a binary file...
2) Save it to your DEFAULT /directory/drwawer/folder/...
3) Display a hexadecimal dump of said binary file to prove that it is binary...
4) Select a single BYTE of that file and save it as an ASCII text decimal _number_, also to your DEFAULT /directory/drawer/folder/...
5) Read this ASCII text decimal number back in again...
6) Add this string representation to a number...
7) Stop...</p>
<p>It was intended purely for OSX 10.7.5 and above using the default terminal and shell...</p>
<p>It does work on many Linux flavours and shells/terminals also however.</p>
<p>Written so the anyone can understand what is going on.</p>
<p>The two files generated and saved in this DEMO to your DEFAULT /directory/drawer/folder/ are:-</p>
<p>BinaryString.dat
BinaryString.txt</p>
<p>This WILL lead to something very unusual in the not too distant future...</p>
<p>This is Public Domain and you may do with it as you wish...</p>
<p>Enjoy finding simple solutions to often very difficult problems...</p>
<p>Bazza, G0LCU...</p>
Remove UTF-8 Byte Order Mark (BOM) from text files (Bash)
2011-10-18T06:37:52-07:00Graham Poulterhttp://code.activestate.com/recipes/users/4172291/http://code.activestate.com/recipes/577912-remove-utf-8-byte-order-mark-bom-from-text-files/
<p style="color: grey">
Bash
recipe 577912
by <a href="/recipes/users/4172291/">Graham Poulter</a>
(<a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/unicode/">unicode</a>, <a href="/recipes/tags/utf8/">utf8</a>).
</p>
<p>Shell script to removes UTF-8 Byte Order Mark (BOM) from text files, where present.</p>
A Simple Clock, Well Maybe Not That Simple... (Bash)
2013-07-17T17:40:18-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578610-a-simple-clock-well-maybe-not-that-simple/
<p style="color: grey">
Bash
recipe 578610
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/clock/">clock</a>, <a href="/recipes/tags/digital/">digital</a>, <a href="/recipes/tags/digits/">digits</a>, <a href="/recipes/tags/display/">display</a>, <a href="/recipes/tags/large/">large</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/readout/">readout</a>, <a href="/recipes/tags/terminal/">terminal</a>).
</p>
<p>Hi all...</p>
<p>Do you remember this little snippet?</p>
<p><a href="http://code.activestate.com/recipes/578079-pure-fun-for-text-mode-python/?in=user-4177147" rel="nofollow">http://code.activestate.com/recipes/578079-pure-fun-for-text-mode-python/?in=user-4177147</a></p>
<p>Well it was voted down, but who cares? I surely don't!
So to those that voted this FUN piece of Python coding down, you are now about to see what
the aim was.</p>
<p>However although I have subsequently done what I said I would, that is, to create large digits
as an _at_a_glance_ digital readout for a project in Python I have decided not to upload it.
However I have decided to let this one go as a bash/shell script instead.</p>
<p>It is a nothing but a simple clock, well, maybe not that simple; but as I am into shell
stuff at the moment I thought I would share this with you.</p>
<p>The terminal does have its colours changed and the cursor disabled for the session but
I expect you big guns to be able to return back to normal in a jiffy.</p>
<p>I am an amateur coder and if that is easy for me then you pros should have no problems.</p>
<p>This code is not like most of my other stuff as is NOT Public Domain...</p>
<p>It is only a DEMO to see what an at a glance display would look like in a terminal.</p>
<p>Move away to a distance and see which of the two time readings you can still read... ;o)</p>
<p>It is for a Macbook Pro 13" OSX 10.7.5 and uses bash imode.
It will probably work on most Linux machines too but I haven't tested it...</p>
<p>This is purely a DEMO only and any other special effects, (e.g. flashing colon every second),
alram, etc, I have already experimented with and is easy enough to do...</p>
<p>My intention is to use this as a kids level text mode digital voltmeter I am doing.</p>
<p>Enjoy...</p>
<p>Bazza...</p>
Bash Prompt Rainbow Color Chart (Bash)
2011-10-02T21:39:21-07:00userendhttp://code.activestate.com/recipes/users/4179007/http://code.activestate.com/recipes/577889-bash-prompt-rainbow-color-chart/
<p style="color: grey">
Bash
recipe 577889
by <a href="/recipes/users/4179007/">userend</a>
(<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/prompt/">prompt</a>, <a href="/recipes/tags/ps1/">ps1</a>, <a href="/recipes/tags/rainbow/">rainbow</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/terminal/">terminal</a>).
</p>
<p>This is a simple loop that displays a rainbow of different colors.</p>
awk sample (Bash)
2010-03-16T13:24:00-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576876-awk-sample/
<p style="color: grey">
Bash
recipe 576876
by <a href="/recipes/users/4170398/">J Y</a>
(<a href="/recipes/tags/awk/">awk</a>).
</p>
<p>this is an awk sample</p>
Simple Bash Text Mode Sine Curve Generator. (Bash)
2014-08-12T20:57:39-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578921-simple-bash-text-mode-sine-curve-generator/
<p style="color: grey">
Bash
recipe 578921
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/cygwin/">cygwin</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/text/">text</a>).
Revision 2.
</p>
<p>This bash script is a taster for a kids level, audio, text mode, sweep generator.
The code just creates a single cycle of a quantised sine curve inside an 80 x 24 bash terminal.
This will be the calculator for a sinewave sweep generator from about 50Hz the 12KHz...
The code tells you more and the display is in comments at the end...</p>
A Building Block, Bash Binary File Manipulation... (Bash)
2013-01-29T22:07:57-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578441-a-building-block-bash-binary-file-manipulation/
<p style="color: grey">
Bash
recipe 578441
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/script/">script</a>, <a href="/recipes/tags/shell/">shell</a>).
Revision 2.
</p>
<p>Apologies for any typos, and IF this has been done before...</p>
<p>The code generates a 256 byte binary file of _characters_ 0x00 to 0xFF for general usage and generates another binary file manipulated in a basic way.</p>
<p>The for loops in the code are purely for DEMO purposes only.</p>
<p>This is Public Domain and you may do with it as you please. I have uploaded it elsewhere too...</p>
<p>Watch for wordwrapping, etc and read the code for more information...</p>
<p>Enjoy finding simple solutions to often very difficult problems...</p>
<p>Bazza, G0LCU...</p>
Print selected text pages to PDF with Python, selpg and xtopdf on Linux (Bash)
2014-10-29T17:38:10-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578954-print-selected-text-pages-to-pdf-with-python-selpg/
<p style="color: grey">
Bash
recipe 578954
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/reportlab/">reportlab</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/text_files/">text_files</a>, <a href="/recipes/tags/text_processing/">text_processing</a>, <a href="/recipes/tags/unix/">unix</a>).
</p>
<p>This recipe shows how to use selpg, a Linux command-line utility written in C, together with xtopdf, a Python toolkit for PDF creation, to print only a selected range of pages from a text file, to a PDF file, for display or print purposes. The way to do this is to run the selpg utility at the Linux command line, with options specifying the start and end pages of the range, and pipe its output to the StdinToPDF.py program, which is a part of the xtopdf toolkit.</p>
Parallelize your shell commands (Bash)
2010-04-02T00:14:38-07:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577171-parallelize-your-shell-commands/
<p style="color: grey">
Bash
recipe 577171
by <a href="/recipes/users/4173535/">Kevin L. Sitze</a>
(<a href="/recipes/tags/fifo/">fifo</a>, <a href="/recipes/tags/multiprocessing/">multiprocessing</a>, <a href="/recipes/tags/parallel/">parallel</a>).
Revision 2.
</p>
<p>This script is used to processes a batch job of commands in parallel. The script dispatches new commands up to a user specified limit, each generated from a template provided on the command line using arguments taken from STDIN. The basic combining semantics are similar to "xargs -1", though support for multiple arguments and parallel processing of commands are provided.</p>
hexadecimal CSS colors, convert the six-digit to the three-digit notation (Bash)
2010-10-02T14:07:23-07:00Romain Dartigueshttp://code.activestate.com/recipes/users/4167472/http://code.activestate.com/recipes/577416-hexadecimal-css-colors-convert-the-six-digit-to-th/
<p style="color: grey">
Bash
recipe 577416
by <a href="/recipes/users/4167472/">Romain Dartigues</a>
(<a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/css/">css</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/rgb/">rgb</a>, <a href="/recipes/tags/sed/">sed</a>, <a href="/recipes/tags/shortening/">shortening</a>).
</p>
<p>Want to replace all six-digit RGB notation (#rrggbb) to the short (three-digit #rgb) form?
Or the opposite?</p>
Git Shell Script to enhance inline automation script... (Bash)
2010-11-01T00:43:03-07:00Patrick Riendeauhttp://code.activestate.com/recipes/users/4175653/http://code.activestate.com/recipes/577447-git-shell-script-to-enhance-inline-automation-scri/
<p style="color: grey">
Bash
recipe 577447
by <a href="/recipes/users/4175653/">Patrick Riendeau</a>
(<a href="/recipes/tags/code/">code</a>, <a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/git/">git</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/oriented/">oriented</a>, <a href="/recipes/tags/programs/">programs</a>, <a href="/recipes/tags/property/">property</a>, <a href="/recipes/tags/property_creation/">property_creation</a>, <a href="/recipes/tags/repository/">repository</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/shelve/">shelve</a>).
</p>
<p>This tiny effort, depend from Fnct.D ActiveState no.577446 Core, also available from github <a href="http://github.com/priendeau/Fnct.d" rel="nofollow">http://github.com/priendeau/Fnct.d</a>, can develop basic methodology of implementing oriented program within uses of function re-declaration with function-parser to create both property-function to discover uses of getter-function and setter-function</p>
scan db to login the ssh servers (Bash)
2010-03-16T13:11:17-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576877-scan-db-to-login-the-ssh-servers/
<p style="color: grey">
Bash
recipe 576877
by <a href="/recipes/users/4170398/">J Y</a>
(<a href="/recipes/tags/awk/">awk</a>, <a href="/recipes/tags/bash/">bash</a>).
Revision 2.
</p>
<p>awk with parameters passed from bash</p>
A simple shell script to keep the wife off of your back... (Bash)
2013-12-09T20:05:49-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578781-a-simple-shell-script-to-keep-the-wife-off-of-your/
<p style="color: grey">
Bash
recipe 578781
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/reminder/">reminder</a>, <a href="/recipes/tags/script/">script</a>, <a href="/recipes/tags/shell/">shell</a>).
</p>
<p>How many times have you been asked to remember to do something from the other half whilst she is out for a short while.</p>
<p>For example: "You WILL check the dinner every few minutes won't you?"</p>
<p>And how many times did/do you forget?</p>
<p>Most of us have been there...</p>
<p>This is a simple kids level, practical learning, shell script that generates an "xterm" with your reminder inside every 30 seconds for a period of 3 seconds.</p>
<p>It is always be the active front window for 3 seconds at a time to _annoy_ you into remembering.</p>
<p>Usage: reminder "What you have to remember here using spaces AND double quotes."<CR></p>
<p>Just reanme the downloaded script to reminder and remember to chmod it as required.</p>
<p>Just run it from your default terminal and when finished press Ctrl-C just AFTER the xterm window closes.</p>
<p>There is NO error detection so steer clear of any special characters in you reminder text.</p>
<p>Enjoy finding simple solutions to often very difficult problems...</p>
A Bash Beep Command For OSX 10.7+... (Bash)
2014-02-27T19:36:17-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578837-a-bash-beep-command-for-osx-107/
<p style="color: grey">
Bash
recipe 578837
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/beep/">beep</a>, <a href="/recipes/tags/error_beep/">error_beep</a>, <a href="/recipes/tags/error_sound/">error_sound</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/sound/">sound</a>).
</p>
<p>This small bash script generates an 8044 byte 1KHz sinewave wave file and immediately plays it.
The file created is a _pure_ sinewave and lasts for 1 second. It uses the default "afplay"
command to run the generated file.</p>
<p>It was designed around an Apple Macbook Pro but using "aplay" it might even work on other *nix
flavours from the command line. I have not bothered to try it as this was purely for my MB Pro.</p>
<p>The wave file can be found as "/tmp/sinewave.wav" during the working session(s) and can be saved
anywhere of your choice.</p>
<p>Enjoy...</p>
<p>(Watch for word wrapping etc...)</p>
<p>Bazza.</p>
find + grep (Bash)
2013-09-17T08:48:07-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578661-find-grep/
<p style="color: grey">
Bash
recipe 578661
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/zsh/">zsh</a>).
Revision 2.
</p>
<p>look for a text pattern in files defined by a pattern.
First argument is passed to the find command, second one to the grep</p>
<p>./find_n_grep.sh '*.py' 'dict'</p>
<p>more over, it sort by date.</p>
DEMO - Generate A Crude 1KHz Sinewave Using A BASH Script. (Bash)
2013-03-01T19:41:47-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578477-demo-generate-a-crude-1khz-sinewave-using-a-bash-s/
<p style="color: grey">
Bash
recipe 578477
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/sinewave/">sinewave</a>, <a href="/recipes/tags/sound/">sound</a>).
</p>
<p>A very simple crude sinewave generator using a BASH script inside a Linux Terminal.</p>
<p>The file required is generated inside the code and requires /dev/audio to work.</p>
<p>Ensure you have this device, if not the download oss-compat from your OS's repository...</p>
<p>It lasts for about 8 seconds before exiting and saves a 65536 byte file to your working directory/drawer/folder as sinewave.raw.</p>
<p>Use an oscilloscope to check the waveform generated...</p>
<p>It is entirely Public Domain and you may do with it as you please...</p>
<p>Enjoy finding simple solutions to often very difficult problems... ;o)</p>
<p>Bazza, G0LCU...</p>