Write a Bash Script For Everything
22 April, 2014
Recently I've been writing a ton of Bash Scripts to do the tedious and monotonous tasks that development can sometimes require. For instance there is a weird bug that I encounter when debugging and Android app on a physical device.
The problem seems to be with a program called EasyTetherUSBEthernet
, which will interfere with the Android debugger (ADB). The problem is well documented in StackOverflow posts. This simple little bash script will refresh that program, and restart the debugger server.
#!/bin/bash
# Andrew Gable
# March 2014
#echo password needed
echo -e "Password needed for ktext loading. . . \n"
#load ktext
sudo kextload -v /System/Library/Extensions/EasyTetherUSBEthernet.kext
#sleep just in case
sleep 1
echo -e "ktext unloading \n"
#unload ktext
sudo kextunload -v /System/Library/Extensions/EasyTetherUSBEthernet.kext
#sleep just in case
sleep 1
echo -e "killing adb server \n"
#kill server
adb kill-server
#sleep in just case
sleep 1
echo -e "starting adb server \n"
#start the server back up
adb devices
Once I figured out the power and speed of a bash script I started writing them for everything.
Here is a simple script that logs into an instance of Amazon Web Services through SSH in a new tab (Most likely will only run on OSX). If you want to use this script just make sure to change the path to your .pem
file and the user@host
to your AWS instance.
#!/bin/bash
#Create a new tab & Login to aws using ssh & correct file
osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' -e 'tell application "Terminal" to do script "ssh -i /LOCATION/TO/FILE.pem user@HOST" in selected tab of the front window'
The last script I will show you is a work in progress that I am continuing to update daily. I run this script at work as soon as I log in. It updates the latest svn directory it is pointed to if I don't have tomcat
open, if tomcat
is open then I shut it down and do the update. After a svn update I open Chrome to Gmail.
#!/bin/bash
# Andrew Gable
# April 2014
# Script to check svn, and start the day
echo "Goodmorning Andrew!"
read -p "Andrew, is tomcat off? (y/n)" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
# If reply is yes then do update
svn update /Users/user1/Documents/sts/eps
else
# If reply is no then shutoff tomcat, then update
/Applications/tomcat/bin/shutdown.sh
svn update /Users/user1/Documents/sts/eps
fi
# Open Gmail
echo "Opening Gmail. . . "
open -a /Applications/Google\ Chrome.app https://mail.google.com/mail
echo "Have a good day!"
exit 0
You can see the possibilities for scripting are absolutely endless for scripting. All the seconds it saves you doing the boring fixes, logins, and checks are a life saver. So save a some time, and write a bash script.