Solved

bash if a variable doesn't exit then create it

Posted on 2004-10-03
4
277 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 65 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

696 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