0

Running the following script as myself works but not when I use sudo. As you can probably tell, the touch error is just to make sure something is written to the log file.

dave@pi1:~ $ ./test.sh
touch: cannot touch '/asdf/asdf/asdf': No such file or directory  <--- As expected
dave@pi1:~ $ sudo ./test.sh
./test.sh: 2: ./test.sh: Syntax error: redirection unexpected  <--- why this not work?
dave@pi1:~ $

The script:

LOG_FILE=/var/log/usbhook
exec > >(tee -a ${LOG_FILE} )
exec 2> >(tee -a ${LOG_FILE} >&2)

touch /asdf/asdf/asdf

The aim is to have a script fired when I plug in a USB stick so I'm assuming it should work when ran using sudo.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
TedTrippin
  • 3,525
  • 5
  • 28
  • 46
  • 4
    Add `#!/usr/bin/env bash` as first line in your script – anubhava Aug 16 '19 at 14:58
  • 1
    Also see [How to use Shellcheck](http://github.com/koalaman/shellcheck). – jww Aug 16 '19 at 15:16
  • // , Do you have a specific question to ask, or more of a "this is what I expect it to do, can you help debug my code?" – Nathan Basanese Aug 17 '19 at 01:34
  • // , Also, welcome to the weird and wonderful world of shell scripting. Here be dragons. – Nathan Basanese Aug 17 '19 at 01:35
  • 1
    @anubhava thanks, that did the trick. – TedTrippin Aug 19 '19 at 08:25
  • it was a long time ago. @TedTrippin please consider + accept VodkaBottle answer. `echo $0` returns the terminal's shell (also `sudo echo $0`). and `grep PPid /proc/self/status | sed -E 's/^(PPid:\s*)(.*)$/\/proc\/\2\/exe/'` returns the program running the current script. – dank8 May 25 '23 at 07:50
  • There's no need to call yourself a "noob". – Andy Lester May 25 '23 at 17:53
  • 1
    @dank8, $0 is not reliably the shell name. It can be the script name, it can be a completely arbitrary string passed at the beginning of argv, etc. – Charles Duffy May 27 '23 at 13:57
  • 1
    And this **is** like the duplicate: `>(` is a syntax element that can't be used in sh. Just because one operator is `>( )` and the other is `<<<` doesn't make it two different questions; it's the same misunderstanding about what it takes to ensure that bash-only syntax is available. – Charles Duffy May 27 '23 at 13:59

1 Answers1

1

Bash: Syntax error: redirection unexpected

^Similar, and might answer your question. From the accepted answer in that thread:

"The default system shell in Ubuntu is dash, not bash, so if you have #!/bin/sh then your script will be using a different shell than you expect. Dash does not have the <<< redirection operator."

  • 1
    I don't believe that's the same. That's about a particular operator which isn't supported on a different Linux flavour – TedTrippin Aug 16 '19 at 19:18
  • // , I agree with Ted that although the _solutions_ might be the same (add a shebang, noob!) the _questions_ could be different. Ted, what's different about your question? – Nathan Basanese Aug 17 '19 at 01:36
  • We don't care if the questions are different, actually. This is a duplicate because the answer is the same. We would have mountains of duplicates if we would allow every minor variation of a basic problem to be a new question. This is bad for the site because then nobody can find the right one in the haystack of very similar questions. – tripleee May 25 '23 at 17:49