开发者

Why ./script/generate is needed instead of script/generate in some Rails instruction?

Inside of Bash or Win开发者_开发百科dows (or any other shell), is it needed to do

./script/generate scaffold foo name:string

instead of just using

script/generate  ...

? I do see the first form sometimes, but the second form always works for me, on Mac OS X or Ubuntu, even if the PATH doesn't include the . (current directory)

So can the second form always work, so the first form is really not needed? I think for executable, we sometimes use ./a.out to run it... but seems like maybe script/generate doesn't need the ./ in front?


They mean exactly the same thing.

Starting from the current directory, select a subdirectory called 'script' and in it an executable called 'generate' and run it.

The difference is that with ./ you're explicitly specifying the current directory and without it, it's implicit.


There are 2 syntaxes of invocations in POSIX shell:

  • Running a program by specifying a name and then searching it in PATH enviornment variable - this one is used when program's name has no slashes (/).
  • Running a program by specifying full path to it manually - absolutely (path starting with /) or relatively (path starting with any other symbol). This one is chosen when program's name includes at least one / - thus it's a path, not just a name of file.

In your case, both ways to invoke - script/generate or ./script/generate are executed using variant #2 - by specifying a path to the program. ./ is an alias to current directory and in some cases it's required to be present (for example, when using cd command, you can't just say cd without argument and expect to change into current directory - cd reserves call without arguments to change to $HOME directory - but you may call cd ./ if you want to cd into current directory), but it's not required in this case of invocation.


you might want to have a look at 4 Ways of Executing a Shell Script and Shell Script Execution Guidlines. The main difference between both styles is:

script/generate  ...

won't work if the parent directory of script does not lie in your current PATH environment. (Well this sure depends on how the shell's lookup method is implemented. There might be implementations that - to a last resort - are looking within your current working directory).

EDIT
Ok, I've done some research on this as I myself didn't seem to be knowing what the difference is. So, this is what I've arrived at:

The ./ (dot slash) syntax is an alias for the absolute path of the current working direcotry, so that - with e.g. /home/peter/script being the cwd -

peter@linux:/home/peter/script$./myscript

is expanded to /home/peter/script/myscript. The slash dot alias basically gives the shell a hint at where to find the executable/script.

That hint is what's essential, and that's the reason why

peter@linux:/home/peter$script/myscript

works as well whereas

peter@linux:/home/peter/script$myscript

won't. The former aids the shell in finding the right executable/script by providing some sort of reference point (namely the script directory). The latter, on the other hand, leaves the shell with a possibly ambiguous command, as there could be a homonymous script within the user's $PATH.

So, to come to an end, both of the styles you've asked the difference for do basically the same - giving the shell a hint at where to look for the executable/script. The shell then is able to unambiguously resolve the correct file and will happily execute it.

Have a nice day.


Nothing wrong with those answers except that they are too long - it's just about your PATH. If your path includes '.' then either way would work

That said, it's a bad habit to put '.' in your PATH for security reasons, so stick with './'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜