如何使用引号将可变数量的命令行参数解析为 eval? [英] How to parse a variable number of command line arguments with quotes into eval?
本文介绍了如何使用引号将可变数量的命令行参数解析为 eval?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到了这个简单的脚本:
I got this simple script:
#!/usr/bin/env bash
eval "${@:2}"
while [ true ]
do
FocusApp=`xdotool getwindowfocus getwindowname`
if [[ "$FocusApp" == *"$1"* ]];
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}"
与:
But when I run it, Sublime Text
tries to open a file named My
, another named File
, and etc. If I replace the eval "${@:2}"
with:
eval "\"$2\" \"$3\" \"$4\" \"$5\" \"$6\" \"$7\" \"$8\""
然后,Sublime Text 正确打开文件 "./My File With Spaces in the Name"
.如何使 eval
正确理解所有带有可变数量命令行参数的参数引号,即,无需硬编码 "\"$2\" \"$3\" \"$4\" ..."
?
Then, Sublime Text correctly opens the file "./My File With Spaces in the Name"
. How to make eval
correctly understand all argument quotes with a variable number of command line arguments, i.e., without hard coding "\"$2\" \"$3\" \"$4\" ..."
?
推荐答案
把 eval
放在外面会更容易:
It's easier to just leave eval
out of it:
#!/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
这篇关于如何使用引号将可变数量的命令行参数解析为 eval?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文