Bash命令替换作为参数。

huangapple go评论80阅读模式
英文:

Bash Command Substitution as Parameter

问题

After I have been following down a challenging problem with my friends, I had an idea to make a blind command substitution.

This one with single quotes,

pid='1024 --help touch /tmp/helw' piduser=$(ps -ouser -p$pid h)

does not create the file /tmp/helw

But this one with double quotes,

pid="1024 --help touch /tmp/helw" piduser=$(ps -ouser -p$pid h)

creates the file.

My problem is, how can i make command substitution without let it run in pid - variable, but in piduser variable.
英文:

After I have been following down a challenging problem with my friends, I had an idea to make a blind command substitution.

This one with single quotes,

pid='1024 --help `touch /tmp/helw`' piduser=$(ps -ouser -p$pid h)

does not create the file /tmp/helw

But this one with double quotes,

pid="1024 --help `touch /tmp/helw`" piduser=$(ps -ouser -p$pid h)

creates the file.

My problem is, how can i make command substitution without let it run in pid - variable, but in piduser variable.

答案1

得分: 2

首先,不要这样做。分开的操作应该分开执行。

touch /tmp/helw && ...

但是如果出于某种奇怪的原因确实需要这样做(我无法想象为什么),在 $(...) 内执行的任何操作都是一个子shell,可以包含多个命令。

pid='1024 --help'
piduser=$(touch /tmp/helw && ps -ouser -p$pid h)
英文:

First, don't. Separate actions can and should be separate actions.

touch /tmp/helw && ...

...but if for some odd reason this is actually necessary (I can't imagine why), anything done inside $(...) is a subshell and can be several commands.

pid='1024 --help ' piduser=$( touch /tmp/helw && ps -ouser -p$pid h )

huangapple
  • 本文由 发表于 2020年1月3日 22:01:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定