Getting no output from command substitution?

EternalHour

I've written a few shell scripts, but have never seen this behavior and am at a loss. I've got the following simple script which runs in a bash shell:

LOGFILE="/var/log/constructor-events.txt"

SUBSYSTEM="$1"
DEVTYPE="$2"
DEVICE="$3"

VENDOR=$(lsusb -D "$DEVICE" | grep idVendor 2>&1)

REQUEST=$(cat <<EOF
{"data": {
    "action": "add",
    "port": {
        "type": "$SUBSYSTEM",
    },
    "drive": {
        "vendor_id": "$VENDOR",
    }
}}
EOF
)

printf "output: $VENDOR" >> $LOGFILE
printf "%s\n" "`date +%x\ %r\ %Z` $REQUEST"  >> $LOGFILE

This is executed from a udev rule. The positional parameters (coming from udev) have the values I expect, and I have no problem printing them to the log file. But for some reason, the $VENDOR variable contains no output from the lsusb command.

Here is the debugging I've done.

  • Added stderr redirection to stdout to capture any error that may be occurring.
  • Added the line to send the $VENDOR variable directly to the log, which is empty.
  • Executed the script manually in the shell and printed the $VENDOR variable to the terminal and it's empty.
  • Took the string that is contained in the $DEVICE variable and executed it directly in the shell, with this result.

    [root@host ~]# lsusb -D /dev/bus/usb/016/030 | grep idVendor
    Cannot open /dev/bus/usb/016/030
    

Why is the $VENDOR variable empty when outputting to the log file?

EDIT:

Here is an update after removing the redirection from the command substitution.

  • Added a line to print the $VENDOR variable to the terminal.
  • Statically assigned the value instead of using the $DEVICE variable.

Printing $VENDOR to the terminal output the error, but it still did not append to the log file! The value of $DEVICE IS in the log file. I believe it has something to do with the output redirection inside the command substitution.

ilkkachu

Here, if lsusb drops an error, the error message is not redirected, only errors from grep. Errors from lsusb still go the the usual stderr, i.e. the terminal (or whatever stderr was when the script started.

VENDOR=$(lsusb -D "$DEVICE" | grep idVendor 2>&1)

E.g.

$ blah=$(ls -l /nonexisting | grep foo 2>&1)
ls: cannot access '/nonexisting': No such file or directory
$ echo "blah: '$blah'"
blah: ''

You'll need to redirect both of their outputs as a group to have the command substitution capture the error, too:

$ blah=$( { ls -l /nonexisting | grep foo; } 2>&1)
$ echo "blah: '$blah'"
blah: 'ls: cannot access '/nonexisting': No such file or directory'

Using separate redirections like $( foo 2>&1 | grep 2>&1 ) would send the errors of the first command to the pipe, and for grep to filter, which in the above example would again produce an empty output.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to capture newlines from command substitution output in fish shell?

From Dev

How to prevent bash command substitution output from being escaped?

From Dev

Testing command substitution output in fish

From Dev

capturing subshell command substitution output

From Dev

Getting a specific part from a PowerShell command output

From Dev

The output of linux command is not getting suppressed from console

From Dev

getting a part of the output from a sed command

From Dev

Getting the output from first command to exit in Bash

From Dev

How do I stop multiple line output from command substitution from being concatenated in BASH script?

From Dev

Store output of sed with command substitution in it into a variable

From Dev

Run output of another command, with variable substitution

From Dev

Bash - Getting output of a command

From Dev

Getting output of command '&'

From Dev

Getting the last x digits from output with grep command

From Dev

python: getting a response from check_output before the command is finished

From Dev

Getting output from running command line app in Inno Setup

From Dev

Getting correct output in a shell script from the curl command

From Dev

python: getting a response from check_output before the command is finished

From Dev

Getting full output from cmd.exe command in VBScript

From Dev

Multiword arguments from backtick (command substitution)

From Dev

echo variable with content from command substitution

From Dev

Multiword arguments from backtick (command substitution)

From Dev

Command substitution with string substitution

From Dev

not getting desired output with cut command?

From Dev

Getting Output of ADSI Command in Powershell

From Dev

getting the output of a grep command in a loop

From Dev

Why is an extra space getting removed in sed substitution command

From Dev

Can parameter expansions be run on command substitution output in one step?

From Dev

How to execute an output string without bash's command substitution?

Related Related

  1. 1

    How to capture newlines from command substitution output in fish shell?

  2. 2

    How to prevent bash command substitution output from being escaped?

  3. 3

    Testing command substitution output in fish

  4. 4

    capturing subshell command substitution output

  5. 5

    Getting a specific part from a PowerShell command output

  6. 6

    The output of linux command is not getting suppressed from console

  7. 7

    getting a part of the output from a sed command

  8. 8

    Getting the output from first command to exit in Bash

  9. 9

    How do I stop multiple line output from command substitution from being concatenated in BASH script?

  10. 10

    Store output of sed with command substitution in it into a variable

  11. 11

    Run output of another command, with variable substitution

  12. 12

    Bash - Getting output of a command

  13. 13

    Getting output of command '&'

  14. 14

    Getting the last x digits from output with grep command

  15. 15

    python: getting a response from check_output before the command is finished

  16. 16

    Getting output from running command line app in Inno Setup

  17. 17

    Getting correct output in a shell script from the curl command

  18. 18

    python: getting a response from check_output before the command is finished

  19. 19

    Getting full output from cmd.exe command in VBScript

  20. 20

    Multiword arguments from backtick (command substitution)

  21. 21

    echo variable with content from command substitution

  22. 22

    Multiword arguments from backtick (command substitution)

  23. 23

    Command substitution with string substitution

  24. 24

    not getting desired output with cut command?

  25. 25

    Getting Output of ADSI Command in Powershell

  26. 26

    getting the output of a grep command in a loop

  27. 27

    Why is an extra space getting removed in sed substitution command

  28. 28

    Can parameter expansions be run on command substitution output in one step?

  29. 29

    How to execute an output string without bash's command substitution?

HotTag

Archive