Results 1 to 10 of 18
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:
...
- 11-24-2005 #1Linux Enthusiast
- Join Date
- Jun 2005
- Location
- Odessa, FL
- Posts
- 586
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:
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.Code:if [ $check -eq 0 ]; then echo "Error: '$check' does not exist!!" echo "EXITING" exit 0 fi
"$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!
- 11-24-2005 #2Linux Engineer
- Join Date
- Mar 2005
- Posts
- 1,431
I would have done it this way:
Code:[ -a directoryname ] || echo "not there"
- 11-24-2005 #3
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
- 11-24-2005 #4Linux Enthusiast
- Join Date
- Jun 2005
- Location
- Odessa, FL
- Posts
- 586
OK, that worked
Originally Posted by dylunio 
Thanks a lot!
....one more question. Is there a way to add text to the beginning of a 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.Code:echo "insert this text" >> file
Can someone help me with this?
Thanks in advance!
- 11-25-2005 #5Linux Enthusiast
- Join Date
- Jun 2005
- Location
- Odessa, FL
- Posts
- 586
*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
- 11-25-2005 #6
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=Arch
Registered Linux User #388732
- 11-25-2005 #7Linux Enthusiast
- Join Date
- Jun 2005
- Location
- Odessa, FL
- Posts
- 586
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).
Originally Posted by Cabhan
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?
- 11-25-2005 #8Linux Enthusiast
- Join Date
- Jun 2005
- Location
- Odessa, FL
- Posts
- 586
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?
- 11-25-2005 #9
Well, regarding the first, have you considered:
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.Code:mv foo bar echo "ooga" > foo cat bar >> foo rm bar
In regards to the second issue, that is called a substring, and a quick Google found me how to do that:
So basically, it works as such: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
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.Code:${var_name:starting index:ending_index}
DISTRO=Arch
Registered Linux User #388732
- 11-25-2005 #10Linux Enthusiast
- Join Date
- Jun 2005
- Location
- Odessa, FL
- Posts
- 586
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!
Originally Posted by Cabhan
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.In regards to the second issue, that is called a substring, and a quick Google found me how to do that:
So basically, it works as such: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
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.Code:${var_name:starting index:ending_index}
so, in the word "testing", if i wanted to take only the word "test" i would do:
right?Code:#!/bin/sh word=testing small_word=${word:0:3} echo $small_word
btw, thanks a lot for helping me with this
i'll post the overall program code once i'm finished with it



