这个问题有3个部分,每一部分都很容易,但是结合在一起并不容易(至少对我来说不容易):)
需要编写一个脚本,该脚本应该将以下内容作为其参数:
这个解决方案在
/some--path/to/more/idiotic file name.txt
问题2:如何正确获取
问题3:如何实现以下执行方式?
这里有一个不错的shell解决方案,或者需要使用例如perl的其他工具吗?
需要编写一个脚本,该脚本应该将以下内容作为其参数:
- 另一个命令的名称
- 用于该命令的多个参数
- 文件列表
./my_script head -100 a.txt b.txt ./xxx/*.txt
./my_script sed -n 's/xxx/aaa/' *.txt
等等。
出于某种原因,我的脚本需要区分:
- 是哪个命令
- 命令的参数是什么
- 文件是什么
所以,最常规的写法可能是:
./my_script head -100 -- a.txt b.txt ./xxx/*.txt
./my_script sed -n 's/xxx/aaa/' -- *.txt
问题1:有更好的解决方案吗?
在./my_script中处理(第一次尝试):
command="$1";shift
args=`echo $* | sed 's/--.*//'`
filenames=`echo $* | sed 's/.*--//'`
#... some additional processing ...
"$command" "$args" $filenames #execute the command with args and files
这个解决方案在
filenames
中包含空格和/或'--'时会失败,例如:/some--path/to/more/idiotic file name.txt
问题2:如何正确获取
$command
、$args
和$filenames
以供后续执行?问题3:如何实现以下执行方式?
echo $filenames | $command $args #but want one filename = one line (like ls -1)
这里有一个不错的shell解决方案,或者需要使用例如perl的其他工具吗?
while (( $# > 0 )) do processArgs ; shift ; done
这样的东西会有所帮助。3.考虑使用case语句。对于“--”,您可能需要转义它们或将其引用或将其转换为charclass,例如“[-][-]”。 - shellter