Given two shell variables, A and B, each of which is a space-separated list of numbers, how would you iterate over the lists in parallel, comparing each pair? That is, compare number A1 to number B1, A2 to B2, and so on. You may not use features not available on old systems (such as functions, arrays, and local variables) and you may not mess with the positional parameters. Comprehensibility is not important.
no subject
Date: 2008-05-26 05:50 am (UTC)#!/usr/bin/perl
use warnings;
use strict;
print ($ARGV[0] eq $ARGV[1] ? "Yes\n" : "No\n");
The following bash shell script seems to do the trick on OS X:
TEST=./test.pl
A="3 56 922 1 2 333"
B="73 1 33 -9 2 1"
LENGTH=`echo $A | tr ' ' '\n' | wc -l | sed 's/ //g'`
for OFFSET in `jot $LENGTH 1 $LENGTH`; do
$TEST `echo $A | cut -d' ' -f$OFFSET` `echo $B | cut -d' ' -f$OFFSET`;
done
This script uses jot, but your system might not have jot, so you'll have to use something like seq.
no subject
Date: 2008-05-26 05:51 am (UTC)no subject
Date: 2008-05-26 04:22 pm (UTC)perl is cheating?
Date: 2008-05-26 06:46 am (UTC)perl -lwe 'my @first = split " ", $ARGV[0];
my @second = split " ", $ARGV[1];
my $cmd = $ARGV[2];
while ( @first && @second ) {
system $cmd, shift(@first), shift(@second);
}' "$var1" "$var2" "do stuff with "
Re: perl is cheating?
Date: 2008-05-26 07:07 am (UTC)#!/bin/sh
if [ $1 -eq $2 ]; then echo "Yes"; else echo "No"; fi
Re: perl is cheating?
Date: 2008-05-26 04:23 pm (UTC)no subject
Date: 2008-05-26 04:22 pm (UTC)wc -w, though? That way you don't need the tr with'\n', which is another of the unfortunately.(It's hard to articulate exactly what you can and cannot do in a shell script that has to run on all systems that are still widely enough used to matter. The minimal spec from SUSv2 (not SUSv3 or POSIX-2001) is a baseline, but then you have to enumerate all of the bugs on this or that system. I am oddly pleased that I am no longer as good at this as I used to be.)
no subject
Date: 2008-05-26 05:54 pm (UTC)And even if tr was not available, sed ought to be.
jot came up on my system when I did an apropos seq. I know seq used to be standard on various Unices, but seems to have fallen out of favor, and I'm not quite sure why. Some BSDs ship with seq2 though. You might have to vary the script depending on what's present.
no subject
Date: 2008-05-27 07:45 pm (UTC)seqwas in at least one of the myriad inconsistent standards that cover these things. If some folks haveseq, and others haveseq2, and yet others havejot, that boils down to you can't use any of them in this particular context.no subject
Date: 2008-05-30 06:12 am (UTC)yes '' | head -n $LENGTH | nc -ba
Or, on systems that don't have yes (which, apparently, isn't a POSIX command,):
dd < /dev/zero 2>&- ibs=1 cbs=1 count=$LENGTH conv=unblock | sed -n = (stolen from here, but now added to my bag-of-tricks.)
no subject
Date: 2008-06-02 07:27 am (UTC)no subject
Date: 2008-05-27 07:39 pm (UTC)I suddenly have a strange feeling I know what my cadets were thinking all semester.
no subject
Date: 2008-05-27 07:43 pm (UTC)no subject
Date: 2008-05-27 08:04 pm (UTC)I got that feeling reading your post. I was talking about math. You were apparently talking about shell scripting. I don't even know what you were asking.
no subject
Date: 2008-05-27 08:44 pm (UTC)