12

My application:

#!/bin/sh

#
# D2GS
#

# Go to the directory
cd ~

# Run the applications
if ! ps aux | pgrep "D2GS"; then
    wine "C:/D2GS/D2GS.exe" >& /dev/null &
fi

Gives error:

./d2gs.sh: 14: ./d2gs.sh: Syntax error: Bad fd number

Which is strange, since when I start wine "C:/D2GS/D2GS.exe" >& /dev/null & - it runs without any issues. The reason why I want to start it from shell is, because I want to crontab it every minute.

htorbov
  • 131
  • 1
  • 1
  • 5

2 Answers2

16

>& is not syntax supported by sh. You're explicitly using sh as the shell in that script. You need to rewrite that line as:

wine "C:/D2GS/D2GS.exe" > /dev/null 2>&1 &
DopeGhoti
  • 73,792
  • 8
  • 97
  • 133
8

>& is the csh syntax (also supported by zsh and recent versions of bash) to redirect both stdout and stderr to a file.

In sh (Bourne (where it comes from) and POSIX), the syntax is:

if ! pgrep D2GS > /dev/null; then 
  wine C:/D2GS/D2GS.exe > /dev/null 2>&1 &
fi

(you've got the wrong ps/pgrep syntax as well; pgrep doesn't read its stdin, so piping the output of ps to it is pointless).

For completeness, the syntax to redirect both stdout and stderr in various shells:

  • > file 2>&1: Bourne, POSIX and derivatives and fish
  • >& file: csh, tcsh, zsh and bash 4+ (though in zsh and bash only when the file name is not a sequence of decimal digits, otherwise it's the >&fd Bourne redirection operator).
  • &> file: bash and zsh 3+
  • > file >[2=1]: rc and derivatives
  • > file ^&1: fish
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501