官术网_书友最值得收藏!

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:

  1. 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: }
    
  2. 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.

主站蜘蛛池模板: 宁安市| 明溪县| 新巴尔虎右旗| 蓬溪县| 天全县| 九龙坡区| 中牟县| 故城县| 淮阳县| 新民市| 新巴尔虎左旗| 藁城市| 奉节县| 尖扎县| 罗江县| 克什克腾旗| 灵台县| 雷山县| 奎屯市| 凌海市| 江北区| 庄浪县| 芦溪县| 高密市| 万州区| 尼木县| 兴海县| 静宁县| 青河县| 清河县| 毕节市| 拉萨市| 平昌县| 谷城县| 台前县| 麻城市| 新野县| 建德市| 礼泉县| 长宁县| 龙岩市|