You are not logged in.
Hey,
I know next to nothing about perl but I've found this handy perl script that uses the accelerometer in my thinkpad to notice movement and then use sox to produce a siren sound as kind of a theft protection.
Now I want to add two simple conditions to the script.
a) check if a process uses /dev/dsp and if so, kill that process and b) check if the kernel module for the accelerometer is loaded and if not, load it.
This is how i do it in bash.
THEFTMODULE=$(lsmod | grep hdaps)
[[ -z $THEFTMODULE ]] && modprobe hdaps && sleep 1
DSPUSED=$(fuser /dev/dsp)
[[ ! -z $DPSUSED ]] && fuser -k /dev/dsp
Could you help me translate that to perl?
This is what i came up with, but it interprets $module just as plain text and doesn't evaluate.
my $module='lsmod | grep hdaps';
if (!$module) {
print $module;
system "modprobe hdaps";
}
my $dsp='fuser /dev/dsp';
if (!$dsp) {
system "fuser -k /dev/dsp";
}
Regards,
demian
Last edited by demian (2010-06-13 18:13:52)
no place like /home
github
Offline
Would have to be system("lsmod | grep hdaps")
but:
a. perl has a grep() function
b. the assignment will go to stdout
edit:
That was a bit unfair of me
Another hint: try using ` in the assignment, sans system. man perlvar
Last edited by andresp (2010-06-11 17:04:38)
Offline
Why are you using perl for this at all? If you're just going to shell out, it doesn't make sense.
#!/bin/bash
lsmod | grep -q hdaps || modprobe hdaps
fuser /dev/dsp && fuser -k /dev/dsp
[git] | [AURpkgs] | [arch-games]
Offline
i am already using bash as a wrapper to do the above and call the perl script.
i just want the perl script to do those two checks for the kernel module and /dev/dsp too, since it needs those to run anway.
i'll check the manual(s) tomorrow, andresp. thanks for the hints.
Last edited by demian (2010-06-11 20:55:40)
no place like /home
github
Offline
Another hint: try using ` in the assignment, sans system. man perlvar
Sweet, be condescending and give bad advice...
The perlvar manpage describes the built-in variables that affect perl's behavior. What you should read, demian, is the perlop manpage which describes perl's operators. In particular, search for qx/STRING/ under quote-like operators and also lookup I/O Operators which references the qx// operator.
The problem you are having is you are using the ' (single quote) character when you should use the ` (backticks) character. The ` character is right above tab on US keyboards and to the left of backspace on german keyboards (according to wikipedia). `` and the qx// operator are one and the same, by the way. The qx// just lets you use any quoting character.
# Here `lsmod` is in list context and returns the output of lsmod as a list of lines
# grep filters lists, returning only the line that matches the /hdaps/ regexp
unless ( grep { /hdaps/ } `lsmod` ) {
system 'modprobe', ( 'hdaps' ) and die 'modprobe hdaps failed';
}
if ( `fuser /dev/dsp` ) {
system 'fuser', ( '-k' => '/dev/dsp' ) and die 'fuser -k /dev/dsp failed';
}
# system returns 0 on success which is the same as false in perl
# which is why we use 'and die ...' to report an error with the system call
Offline
Thank you, juster. The wikipedia is right and your code works perfectly.
no place like /home
github
Offline