Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
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: ...
  1. #1
    MBN
    MBN is offline
    Just Joined!
    Join Date
    Oct 2008
    Posts
    6

    Excuse the newbieness

    Quote Originally Posted by dylunio View Post
    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
    Excuse the newbieness so if I wanted to use this it would look something like .....
    if [ -d /home/james/txtFiles/ $check ]; then
    #do nothing
    elif
    echo "Error: '$check' does not exist!!"
    echo "EXITING"
    exit 1
    fi

  2. #2
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    No. $check here is being used as a variable. As dylunio's script is written, he would have needed to have previously $check as some directory name.

    So, say you have a folder called Desktop in ~ (so the full path would be /home/james/Desktop). Consider this problem, which we'll call dircheck.sh
    Code:
    #!/bin/bash
    
    if [ -d $1 ]; then
    echo "> That directory exists!"
    exit 0;
    fi
    exit 1;
    Note that $1 contains the value of the first argument passed to a shell script. I use it here to demonstrate variable usage.

    So, the output on my computer looks like this:
    Code:
    Equinox:~ danielsmw$ ./dircheck.sh Desktop
    > That directory exists!
    Equinox:~ danielsmw$ ./dircheck.sh awefalweigjawe4g
    Equinox:~ danielsmw$
    Does that make sense? If not, I can try to explain it differently. Sometimes it helps to see it in different ways.
    Registered Linux User: #479567
    Asking a question? Read this page first.
    Now... sudo make me a sandwich.
    ratiocinativeroot.blogspot.com

  3. #3
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    I would write dircheck.sh like this:
    #!/bin/bash
    test -d $1 || (echo "$1 does not exist"; exit 1)

  4. #4
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    I was trying to keep it close to the original example... but I have to say, secondmouse, that is a very elegant solution. =)
    Registered Linux User: #479567
    Asking a question? Read this page first.
    Now... sudo make me a sandwich.
    ratiocinativeroot.blogspot.com

  5. #5
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    danielsmw,
    Thanks but I just found out my script has little problem if there are more code after it, for example,

    #!/bin/bash
    test -d $1 || (echo "$1 does not exist"; exit 1)
    echo "this line should not show if the folder does not exist"


    The bold line will show no matter $1 exists or not. The reason for that is codes inside () are considered to be in a subshell. exit 1 will simply exist from that subshell but not the outer level shell. To make it error proof, I have to do change () to {}, that is

    #!/bin/bash
    test -d $1 || { echo "$1 does not exist"; exit 1; }
    echo "this line should not show if the folder does not exist"


    Please note that a space after { and before } is required, besides the semicolon after exit 1

  6. #6
    MBN
    MBN is offline
    Just Joined!
    Join Date
    Oct 2008
    Posts
    6
    Gawd lol I'm trying to get in the good books with this....

    maybe if I explain what I am trying to do would be easier....


    I want to check if the folder exits......if not mkdir /textFiles/
    then copy text files into it.

    If it does I want to copy the files into the folder /textFiles/

  7. #7
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    Either my or secondmouse's code examples can be extended to do this. In my code, you'd need to use an if statement to determine if the directory is there or not, and then to mkdir the directory if necessary. At the end of both statements, you can do your file copy.

    In secondmouse's code, you can do the mkdir and cp inside the {}'s, and then just do a similar cp at the end in case the program gets to there.

    By the way, since the mkdir command just exits cleanly on error, you really don't need to worry about the directory's existence if you don't want to... but that would be bad coding practice, so don't do it if this is a serious application that needs to be very reliable.
    Registered Linux User: #479567
    Asking a question? Read this page first.
    Now... sudo make me a sandwich.
    ratiocinativeroot.blogspot.com

  8. #8
    MBN
    MBN is offline
    Just Joined!
    Join Date
    Oct 2008
    Posts
    6
    after having abit of a play I came up with this

    #!/bin/bash
    directory="/home/txtFiles"

    if [ -d $directory ]; then
    echo $directory exits # this is where I will put the copy command instead of the echo for the files I want to copy into the folder instead of the echo
    else
    mkdir /home/txtFiles/
    this is where I will put the copy command instead of the echo for the files I want to copy into the folder.
    fi




    Does this look about right???

  9. #9
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    Looks about right to me! Except for that "exits"... what is that? Use "exit 0" and put it on a new line. (0 = exits without error).
    Registered Linux User: #479567
    Asking a question? Read this page first.
    Now... sudo make me a sandwich.
    ratiocinativeroot.blogspot.com

  10. #10
    MBN
    MBN is offline
    Just Joined!
    Join Date
    Oct 2008
    Posts
    6
    after having abit of a play I came up with this

    #!/bin/bash
    directory="/home/txtFiles"

    if [ -d $directory ]; then
    echo $directory exits # this is where I will put the copy command instead of the echo for the files I want to copy into the folder instead of the echo
    else
    mkdir /home/txtFiles/
    this is where I will put the copy command instead of the echo for the files I want to copy into the folder.
    fi




    Does this look about right???

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
  •  
...