If you are on a POSIX compliant machine (OSX or Linux) you can make use of Shell Scripts to do day-to-day donkey work. I use Shell Scripts for backups, cleaning out temporary folders, profiling and logging. Writing these scripts is pretty straightforward and if you are completely new to it I recommend getting a copy of Classic Shell Scripting.
Recently I came across a couple of shortcuts that have greatly reduced the time it takes to write a script in the vi editor.
Using :! from within vi you can run a shell command. This is great as you don’t need to exit vi to test your code. Let’s say for example your script is called myscript.sh. You can run your script from within the vi editor by using.
Furthermore you can make more changes in vi, save the file and then run the script again by using
This is a shortcut to the last command issued to the shell, allowing you to retest your shell script without even typing the filename.
Within vi the % variable refers to the file that you are currently editing. So to run the script you are editing from within vi you can also use:
Have an update or suggestion for this article? You can edit it here and send me a pull request.
Using HashiCorp Vault with LDAP
How to use HashiCorp Vault to setup an LDAP backed secret store with read-only access for users in groups and read-write access for specific users
Linux and Unix xargs command tutorial with examples
Tutorial on using xargs, a UNIX and Linux command for building and executing command lines from standard input. Examples of cutting by character, byte position, cutting based on delimiter and how to modify the output delimiter.
Copy a file in Go
How to copy a file in Go. The ioutil package does not offer a shorthand way of copying a file. Instead the os package should be used.