• Rex::Misc::ShellBlock 内容


    # (c) Jan Gehring <jan.gehring@gmail.com>
    # 
    # vim: set ts=3 sw=3 tw=0:
    # vim: set expandtab:
       
    package Rex::Misc::ShellBlock;
    
    use strict;
    use warnings;
    
    use Rex::Commands;
    use Rex::Commands::Fs;
    use Rex::Commands::File;
    use Rex::Commands::Run;
    
    require Exporter;
    
    use vars qw(@EXPORT);
    @EXPORT = qw(shell_block);
    use base qw(Exporter);
    
    sub shell_block {
    
       my ($shebang, $code) = @_;
    
       if(! $code) {
          $code = $shebang;
          $shebang = "#!/bin/bash";
       }
    
       if($shebang !~ m/^#!//) {
          $shebang = "#!$shebang";
       }
    
       my @lines = split(/
    /, $code);
       if($lines[0] !~ m/^#!//) {
          # shebang not there, so add /bin/bash for default
          unshift(@lines, $shebang);
       }
    
       my $rnd_file = "/tmp/" . get_random(8, 'a' .. 'z') . ".tmp";
    
       file $rnd_file,
          content => join("
    ", @lines),
          mode => 755;
    
       my $ret = run "$rnd_file 2>&1";
    
       my $ret_code = $?;
    
       unlink $rnd_file;
    
       $? = $ret_code;
    
       return $ret;
    
    }
    
    1;
    
    =pod
    =head1 NAME
    Rex::Misc::ShellBlock - Module to execute a shell block.
    =head1 DESCRIPTION
    This module exports a function called I<shell_block>. This function will upload your shell script to the remote system and executes it. Returning its output as a string.
    =head1 EXPORTED FUNCTIONS
    =over 4
    =item shell_block($code)
    This function will add a default shebang of '#!/bin/bash' to your code if no shebang is found and return its output.
     my $ret = shell_block <<EOF;
     echo "hi"
     EOF
    =item shell_block($shebang, $code)
    This function will add $shebang to your code and return its output.
     my $ret = shell_block "/bin/sh", <<EOF;
     echo "hi"
     EOF
    =back
    =head1 USAGE
     use Rex::Misc::ShellBlock;
        
     task "myexec", sub {
        shell_block <<EOF;
     echo "hi"
     EOF
      
     };

  • 相关阅读:
    CSS3——过渡
    CSS——优雅降级和渐进增强
    jq1 颜色填充器 和清空指定颜色
    1. 初识node
    javaSE- 01
    鼠标悬浮图片时弹出透明提示图层的jQuery特效
    通过jQuery制作电子时钟表的代码
    9种网页Flash焦点图和jQuery焦点图幻灯片
    7种网页图片切换方式代码
    21种网页在线客服代码实例演示
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349748.html
Copyright © 2020-2023  润新知