Sunday, May 5, 2024
21
rated 0 times [  21] [ 0]  / answers: 1 / hits: 69638  / 3 Years ago, tue, july 6, 2021, 3:23:09

I have following command:



usb_soundcard_sink=$(pactl list short sinks | grep "alsa_output" | awk '{ print $2 }' | tail -n1)


output of this command is:



alsa_output.pci-0000_00_1b.0.analog-stereo


And this is another command that find index number:



var=$(pactl list short sinks | awk '$2=="alsa_output.pci-0000_00_1b.0.analog-stereo" {print $1}')


output:



0


But I want pass the "usb_soundcard_sink" variable instead of hard coded value i.e. "alsa_output.pci-0000_00_1b.0.analog-stereo" in above command. b'coz value of "usb_soundcard_sink" variable may change dynamically.



Also I tried the following:



var=$(pactl list short sinks | awk '$2=="$usb_soundcard_sink" {print $1}')


But it is not working



so how can I pass value of "usb_soundcard_sink" variable to the above command


More From » command-line

 Answers
4

To pass a inside into your awk code, you have to use the -v syntax like this:


$ awk -v myvar="3" 'BEGIN{print myvar}'
# __________/ ___/
3

In your specific case, just use:


pactl list short sinks | awk -v myvar="$usb_soundcard_sink" '$2==myvar {print $1}'
# ___/ ___/
# |________________________________|

[#25597] Wednesday, July 7, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
termetalli

Total Points: 326
Total Questions: 127
Total Answers: 110

Location: Sao Tome and Principe
Member since Sat, Sep 12, 2020
4 Years ago
;