I just want to verify that the script I wrote is doing what I think it's doing, and that it's doing it properly.
I wanted to write a script that takes an environment variable and a string value, and then sets that variable to the given value. So I can do something like setvar BOOST_HOME /home/me/boost/boost_1.52.0 and the script will export BOOST_HOME=/home/me/boost/boost_1.52.0
Something like:
#!/bin/bash
# Usage: setvar VAR VAR_VALUE
function setvar()
{
VAR=${1}
VAR_VALUE=${2}
if [ -d $2 ]
then
eval export $VAR=$2
fi
}
This seems to work, at least judging from a echo echo tests, but I am still not very comfortable with shell scripting, and would like someone to either verify what I am doing or point out what I am doing wrong / less correct.
{}bracket only var=$1 like that is enougheval, and that it would be safer without theeval(and I'd put double quotes around the$2, but I may be old-fashioned). You setVAR_VALUEbut don't use it.exportyou need quotes around both$1and$2to be safe.$2isn't a directory, this function gives no indication thatBOOST_HOMEwon't actually be set.