What command can be used to check if a directory exists or not, within a shell script?
相关问题
- How to get the return code of a shell script in lu
- Is shmid returned by shmget() unique across proces
- Invoking Mirth Connect CLI with Powershell script
- Why should we check WIFEXITED after wait in order
- Emacs shell: save commit message
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Making new files automatically executable?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- What does it take to be durable on Linux?
Here's a very pragmatic idiom:
I typically wrap it in a function:
Or:
The nice thing about this approach is that I do not have to think of a good error message.
cd
will give me a standard one line message to stderr already. It will also give more information than I will be able to provide. By performing thecd
inside a subshell( ... )
, the command does not affect the current directory of the caller. If the directory exists, this subshell and the function are just a no-op.Next is the argument that we pass to
cd
:${1:?pathname expected}
. This is a more elaborate form of parameter substitution which is explained in more detail below.Tl;dr: If the string passed into this function is empty, we again exit from the subshell
( ... )
and return from the function with the given error message.Quoting from the
ksh93
man page:and
The phrasing here is peculiar to the shell documentation, as
word
may refer to any reasonable string, including whitespace.In this particular case, I know that the standard error message
1: parameter not set
is not sufficient, so I zoom in on the type of value that we expect here - thepathname
of a directory.A philosphical note: The shell is not an object oriented language, so the message says
pathname
, notdirectory
. At this level, I'd rather keep it simple - the arguments to a function are just strings.The above code checks if the directory exists and if it is writable.
You can use
test -d
(seeman test
).For example:
Note: The
test
command is same as conditional expression[
(see:man [
), so it's portable across shell scripts.For possible options or further help, check:
help [
help test
man test
orman [
Actually, you should use several tools to get a bulletproof approach:
No need to worry about spaces and special characters as long as you use
"${}"
.Note that
[[]]
is not as portable as[]
, but since most people work with modern versions of Bash (since after all, most people don't even work with command line :-p), the benefit is greater than the trouble.Have you considered just doing whatever you want to do in the
if
rather than looking before you leap?IE, if you want to check for the existence of a directory before you enter it, try just doing this:
If the path you give to
pushd
exists, you'll enter it and it'll exit with0
, which means thethen
portion of the statement will execute. If it doesn't exist, nothing will happen (other than some output saying the directory doesn't exist, which is probably a helpful side-effect anyways for debugging).Seems better than this, which requires repeating yourself:
Same thing works with
cd
,mv
,rm
, etc... if you try them on files that don't exist, they'll exit with an error and print a message saying it doesn't exist, and yourthen
block will be skipped. If you try them on files that do exist, the command will execute and exit with a status of0
, allowing yourthen
block to execute.