Link to home
Start Free TrialLog in
Avatar of illtbagu
illtbagu

asked on

bash if a variable doesn't exit then create it

I am trying to create a script. I need the script to check and see if a variable exists. If the variable does not exist then it needs to create it other wise it can skip this part.
-----------------
if [ -z echo $KDEDIR ]
then
export KDEDIR=/opt/kde3
else
cd $KDEDIR/share/apps/konqueror/servicemenus/
fi
-----------------

Thanks
Avatar of blkline
blkline

-z tests for a zero-length string.  Thus:

if [ -z "$KDEDIR" ]
then
export KDEDIR=/opt/kde3
else
cd $KDEDIR/share/apps/konqueror/servicemenus/
fi


Barry
ASKER CERTIFIED SOLUTION
Avatar of blkline
blkline

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of illtbagu

ASKER

Yes thats what I was looking for, Thanks. I knew I was kind of close. Can I bother you for one more question? This is really the fist bash script I have wrote and I can't get this part right. I am so use to using windows batch scripting that its killing me. I like batch scripting in windows because I can use the GOTO
----------------------------------------------------------------------------------------
if [ -d /opt/kde3/share/apps/konqueror/servicemenus ]
then
echo "Looks Like SuSE."
sleep 1
if [ -z "$KDEDIR" ]
then
echo "Lets create a enviorment varibale for KDE, bad bad SuSE this should have already been done for us."
export KDEDIR=/opt/kde3
if [ -n "$KDEDIR" ]
then
echo "added enviorment variable $KDEDIR"
else
cd $KDEDIR/share/apps/konqueror/servicemenus/
rm -f shred_files.desktop*
rm -f shred_folders_wrapper.sh*
fi
----------------------------------------------------------------------------------------
Never mind what I needed was elif.