From 55c08e07f6e38190f0420a659367c8203b82d4cc Mon Sep 17 00:00:00 2001 From: Salt Date: Wed, 16 Dec 2020 00:52:57 -0600 Subject: [PATCH] ps1test: Add a PS1 tester --- ps1test | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 ps1test diff --git a/ps1test b/ps1test new file mode 100755 index 0000000..c8cdb00 --- /dev/null +++ b/ps1test @@ -0,0 +1,45 @@ +#! /bin/bash +# +# ps1test +# Tests your bash PS1 +# Copyright (C) 2020 Vintage Salt +# +# Distributed under terms of the MIT license. +# + +main() { + . ~/.profile + . ~/.bashrc + printf "Default:\n" + printps1 # Test a default ps1 + false || printps1 # Test after command failure + PWD=~/Documents printps1 # Test in a subdir of ~ + PWD=/tmp printps1 # Test in a world-writeable dir + PWD=/usr/local printps1 # Test in unwriteable dir + PWD=/dir/that/doesnt/exist printps1 # Test in dir that doesn't exist + printf "\nDefault, different user:\n" + USER=notme printps1 # Test as another user + printf "\nSame user, different machine:\n" + SSH_CLIENT=notme HOSTNAME=notme printps1 # Test over fake SSH + printf "\nForeign machine, foreign user:\n" + USER=notme SSH_CLIENT=notme HOSTNAME=notme printps1 # Test both of the above +} + +hostname() { + # We have this little stub here to intercept calls to hostname + echo "$HOSTNAME" +} + +printps1() { + # Print the PS1, with some substitutions + "$PROMPT_COMMAND" > /dev/null 2>&1 + PS1="${PS1//\\[/}" + PS1="${PS1//\\]/}" + PS1="${PS1//\\w/$PWD}" + PS1="${PS1//$HOME/\~}" + printf "\t$PS1\n" +} + + +main "$@" +