Mac OS X的 - 传递路径用空格作为参数bashscript,然后发出打开终端命令 [英] Mac OS X - Passing pathname with spaces as arguments to bashscript and then issue open Terminal command
问题描述
的问题在Mac OS X小牛队使用bash shell的
我创建 test.sh
称为bash脚本,接受一个命令行参数: -
$猫test.sh
#!/斌/庆典
打开-a终端$ 1
当我以下面的方式执行该脚本: -
$ ./test.sh /用户/名为myusername /桌面/ folderwithoutspaces /
它执行完美,在给定的文件夹中启动一个新的终端窗口。
但是,当我以下面的方式执行该脚本: -
$ ./test.sh /用户/名为myusername /桌面/文件夹\\ withspaces /
它无法打开新的终端窗口,并显示以下错误消息: -
中的文件/用户/名为myusername /桌面/文件夹和/用户/名为myusername /桌面/不存在withspaces。
我试过所有可能的方式,但没有成功的任何: -
$ ./test.sh/用户/名为myusername /桌面/文件夹\\ withspaces /
$ ./test.sh/用户/名为myusername /桌面/文件夹withspaces /
$ ./test.sh'/用户/名为myusername /桌面/文件夹\\ withspaces /'
$ ./test.sh'/用户/名为myusername /桌面/文件夹withspaces /
$ ./test.sh /用户/名为myusername /桌面/文件夹\\ withspaces /
当您使用该变量引用,它就会根据 IFS
分(其中包括由一个空白默认)。解决的办法是引用变量。不要说:
打开-a终端$ 1
说:
打开-a终端$ 1
Problem Using bash shell on Mac OS X Mavericks
I created a bash script called test.sh
that accepts one command line argument:-
$ cat test.sh
#!/bin/bash
open -a Terminal $1
When i execute this script in the following way:-
$ ./test.sh /Users/myusername/Desktop/folderwithoutspaces/
it executes perfectly and launches a new Terminal window in the given folder.
But when i execute this script in the following way:-
$ ./test.sh /Users/myusername/Desktop/folder\ withspaces/
it fails to open a new Terminal window and shows the following error message:-
The files /Users/myusername/Desktop/folder and /Users/myusername/Desktop/withspaces do not exist.
I tried all these possible ways but wasn't successful at any:-
$ ./test.sh "/Users/myusername/Desktop/folder\ withspaces/"
$ ./test.sh "/Users/myusername/Desktop/folder withspaces/"
$ ./test.sh '/Users/myusername/Desktop/folder\ withspaces/'
$ ./test.sh '/Users/myusername/Desktop/folder withspaces/'
$ ./test.sh /Users/myusername/Desktop/folder\ withspaces/
When you use the variable with quoting, it gets split based on IFS
(which includes a whitespace by default). The solution is to quote the variable. Instead of saying:
open -a Terminal $1
say:
open -a Terminal "$1"
这篇关于Mac OS X的 - 传递路径用空格作为参数bashscript,然后发出打开终端命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!