Solved

bash if a variable doesn't exit then create it

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
open source backup solution 1 29
ftp to port 21 4 43
awk to variable in bash 2 43
Linux Desktop suggestion for Dell Inspiron 3043 13 40
rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

809 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