Got questions? Go ahead: Ask me anything!
Why does this variable become blank? It's obviously set in the first part, but it goes away.
who am i | if grep tony > /dev/null 2>&1 then echo "Setting source directory to /usr1/src" export src_path=/usr1/src echo $src_path else src_path=/usr2/src fi echo -e "src_path= $src_path"
What happens here is actually very simple, although it may seem baffling at first. Yes, the "if then" block sets the variable to either /usr1/src or /usr2/src, so it surprised you to find it blank in the last line which echoed "src_path= $src_path".
What you missed is that piping "who am i" to that "if" block caused the creation of a new shell for everything in the block. Variables changed in a new shell don't change the values in the calling shell, so $src_path was still blank.
Something like this will work much better:
IAM=`who am i | cut -d" " -f1` if [ $IAM == "tony" ] then ..
brinan White noted that the script will work in ksh
Got something to add? Send me email.
(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version
Increase ad revenue 50-250% with Ezoic