]> git.proxmox.com Git - pve-common.git/blobdiff - data/PVE/Tools.pm
add shellquote utility function
[pve-common.git] / data / PVE / Tools.pm
index 4c4e25958ad38336ae44b05cce672d9bbbc76f22..78d7b9f2140026d4d6d1d4142773a768a05b017d 100644 (file)
@@ -687,4 +687,26 @@ sub random_ether_addr {
     return $mac;
 }
 
+sub shellquote {
+    my $str = shift;
+
+    return "''" if !defined ($str) || ($str eq '');
+    
+    die "unable to quote string containing null (\\000) bytes"
+       if $str =~ m/\x00/;
+
+    # from String::ShellQuote
+    if ($str =~ m|[^\w!%+,\-./:@^]|) {
+
+       # ' -> '\''
+       $str =~ s/'/'\\''/g;
+
+       $str = "'$str'";
+       $str =~ s/^''//;
+       $str =~ s/''$//;
+    }
+
+    return $str;
+}
+
 1;