2
1
Using Ubuntu 9.10 and BASH_VERSION='4.0.33(1)-release'...
I have a bash function that I want to be able to call from my shell. I asked a different question about it here:
So now that I've fixed the syntax issues, the new problem is that the function is not being exported properly to my shell environment. So I tried moving the file from /etc/profile.d to my home dir and now I am just trying to source it from ~/.profile, like this:
. $HOME/.p4c
This doesn't have any effect - the function is still not exported. How I am checking this:
- When I open a terminal and run
set|grep p4c
I don't see this function defined. - If I then type
. $HOME/.p4c
on the command line, and then runset|grep p4c
again, now I can see the function exported properly to my environment.
I know that my ~/.profile is being read, because immediately above . $HOME/.p4c
I am exporting an env variable and it is defined in my environment when I first open a terminal. Ugh...
Here is the contents of $HOME/.p4c:
# p4c() function setup params
p4_HOST=`hostname | awk -F . '{print $1}'`
# function for setting the P4CLIENT variable based on the first non-option
# argument provided
p4c() {
HELP_MODE=''
VERBOSE_MODE=''
DESC_MODE=''
SHORT_MODE=''
while getopts ":hdsv" option
do
case $option in
h) echo "p4c provides information about perforce clients."
echo "Recognized arguments:"
echo " -h help (this message)"
echo " -d descriptions (prints client descriptions - usefu l, but slightly slower)"
echo " -v verbose (print unreasonable amounts of debugging info"
echo
# About to exit - reset OPTIND or we'll be in trouble later.
OPTIND=1
# Abort
return
;;
v) VERBOSE_MODE='verbose';;
d) DESC_MODE='descriptions';;
s) SHORT_MODE='short';;
*) echo "Unknown option '$OPTARG'! Specify -h for help..."
# About to exit - reset OPTIND or we'll be in trouble later.
OPTIND=1
# Abort
return
;;
esac
done
# Set argument pointer to first non-option argument
shift $(($OPTIND - 1))
# Done with OPTIND - better reset it before something bad happens...
OPTIND=1
PROJECT=$1;
if [ $VERBOSE_MODE ]
then
echo "PROJECT: $PROJECT"
fi
# Need to check/set p4_USER every time to allow changes between invocations
if [ -z "$p4c_USER" ]
then
p4_USER=`echo $P4USER`
if [ -z "$p4_USER" ]
then
p4_USER=`id -nu`
fi
else
p4_USER=$p4c_USER
fi
if [ $VERBOSE_MODE ]
then
echo "p4_USER: $p4_USER"
fi
if [ -n "$PROJECT" ]
then
# provided a non empty string project name
p4_CLIENT=$p4_HOST-$p4_USER-$PROJECT
if [ $VERBOSE_MODE ]
then
echo "p4_CLIENT: $p4_CLIENT"
fi
# check client to see if it exists
p4_GREP_RESULT=`p4 clients | grep "$p4_CLIENT"`
if [ -z "$p4_GREP_RESULT" ]
then
echo "NOTE: P4 client \"$p4_CLIENT\" does not exist on server."
echo "Setting P4CLIENT anyway so that client
Debugging: try putting the line
declare -f p4c
just after it is declared, to check that things are in order before the export. I'd prefer the syntax declare -fx ... to traditional shell syntax for this sort of thing, fwiw. – Charles Stewart – 2010-02-08T18:26:23.817@Charles - thanks for the tip. I tried this when implementing Charles' solution below, and I did see the definition of
p4c
printed out. Nice to know aboutdeclare
- thanks! – AJ. – 2010-02-08T19:18:27.730