Created
April 1, 2016 17:34
-
-
Save hughdunne/d7e4b0b22e1c757200e0f7ae74f6a98e to your computer and use it in GitHub Desktop.
Script to cycle through a list of files, setting a symlink to the next file in the list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# I had a bunch of credentials files in a directory, and wanted to be able to | |
# cycle quickly between them so that I could log into a service with different | |
# identities. This script does the job by creating a symlink: | |
# credentials => creds.foo | |
# Every time you call it, it sets the symlink to the next file in the list. | |
# It's probably way overkill, but it was an interesting learning exercise writing it! | |
# This may need tweaking to work on your version of Linux. | |
setlink() { | |
rm -f credentials | |
ln -s $1 credentials | |
echo "Using $1" | |
} | |
credfiles=(`ls creds.*`) | |
len=${#credfiles[@]} | |
curr=$(readlink -n 'credentials') | |
if [ "X$curr" = "X" ] ; then | |
# Symlink doesn't exist, arbitrarily set it to first creds file in list. | |
setlink ${credfiles[0]} | |
exit | |
fi | |
for (( ii = 0 ; ii < $len ; ++ii )); do | |
if [ "$curr" = "${credfiles[$ii]}" ] ; then | |
next=$(( ($ii+1) % $len )) | |
setlink ${credfiles[$next]} | |
break | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment