英文:
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 )
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论