3

I have this script (just copy and paste it into shell)

perl -c <(cat <<'EOF'
#!/usr/bin/perl
while( @mylist>1){
if($i > $initnum) {$i--;}
   {splice( @mylist,1);}
}
EOF
)

On one linux machine, I got /dev/fd/63 syntax OK output, which is ok. But on macbook terminal, I saw this

$ perl -c <(cat <<'EOF'
> #!/usr/bin/perl
> while( @mylist>1){
> if($i > $initnum) {$i--;}
>    {splice( @mylist,1);}
> }
> EOF
> )
-bash: bad substitution: no closing `)' in <(cat <<'EOF'
#!/usr/bin/perl
while( @mylist>1){
if($i > $initnum) {$i--;}
   splice( @mylist
}
EOF
)

My question is why such error. And ideally how to fix it on mac.

bishop
  • 37,830
  • 11
  • 104
  • 139
Qiang Li
  • 10,593
  • 21
  • 77
  • 148
  • `bash --version` on each machine, please. – bishop Sep 09 '15 at 14:05
  • Appears to be a parser bug; the error occurs in version 3.2, but not in 4.3. – chepner Sep 09 '15 at 14:08
  • On mac version is `GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13)`, on linux is `GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)` – Qiang Li Sep 09 '15 at 14:14
  • @chepner is this a known bug? – Qiang Li Sep 09 '15 at 14:31
  • I'd assume so, unless some other change in the parser in bash 4 fixed it by coincidence. If what you're really asking is, "Will it be fixed in 3.2?", I'd say the answer is "no". The 3.x branch will get security patches for serious bugs (like the Shellshock vulnerability), but it's unlikely to get bug fixes. – chepner Sep 09 '15 at 14:38
  • @chepner Based on this [SE post](http://unix.stackexchange.com/q/137506/50240), I don't think this is a bug to 3.2 as it's reported in 4.2 as well. – bishop Sep 09 '15 at 14:42
  • Ah, I didn't check to see when the bug was fixed. In any case, it's almost certain that 3.2 is stuck with it, unless you can find a way to exploit it to run arbitrary code :) – chepner Sep 09 '15 at 14:44

2 Answers2

3

It's a parser bug in bash 3.2, but your example is a quite torturous replacement for a simple here document:

perl -c <<'EOF'
#!/usr/bin/perl
while( @mylist>1){
if($i > $initnum) {$i--;}
   {splice( @mylist,1);}
}
EOF

It does not seem likely that Apple will ever ship a newer version of bash by default, so your best bet is to install one yourself (via Homebrew, for example).

chepner
  • 497,756
  • 71
  • 530
  • 681
2

Likely an incompatible version (or broken version as @chepner commented) of bash on your MacOS, so you can naively rewrite it with echo:

echo '#!/usr/bin/perl
while( @mylist>1){
if($i > $initnum) {$i--;}
    {splice( @mylist,1);}
}
' | perl -c

Or if you need to keep the cat business, you can bust it up into a pipeline:

cat <<'EOF' | perl -c
#!/usr/bin/perl
while( @mylist>1){
if($i > $initnum) {$i--;}
   {splice( @mylist,1);}
}
EOF
Community
  • 1
  • 1
bishop
  • 37,830
  • 11
  • 104
  • 139