Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > How do i check if a directory exists and display that it doesn't using bash?

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Closed Thread
 
Thread Tools Display Modes
Old 11-24-2005   #1 (permalink)
Linux Enthusiast
 
Join Date: Jun 2005
Location: Odessa, FL
Posts: 586
Send a message via AIM to josolanes Send a message via MSN to josolanes Send a message via Yahoo to josolanes
How do i check if a directory exists and display that it doesn't using bash?

Like the title says: How would I check if a directory exists within a script and display a message if it doesn't using bash?

This is what I have:
Code:
if [ $check -eq 0 ]; then
        echo "Error: '$check' does not exist!!"
        echo "EXITING"
        exit 0
fi
The part that I can't figure out is what should be where the "-eq" is? Something has to go there to determine whether it exists or not.
"$check" has to be a string, as the user inputs part of the directories name and "$check" is just the rest of the path + the directory name that the user inputs.

I'm trying to check if the directory with the string name "$check" exists, then display a message that it doesn't if it doesn't. Can someone help me with this? I'm very new to bash and was using another program as a refrence/learning program but I'm not sure how I would do this.

Thanks in advance!
josolanes is offline  


Old 11-24-2005   #2 (permalink)
Linux Engineer
 
Join Date: Mar 2005
Posts: 1,431
I would have done it this way:
Code:
[ -a directoryname ] || echo "not there"
jaboua is offline  
Old 11-24-2005   #3 (permalink)
Linux Guru
 
dylunio's Avatar
 
Join Date: Aug 2004
Location: Cymru
Posts: 4,157
If you want to check for a ditectory use the -d file (with file being the directory). So something like this will check if it exists:
Code:
if [ -d $check ]; then
#do nothing
elif
echo "Error: '$check' does not exist!!"
echo "EXITING"
exit 1
fi
__________________
Registered Linux User #371543!
Get force-get May The Source Be With You
/dev/null
/dev/null2
dylunio is offline  
Old 11-24-2005   #4 (permalink)
Linux Enthusiast
 
Join Date: Jun 2005
Location: Odessa, FL
Posts: 586
Send a message via AIM to josolanes Send a message via MSN to josolanes Send a message via Yahoo to josolanes
Quote:
Originally Posted by dylunio
If you want to check for a ditectory use the -d file (with file being the directory). So something like this will check if it exists:
Code:
if [ -d $check ]; then
#do nothing
elif
echo "Error: '$check' does not exist!!"
echo "EXITING"
exit 1
fi
OK, that worked
Thanks a lot!



....one more question. Is there a way to add text to the beginning of a text file?

Code:
echo "insert this text" >> file
...adds it to the end if i'm correct, and I'd like to add text to the beginning of the file instead. There's probably a simple command to do this, but if there's not, I thought about copying the file, deleting the original, recreating the file and echo'ing the needed text into it, and moving the text from the copy back. I don't know how to move text from one file to another though either.

Can someone help me with this?

Thanks in advance!
josolanes is offline  
Old 11-25-2005   #5 (permalink)
Linux Enthusiast
 
Join Date: Jun 2005
Location: Odessa, FL
Posts: 586
Send a message via AIM to josolanes Send a message via MSN to josolanes Send a message via Yahoo to josolanes
*bump*

anyone?

this is the last thing i need and the program will be finished. i'll most likely post the progam here for you to use. it's a program that i'm writing that will safely update your kernel.
it will:
(if asked) tell you what kernel you're currently running, show you what folders are in /usr/src/, and show you what's symlinked to /usr/src/linux right now
ask you for the kernel number (exluding "linux-")
download the vanilla kernel for you
untar it
do the necessary things to use the old .config file and symlink the new kernel to /usr/src/linux
compile it and run make menuconfig so you can change something if you'd like to (even though it would all be preset from your previous kernel)
mount /boot
move the necessary files to /boot
add the lines necessary to the grub.conf
unmount /boot



i'm at the part of how to add the text to the beginning of grub.conf. the rest works
josolanes is offline  
Old 11-25-2005   #6 (permalink)
Trusted Penguin
 
Cabhan's Avatar
 
Join Date: Jan 2005
Location: Boston, MA, USA
Posts: 2,691
The way I'd do it with Perl would be to read the entire file, write the new words to the file, then rewrite the earlier words back below it.

As a note, you might prompt the user for if they use LILO or GRUB and make the necessary changes to either /etc/lilo.conf or /boot/grub/menu.lst (and run "lilo" afterwards, if they said LILO). That just adds a bit of extra functionality.
__________________
DISTRO=Gentoo
Registered Linux User #388732
Gentoo Linux, 410 GB HD, 1.2 GB RAM, Fluxbox, These are a Few of my Favorite Things
Cabhan is offline  
Old 11-25-2005   #7 (permalink)
Linux Enthusiast
 
Join Date: Jun 2005
Location: Odessa, FL
Posts: 586
Send a message via AIM to josolanes Send a message via MSN to josolanes Send a message via Yahoo to josolanes
Quote:
Originally Posted by Cabhan
The way I'd do it with Perl would be to read the entire file, write the new words to the file, then rewrite the earlier words back below it.

As a note, you might prompt the user for if they use LILO or GRUB and make the necessary changes to either /etc/lilo.conf or /boot/grub/menu.lst (and run "lilo" afterwards, if they said LILO). That just adds a bit of extra functionality.
i can add that, sure. i also plan on asking the user if it's a 2.4 kernel or 2.6, so it finds the correct kernel and does the correct commands to install it (but i'm not sure if i'll add functionality for 2.4 as i've never used one and don't know if it works).

for lilo, the only thing i would change is where it's at and how it's placed in the file right?

also, is there any way to add text to the beginning of a file using bash? or just a script command that i can use. i'd think it would be easy to do, but i guess there's not a command to do it.

do you know how i would do what you were explaining, but in bash rather than perl?
josolanes is offline  
Old 11-25-2005   #8 (permalink)
Linux Enthusiast
 
Join Date: Jun 2005
Location: Odessa, FL
Posts: 586
Send a message via AIM to josolanes Send a message via MSN to josolanes Send a message via Yahoo to josolanes
also, is there a way to use only the first few characters of a string and make that a new string?

i want the user to type something like this:
install-kernel 2.6.12.2
and want the program to find out whether it's 2.6 or 2.4, so it can install it the correct way and download it from the correct place. how would i make the 2.4 or 2.6 a different string using bash?
josolanes is offline  
Old 11-25-2005   #9 (permalink)
Trusted Penguin
 
Cabhan's Avatar
 
Join Date: Jan 2005
Location: Boston, MA, USA
Posts: 2,691
Well, regarding the first, have you considered:
Code:
mv foo bar
echo "ooga" > foo
cat bar >> foo
rm bar
That is, rename the original file, write something to what the original file was called, then copy the original file below the new one, and erase the copy.


In regards to the second issue, that is called a substring, and a quick Google found me how to do that:
Code:
alex@tux ~/test $ cat substring_test
#!/bin/sh

test=2.5.4
more_test=${test:0:3}

echo $more_test
alex@tux ~/test $ ./substring_test
2.5
So basically, it works as such:
Code:
${var_name:starting index:ending_index}
The indexes are 0-based, not 1-based. Also, the ending_index is exclusive, so if you say (0,2), you will only get back characters 0 and 1.
__________________
DISTRO=Gentoo
Registered Linux User #388732
Gentoo Linux, 410 GB HD, 1.2 GB RAM, Fluxbox, These are a Few of my Favorite Things
Cabhan is offline  
Old 11-25-2005   #10 (permalink)
Linux Enthusiast
 
Join Date: Jun 2005
Location: Odessa, FL
Posts: 586
Send a message via AIM to josolanes Send a message via MSN to josolanes Send a message via Yahoo to josolanes
Quote:
Originally Posted by Cabhan
Well, regarding the first, have you considered:
Code:
mv foo bar
echo "ooga" > foo
cat bar >> foo
rm bar
That is, rename the original file, write something to what the original file was called, then copy the original file below the new one, and erase the copy.
that's what i was thinking of how to do it, i just couldn't figure out how to copy text within a file to another (the cat command). thanks a lot!

Quote:
In regards to the second issue, that is called a substring, and a quick Google found me how to do that:
Code:
alex@tux ~/test $ cat substring_test
#!/bin/sh

test=2.5.4
more_test=${test:0:3}

echo $more_test
alex@tux ~/test $ ./substring_test
2.5
So basically, it works as such:
Code:
${var_name:starting index:ending_index}
The indexes are 0-based, not 1-based. Also, the ending_index is exclusive, so if you say (0,2), you will only get back characters 0 and 1.
thanks a lot! i'm going to try that. so, the starting index is where which character it would start at in the string (0 means the first), and the ending index is the character it would end at.

so, in the word "testing", if i wanted to take only the word "test" i would do:
Code:
#!/bin/sh

word=testing
small_word=${word:0:3}

echo $small_word
right?



btw, thanks a lot for helping me with this
i'll post the overall program code once i'm finished with it
josolanes is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 09:41 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2