?
Solved

bash if a variable doesn't exit then create it

Posted on 2004-10-03
4
Medium Priority
?
278 Views
Last Modified: 2010-04-20
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
0
Comment
Question by:illtbagu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:blkline
ID: 12213537
-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
0
 
LVL 6

Accepted Solution

by:
blkline earned 260 total points
ID: 12213544
Now that I look at the code, aren't you really interested in doing this?

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

I'm assuming that you have a standard directory into which you will cd into, unless otherwise specified.

Barry
0
 
LVL 1

Author Comment

by:illtbagu
ID: 12213746
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
----------------------------------------------------------------------------------------
0
 
LVL 1

Author Comment

by:illtbagu
ID: 12213775
Never mind what I needed was elif.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question