Bash string manipulation When I write bash scripts in my terminal, I often need to manipulate strings. Unfortunately, I often forget how to do this properly in bash, so I thought I’d write a blog article for me to remember better in the future. Hopefully it will be helpful for some of you developers out there as well. String manipulation in bash is not hard, but I find some of the notation a bit cumbersome especially when normally working more with Python or other languages.
Every developer needs access to some servers for example to check the application logs. Usually, this is done using public-private key encryption where each developer generates their own public-private key pair. The public keys of each developer are added to the authorized_keys file on each server they should have access to. Painful manual changes So far so good. However, what happens when one developer leaves the company? In that case, the public keys of that developer should be removed from all servers.
When you are prototyping and developing small scripts that you keep running, it might be annoying that they quit when an error occurs. If you want very basic robustness against these crashes, you can at least use a bash script to automatically restart your script on error. The tool to use here is called until and makes this a breeze. Let’s use a dumb example Python script called test.py: import time while True: print('Looping') time.
Python is a great language to build command-line tools in as it’s very expressive and concise. You want to have the ability to parse arguments in your scripts as you don’t want to hard-code the relevant variable values to make the tool useful. So how do we go about this in Python? It’s easily done using the argparse module. With argparse, you define the different arguments which you expect, their default values and their shortcuts to call them.
Follow me on twitter! Follow @mpaepper