Solved

bash if a variable doesn't exit then create it

Posted on 2004-10-03
4
272 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
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:blkline
Comment Utility
-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
Comment Utility
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
Comment Utility
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
Comment Utility
Never mind what I needed was elif.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now