如何将可变数量的带引号的命令行参数解析为 eval?
How to parse a variable number of command line arguments with quotes into eval?
我得到了这个简单的脚本:
#!/usr/bin/env bash
eval "${@:2}"
while [ true ]
do
FocusApp=`xdotool getwindowfocus getwindowname`
if [[ "$FocusApp" == *""* ]];
then
wmctrl -ir $(xdotool getactivewindow) -b add,maximized_vert,maximized_horz
break
fi
done
我运行是这样的:
$ ./maximize.sh "Sublime Text" /usr/bin/subl -n "./My File With Spaces in the Name"
但是当我 运行 它时,Sublime Text
尝试打开一个名为 My
的文件,另一个名为 File
的文件,等等。如果我替换 eval "${@:2}"
与:
eval "\"\" \"\" \"\" \"\" \"\" \"\" \"\""
然后,Sublime Text 会正确打开文件 "./My File With Spaces in the Name"
。如何使 eval
使用可变数量的命令行参数正确理解所有参数引号,即,无需硬编码 "\"\" \"\" \"\" ..."
?
将 eval
排除在外更容易:
#!/usr/bin/env bash
"${@:2}"
示例:
$ ./myscript "Demo" 'printf' 'This is one argument: %s\n' 'One long arg' 'Another, with * and such'
This is one argument: One long arg
This is one argument: Another, with * and such
我得到了这个简单的脚本:
#!/usr/bin/env bash
eval "${@:2}"
while [ true ]
do
FocusApp=`xdotool getwindowfocus getwindowname`
if [[ "$FocusApp" == *""* ]];
then
wmctrl -ir $(xdotool getactivewindow) -b add,maximized_vert,maximized_horz
break
fi
done
我运行是这样的:
$ ./maximize.sh "Sublime Text" /usr/bin/subl -n "./My File With Spaces in the Name"
但是当我 运行 它时,Sublime Text
尝试打开一个名为 My
的文件,另一个名为 File
的文件,等等。如果我替换 eval "${@:2}"
与:
eval "\"\" \"\" \"\" \"\" \"\" \"\" \"\""
然后,Sublime Text 会正确打开文件 "./My File With Spaces in the Name"
。如何使 eval
使用可变数量的命令行参数正确理解所有参数引号,即,无需硬编码 "\"\" \"\" \"\" ..."
?
将 eval
排除在外更容易:
#!/usr/bin/env bash
"${@:2}"
示例:
$ ./myscript "Demo" 'printf' 'This is one argument: %s\n' 'One long arg' 'Another, with * and such'
This is one argument: One long arg
This is one argument: Another, with * and such