PDA

View Full Version : [SOLVED] else command script not working



prao060
January 1st, 2014, 12:29 PM
#!/bin/bash
iec9581on="
Simple mixer control 'IEC958',1
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [on]
"
if [ "$amixer get 'IEC958',1" = "$iec9581on" ];
then
amixer set 'IEC958',1 off
else
echo stuffed up
fi


are there issues with the script? the idea is to scan the output of "amixer get 'IEC958',1" and if it is on, execute "amixer set 'IEC958',1 off". for some reason this is not working. Instead, it always outputs "stuffed up", even when "amixer get 'IEC958',1" gives the message i assigned to "$iec958on" If you could modify the script to work and then explain your changes, that would be greatly appreciated.
Thanks in advance

sudodus
January 1st, 2014, 01:49 PM
Maybe you forgot the parenthesis. Try


"$(amixer get 'IEC958',1)"

prao060
January 1st, 2014, 02:36 PM
nah, didn't work. im still having trouble
EDIT- this worked. Massive mind screwup when i said this didn't. i just didn't do this properly :) I LOVE U SUDODUS

Dave_L
January 1st, 2014, 03:00 PM
Running the script with some debugging options may help:


sh -vx script

steeldriver
January 1st, 2014, 03:08 PM
aside for the issue mentioned by sudodus, I think your other issue is the multiline string match - in particular your



iec9581on="
Simple mixer control 'IEC958',1
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [on]
"


has an extra newline before and after - so it's never going to match with the "$(amixer get 'IEC958',1)" output, youd need to do EXACTLY



iec9581on="Simple mixer control 'IEC958',1
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [on]"


Matching long strings like this is something I would try to avoid personally - is there a single field that you could extract and match against?

prao060
January 1st, 2014, 03:12 PM
sorry guys, false alarm. The first answer actually did work. I just needed to experiment a bit more. finally, here is my finalised script. It belongs on my desktop, and i set it to also output a log file there. I am very proud of this :) im just new to scripting.

#!/bin/bash
{ iec9581on="Simple mixer control 'IEC958',1
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [on]"
iec9581off="Simple mixer control 'IEC958',1
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [off]"
if [ "$(amixer get 'IEC958',1)" = "$iec9581on" ]; then
amixer set 'IEC958',1 off
elif [ "$(amixer get 'IEC958',1)" = "$iec9581off" ]; then
echo FIRST ONE was already off
else
echo FIRST ONE had a problem
fi
iec9582on="Simple mixer control 'IEC958',2
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [on]"
iec9582off="Simple mixer control 'IEC958',2
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [off]"
if [ "$(amixer get 'IEC958',2)" = "$iec9582on" ]; then
amixer set 'IEC958',2 off
elif [ "$(amixer get 'IEC958',2)" = "$iec9582off" ]; then
echo SECOND ONE was already of
else
echo SECOND ONE had a problem
fi
iec95816on="Simple mixer control 'IEC958',16
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [on]"
iec95816off="Simple mixer control 'IEC958',16
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [off]"
if [ "$(amixer get 'IEC958',16)" = "$iec95816on" ]; then
amixer set 'IEC958',16 off
elif [ "$(amixer get 'IEC958',16)" = "$iec95816off" ]; then
echo THIRD ONE was already off
else
echo THIRD ONE had a problem
fi } > ~/Desktop/amixerredlight.log

however, to all who replied after, Thanks for your posts and they have given me a lot to work with after :)

prao060
January 1st, 2014, 03:14 PM
steeldriver- no, there isn't. I knew that would be easier, however, amixer wont give me a single field that would define on and off. The only way i could find was this lengthy field. Although, your shortening of the field helped as i was unable to perform this without it. Thanks a lot :D

Dave_L- I didn't know about this. Very interesting. yessss...... This is something i was looking for

steeldriver
January 1st, 2014, 03:31 PM
Could you not do it with a regex match? something like



$ if [[ $(amixer get "'IEC958',0") =~ \[on\] ]]; then echo "On"; else echo "Not on"; fi


If you need case insensitive matching (on/ON) you can try setting the nocasematch shopt

prao060
January 1st, 2014, 03:44 PM
I only know of regex. I will look into it however