#!perl -w use strict; # $Id: shell-quote,v 1.3 2010-06-11 20:00:24 roderick Exp $ # # Roderick Schertler # Copyright (C) 1999 Roderick Schertler # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or (at # your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # For a copy of the GNU General Public License write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA use String::ShellQuote qw(shell_quote); (my $Me = $0 ne '-e' ? $0 : $^X) =~ s-.*/--; my $Debug = 0; my $Exit = 0; my $Version = q$Revision: 1.3 $ =~ /(\d\S+)/ ? $1 : '?'; my @Option_spec = ( 'debug!' => \$Debug, 'help!' => sub { usage() }, 'version' => sub { print "$Me version $Version\n"; exit }, ); my $Usage = <VERSION(2.19); Getopt::Long::Configure( 'no_auto_abbrev', 'no_getopt_compat', 'require_order', $bundling ? 'bundling' : (), 'no_ignore_case', 'prefix_pattern=(--|-)', ) if 1; # The getopt function puts the vars into its caller's package so # it's necessary to jump to it so that its caller is my caller. #goto &Getopt::Long::GetOptions; Getopt::Long::GetOptions(@_); } sub init { getopt -bundle, @Option_spec or usage if @ARGV; } sub main { init; print shell_quote(@ARGV), "\n" if @ARGV; return 0; } $Exit = main || $Exit; $Exit = 1 if $Exit && !($Exit % 256); exit $Exit; __END__ =head1 NAME shell-quote - quote arguments for safe use, unmodified in a shell command =head1 SYNOPSIS B [I]... I... =head1 DESCRIPTION B lets you pass arbitrary strings through the shell so that they won't be changed by the shell. This lets you process commands or files with embedded white space or shell globbing characters safely. Here are a few examples. =head1 EXAMPLES =over =item B When running a remote command with ssh, ssh doesn't preserve the separate arguments it receives. It just joins them with spaces and passes them to C<$SHELL -c>. This doesn't work as intended: ssh host touch 'hi there' # fails It creates 2 files, F and F. Instead, do this: cmd=`shell-quote touch 'hi there'` ssh host "$cmd" This gives you just 1 file, F. =item B It's not ordinarily possible to process an arbitrary list of files output by B with a shell script. Anything you put in $IFS to split up the output could legitimately be in a file's name. Here's how you can do it using B: eval set -- `find -type f -print0 | xargs -0 shell-quote --` =item B B is better than B for debugging shell scripts. debug() { [ -z "$debug" ] || shell-quote "debug:" "$@" } With B you can't tell the difference between C and C, but with B you can. =item B B can be used to build up a shell command to run later. Say you want the user to be able to give you switches for a command you're going to run. If you don't want the switches to be re-evaluated by the shell (which is usually a good idea, else there are things the user can't pass through), you can do something like this: user_switches= while [ $# != 0 ] do case x$1 in x--pass-through) [ $# -gt 1 ] || die "need an argument for $1" user_switches="$user_switches "`shell-quote -- "$2"` shift;; # process other switches esac shift done # later eval "shell-quote some-command $user_switches my args" =back =head1 OPTIONS =over 4 =item B<--debug> Turn debugging on. =item B<--help> Show the usage message and die. =item B<--version> Show the version number and exit. =back =head1 AVAILABILITY The code is licensed under the GNU GPL. Check http://www.argon.org/~roderick/ or CPAN for updated versions. =head1 AUTHOR Roderick Schertler =cut