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:
...
- 10-07-2008 #1Just Joined!
- Join Date
- Oct 2008
- Posts
- 6
- 10-07-2008 #2
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
Note that $1 contains the value of the first argument passed to a shell script. I use it here to demonstrate variable usage.Code:#!/bin/bash if [ -d $1 ]; then echo "> That directory exists!" exit 0; fi exit 1;
So, the output on my computer looks like this:
Does that make sense? If not, I can try to explain it differently. Sometimes it helps to see it in different ways.Code:Equinox:~ danielsmw$ ./dircheck.sh Desktop > That directory exists! Equinox:~ danielsmw$ ./dircheck.sh awefalweigjawe4g Equinox:~ danielsmw$
Registered Linux User: #479567
Asking a question? Read this page first.
Now... sudo make me a sandwich.
ratiocinativeroot.blogspot.com
- 10-08-2008 #3Linux 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)
- 10-08-2008 #4
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
- 10-08-2008 #5Linux 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
- 10-09-2008 #6Just 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/
- 10-09-2008 #7
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
- 10-09-2008 #8Just 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???
- 10-09-2008 #9
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-09-2008 #10Just 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???



Reply With Quote