Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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: ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux 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:
    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!

  2. #2
    Linux Engineer
    Join Date
    Mar 2005
    Posts
    1,431
    I would have done it this way:
    Code:
    [ -a directoryname ] || echo "not there"

  3. #3
    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

  4. #4
    Linux Enthusiast
    Join Date
    Jun 2005
    Location
    Odessa, FL
    Posts
    586
    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!

  5. #5
    Linux 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

  6. #6
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    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.

  7. #7
    Linux Enthusiast
    Join Date
    Jun 2005
    Location
    Odessa, FL
    Posts
    586
    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?

  8. #8
    Linux 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?

  9. #9
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    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.

  10. #10
    Linux Enthusiast
    Join Date
    Jun 2005
    Location
    Odessa, FL
    Posts
    586
    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!

    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

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •