编写shell脚本,接受带空格的命令行输入(writing shell script that accepts command line inputs with spaces)

我想写一个shell脚本myscript。 用户将键入一个文件夹以使其工作:

~$: myscript /myfolder1/random folder/fb/picture set/...

正如您所看到的,取决于文件夹,输入中有未知的空格数。

我希望myscript能够将整个输入解释为一个输入。 我能做些什么来实现这个目标?

我可以使用read命令捕获输入,但它只需要在用户输入后输入下一行

的MyScript +输入。

这不是我想要的。

I want to write a shell script myscript. The user will type a folder to get it to work:

~$: myscript /myfolder1/random folder/fb/picture set/...

As you can see depends on the folder there are unknown number of spaces in the input.

I want myscript to be able to interpret the whole input as one input. What can i do to achieve this?

I can use read command to capture the input, but it only expects the input in the next line after the user types

myscript+enter.

Which is not what i want.

最满意答案

你不能,因为你的脚本不知道有多少空格,正在使用哪些空白字符,或者某些参数应该是标志还是路径片段。 您的用户必须正确引用脚本的参数。

You can't, since your script can't know how much whitespace there is, which whitespace characters are being used, or whether certain arguments are supposed to be flags or fragments of paths. Your users will have to correctly quote arguments to the script.

更多推荐