title: "Ambiguous output redirect" trying to send both stdout and stderr to mailx from a command sent to at
body: <p>I have a bash script called <em>test.sh</em> which, for the sake of simplicity, prints one line to <em>stdout</em> and one line to <em>stderr</em>.</p>
  
  <p><strong>test.sh:</strong></p>
  
  <pre><code>#!/bin/bash
  echo "this is to stdout"
  echo "this is to stderr" 1&gt;&amp;2
  </code></pre>
  
  <p>I want to run the script <em>test.sh</em> at 7:00 PM, but only if certain conditions are met. To this end, I have another bash script called <em>schedule.sh</em>, which checks some stuff and then submits the command to <code>at</code> to be run later.</p>
  
  <p>I want the output of <em>test.sh</em> (both <em>stdout</em> and <em>stderr</em>) to be sent to me in an email. I use <code>mailx</code> to do this so I can get a nice subject name.</p>
  
  <p>Furthermore, I want <code>at</code> to shut up. No output from <code>at</code> because it always sends me ugly emails (no subject line) if <code>at</code> produces any output.</p>
  
  <p><strong>schedule.sh:</strong></p>
  
  <pre><code>#!/bin/bash
  my_email="me@example.com" # Email is a variable
  # Check some stuff, exit if certain conditions not met
  echo "~/test.sh 2&gt;&amp;1 | mailx -s\"Cool title\" $my_email" | at 7:00 PM &amp;&gt; /dev/null
  </code></pre>
  
  <p>What's interesting is that when I run <em>schedule.sh</em> from <code>cron</code> (which runs the script with <code>sh</code>), it works perfectly. However, when I manually run <em>schedule.sh</em> from the terminal (NB: I'm using <code>tcsh</code>), <code>at</code> (not <code>mailx</code>) sends me an email saying</p>
  
  <pre><code>Ambiguous output redirect.
  </code></pre>
  
  <p>I'm not sure why the shell I run <em>schedule.sh</em> from makes a difference, when <em>schedule.sh</em> is a bash script.</p>
  
  <p>Here is my thinking in looking at <em>schedule.sh</em>. Everything within the quotation marks <code>"~/test.sh 2&gt;&amp;1 | mailx -s\"Cool title\" me@email.com"</code> should be an argument to <code>at</code>, and <code>at</code> runs that argument as a command using <code>sh</code>. The redirection <code>2&gt;&amp;1 |</code> is in the style of <code>sh</code> for this reason.</p>
  
  <p>When I remove <code>2&gt;&amp;1</code> and only pipe the <em>stdout</em> of <em>test.sh</em> to <code>mailx</code>, it does work; however, I receive 2 emails: one with <em>stdout</em> from <code>mailx</code> and another from <em>stderr</em> from <code>at</code>.</p>
  
  <p>What gives? How can I make this work regardless of the shell I'm calling it from?</p>
  
  <p>Thanks.</p>
  
  <p><strong>edit:</strong></p>
  
  <p><code>uname -o</code> says my OS is <code>GNU/Linux</code></p>
  
  <p>Here is <code>uname -a</code> if it helps:</p>
  
  <pre><code>Linux [hostname censored] 2.6.9-89.ELlargesmp #1 SMP Mon Jun 22 12:46:58 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux
  </code></pre>
  
  <p>When I check the <code>at</code> contents using <code>at -c</code>, here's what I see:</p>
  
  <pre><code>#!/bin/sh
  # atrun uid=xxxxx gid=xxxxx
  # mail  username 0
  # ...
  SHELL=/bin/tcsh; export SHELL
  # ...
  ${SHELL:-/bin/sh} &lt;&lt; `(dd if=/dev/urandom count=200 bs=1 2&gt;/dev/null|LC_ALL=C tr -d -c '[:alnum:]')`
  
  ~/test.sh 2&gt;&amp;1 | mailx -s"Cool title" me@example.com
  </code></pre>
  
  <p>I'm having a hard time understanding the second to last line... is this going to execute using $SHELL or <code>/bin/sh</code>?</p>