Passing arguments to shell commands
If you want to insert values into a command line (to be run by an exec
resource, for example), they often need to be quoted, especially if they contain spaces. The shellquote
function will take any number of arguments, including arrays, and quote each of the arguments and return them all as a space-separated string that you can pass to commands.
In this example, we would like to set up an exec
resource that will rename a file; but both the source and the target name contain spaces, so they need to be correctly quoted in the command line.
How to do it…
Here's an example of using the shellquote
function:
- Create a
shellquote.pp
manifest with the following command:$source = 'Hello Jerry' $target = 'Hello... Newman' $argstring = shellquote($source, $target) $command = "/bin/mv ${argstring}" notify { $command: }
- Run Puppet:
$ puppet apply shellquote.pp ... Notice: /bin/mv "Hello Jerry" "Hello... Newman" Notice: /Stage[main]/Main/Notify[/bin/mv "Hello Jerry" "Hello... Newman"]/message: defined 'message' as '/bin/mv "Hello Jerry" "Hello... Newman"'
How it works…
First we define the $source
and $target
variables, which are the two filenames we want to use in the command line:
$source = 'Hello Jerry' $target = 'Hello... Newman'
Then we call shellquote
to concatenate these variables into a quoted, space-separated string as follows:
$argstring = shellquote($source, $target)
Then we put together the final command line:
$command = "/bin/mv ${argstring}"
The result will be:
/bin/mv "Hello Jerry" "Hello... Newman"
This command line can now be run with an exec resource. What would happen if we didn't use shellquote
?
$source = 'Hello Jerry' $target = 'Hello... Newman' $command = "/bin/mv ${source} ${target}" notify { $command: } Notice: /bin/mv Hello Jerry Hello... Newman
This won't work because mv
expects space-separated arguments, so it will interpret this as a request to move three files Hello
, Jerry
, and Hello...
into a directory named Newman
, which probably isn't what we want.
- 數(shù)據(jù)展現(xiàn)的藝術(shù)
- 后稀缺:自動(dòng)化與未來(lái)工作
- 人工智能超越人類
- 工業(yè)機(jī)器人工程應(yīng)用虛擬仿真教程:MotoSim EG-VRC
- 模型制作
- PostgreSQL Administration Essentials
- Implementing Oracle API Platform Cloud Service
- 自動(dòng)控制理論(非自動(dòng)化專業(yè))
- Python:Data Analytics and Visualization
- Machine Learning with the Elastic Stack
- 人工智能技術(shù)入門
- Spark大數(shù)據(jù)商業(yè)實(shí)戰(zhàn)三部曲:內(nèi)核解密|商業(yè)案例|性能調(diào)優(yōu)
- MongoDB 4 Quick Start Guide
- 大型機(jī)系統(tǒng)應(yīng)用基礎(chǔ)
- 基于元胞自動(dòng)機(jī)的人群疏散系統(tǒng)建模與分析