将字符串分割按照不EVAL外壳参数规则令牌 [英] Splitting a string to tokens according to shell parameter rules without eval

查看:104
本文介绍了将字符串分割按照不EVAL外壳参数规则令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像

$ str="abc 'e f g' hij"

和我希望得到它的整个 E F G 部分。换句话说,我希望来标记字符串根据shell参数的规则。

and i wish to get whole e f g part of it. In other words, i wish to tokenize the string according to shell parameter rules.

目前,我在做什么,作为

Currently, i am doing that as

$ str="abc 'e f g' hij"; (eval "set -- $str"; echo $2)

不过这完全是不安全的,如果一个 * 获取外部 -ticks。

but this is totally unsafe if a single * gets outside of '-ticks.

没有更好的办法?

推荐答案

您可以使用设置-f 禁用文件名扩展干脆。

You can use set -f to disable filename expansion altogether.

$ str="* 'e f g' hij"
$ ( set -f; eval "set -- $str"; echo $2 )
e f g

这解决您可能会评估预测,但可能有其他可用选项的只有一个问题设置你可以探索。

This addresses just one problem you might anticipate with eval, but there may be other options available with set you can explore.

这篇关于将字符串分割按照不EVAL外壳参数规则令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆