From 16939a0fa6226b04869c84db366ff1e01c21a5c7 Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Sat, 28 Aug 2010 02:46:44 +0200 Subject: [PATCH] --shebang implemented. Unittest passes. --- .gitignore | 7 +++++ doc/FUTURE_IDEAS | 11 ++++++-- src/parallel | 46 +++++++++++++++++++++++++++++++-- src/sql | 2 +- unittest/tests-to-run/test31.sh | 23 +++++++++++++++++ unittest/wanted-results/test31 | 8 ++++++ 6 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 unittest/tests-to-run/test31.sh create mode 100644 unittest/wanted-results/test31 diff --git a/.gitignore b/.gitignore index 1c5ead38..17d38ce0 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,14 @@ config.h config.log config.status parallel-*.bz2* +parallel-*.tar.gz stamp-h1 actual-results tmp video +src/parallel.1 +src/parallel.html +src/sem.1 +src/sem.html +src/sql.1 +src/sql.html diff --git a/doc/FUTURE_IDEAS b/doc/FUTURE_IDEAS index ccb1fd2a..aad98cc0 100644 --- a/doc/FUTURE_IDEAS +++ b/doc/FUTURE_IDEAS @@ -2,10 +2,17 @@ fex syntax for splitting fields http://www.semicomplete.com/projects/fex/ sql :foo 'select * from bar' | parallel --fex '|{1,2}' do_stuff {2} {1} -Import sql - +Example: sql :foo 'select * from bar' | parallel --colsep '\|' do_stuff {4} {1} +Virker shebang? +#!/usr/bin/sql -t + +GNU kræver: +--version +--help +-h + --autocolsep: Læs alle linjer. Prøv fastlængde: Find tegn, som står i alle linjer på de samme pladser. Risiko for falske pos diff --git a/src/parallel b/src/parallel index c2f03cbb..76b214d7 100755 --- a/src/parallel +++ b/src/parallel @@ -640,6 +640,12 @@ If the semaphore is not released within secs seconds, take it anyway. Implies B<--semaphore>. +=item B<--skip-first-line> + +Do not use the first line of input (used by GNU B itself +when called with B<--shebang>). + + =item B<-S> I<[ncpu/]sshlogin[,[ncpu/]sshlogin[,...]]> =item B<--sshlogin> I<[ncpu/]sshlogin[,[ncpu/]sshlogin[,...]]> @@ -841,6 +847,21 @@ Support for B<-X> with B<--sshlogin> is limited and may fail. Exit if the size (see the B<-s> option) is exceeded. +=item B<--shebang> + +=item B<-Y> + +GNU B can be called as a shebang (#!) command as the first line of a script. Like this: + + #!/usr/bin/parallel -Yr traceroute + + foss.org.my + debian.org + freenetproject.org + +For this to work B<--shebang> or B<-Y> must be set as the first option. + + =back =head1 EXAMPLE: Working as xargs -n1. Argument appending @@ -2191,6 +2212,17 @@ sub parse_options { $Global::arg_file_sep = "::::"; $Global::trim = 'n'; + # This must be done first as this may exec myself + if(defined $ARGV[0] and ($ARGV[0]=~/^-Y/ or $ARGV[0]=~/^--shebang /)) { + # Program is called from #! line in script + $ARGV[0]=~s/^-Y //; # remove -Y if on its own + $ARGV[0]=~s/^-Y/-/; # remove -Y if bundled with other options + $ARGV[0]=~s/^--shebang //; # remove --shebang if it is set + my $argfile = pop @ARGV; + # exec myself to split @ARGV into separate fields + exec "$0 --skip-first-line -a $argfile @ARGV"; + } + Getopt::Long::Configure ("bundling","require_order"); # Add options from .parallel/config my $parallel_config = $ENV{'HOME'}."/.parallel/config"; @@ -2265,6 +2297,9 @@ sub parse_options { "fg" => \$::opt_fg, "bg" => \$::opt_bg, "wait" => \$::opt_wait, + # Shebang #!/usr/bin/parallel -Yotheroptions + "Y|shebang" => \$::opt_shebang, + "skip-first-line" => \$::opt_skip_first_line, ) || die_usage(); $Global::debug = (defined $::opt_D); if(defined $::opt_m) { $Global::xargs = 1; } @@ -2334,6 +2369,9 @@ sub parse_options { if($#::opt_a == 0) { # One -a => xargs compatibility $Global::argfile = open_or_exit($::opt_a[0]); + if($::opt_skip_first_line) { + <$Global::argfile>; # Read first line and forget it + } } else { # Multiple -a => xapply style argfiles_xapply_style(); @@ -2489,6 +2527,9 @@ sub argfiles_xapply_style { my $in_fh = gensym; for (my $fileno = 0; $fileno <= $#::opt_a; $fileno++) { $in_fh = open_or_exit($::opt_a[$fileno]); + if($::opt_skip_first_line and $fileno == 0) { + <$in_fh>; # Read first line and forget it + } for (my $lineno=0; $content[$fileno][$lineno] = get_arg($in_fh); $lineno++) { @@ -4530,6 +4571,7 @@ sub unlock { } # Keep perl -w happy -$Private::control_path = $Semaphore::timeout = -$Semaphore::wait = 0; + +$Private::control_path = $Semaphore::timeout = $Semaphore::wait = +$::opt_skip_first_line = $::opt_shebang = 0 ; diff --git a/src/sql b/src/sql index 838f6b7c..fd172809 100755 --- a/src/sql +++ b/src/sql @@ -593,7 +593,7 @@ sub Usage { print map{ "$_\n" } @_; print "\n"; } - print `cat $0 | pod2man | man -l -`; + print "sql [-hnr] [--table-size] [--db-size] [-p pass-through] [-s string] dburl [command]\n"; } sub get_alias { diff --git a/unittest/tests-to-run/test31.sh b/unittest/tests-to-run/test31.sh new file mode 100644 index 00000000..5a0e89a0 --- /dev/null +++ b/unittest/tests-to-run/test31.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +echo '### Test of #! with file as input' +cat >/tmp/shebang </tmp/shebang </tmp/123 +/tmp/shebang + diff --git a/unittest/wanted-results/test31 b/unittest/wanted-results/test31 new file mode 100644 index 00000000..2099d8c5 --- /dev/null +++ b/unittest/wanted-results/test31 @@ -0,0 +1,8 @@ +### Test of #! with file as input +A +B +C +### Test of #! with 2 files as input +A 1 +B 2 +C 3