9 linux unix bash gnu command-line-arguments
我总是想知道:大多数 GNU/Unix 工具都采用“减去某物”形式的选项,有时后跟一个参数。如果您有一个名为减去某些内容的文件怎么办?
$ ls
-f
$ rm -f
$ ls
-f
$ mv -f abc
mv: missing destination file operand after `abc'
Try `mv --help' for more information.
$ cat -f
cat: invalid option -- 'f'
Try `cat --help' for more information.
Run Code Online (Sandbox Code Playgroud)
或者
$ ls
-ohello.c
$ gcc -ohello -ohello.c
gcc: fatal error: no input files
compilation terminated.
Run Code Online (Sandbox Code Playgroud)
这只是出于好奇;我没有这方面的用例。
Wil*_*ell 10
在面试环境中问这种类型的问题是相当普遍的。处理带有破折号的文件的常用方法是:
$ rm -- -f
$ rm ./-f
Run Code Online (Sandbox Code Playgroud)
Unix 中的一个常见问题。主要的方法是给出文件的完整路径名,所以它前面没有破折号:
$ rm -file.txt
unknown option -l
$ rm ./-file.txt #No problem!
$ rm $PWD/-file.txt #Same thing
Run Code Online (Sandbox Code Playgroud)
某些命令,您可以单独使用破折号(或双破折号)来结束选项。然而,这不一定适用于所有命令,甚至不同系统上的相同命令。
$ rm -- -file.txt #Works on Linux but not on some Unix systems
Run Code Online (Sandbox Code Playgroud)