bash或perl:切换内部键盘脚本

bash或perl:切换内部键盘脚本,bash,perl,keyboard,Bash,Perl,Keyboard,不太熟悉perl或bash脚本,试图编写一个脚本来切换内部键盘(在笔记本电脑上使用外部机械键盘) 使用xinput命令切换内部键盘非常简单: $ xinput ⎡ Virtual core pointer id=2 [master pointer (3)] ⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)] ⎜ ↳

不太熟悉perl或bash脚本,试图编写一个脚本来切换内部键盘(在笔记本电脑上使用外部机械键盘)

使用xinput命令切换内部键盘非常简单:

$ xinput
    ⎡ Virtual core pointer                      id=2    [master pointer  (3)]
    ⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
    ⎜   ↳ Elan Touchpad                             id=11   [slave  pointer  (2)]
    ⎜   ↳ SINO WEALTH USB KEYBOARD                  id=15   [slave  pointer  (2)]
    ⎣ Virtual core keyboard                     id=3    [master keyboard (2)]
        ↳ Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
        ↳ Power Button                              id=6    [slave  keyboard (3)]
        ↳ Video Bus                                 id=7    [slave  keyboard (3)]
        ↳ Video Bus                                 id=8    [slave  keyboard (3)]
        ↳ Sleep Button                              id=9    [slave  keyboard (3)]
        ↳ USB2.0 VGA UVC WebCam                     id=10   [slave  keyboard (3)]
        ↳ Asus WMI hotkeys                          id=12   [slave  keyboard (3)]
        ↳ AT Translated Set 2 keyboard              id=13   [slave  keyboard (3)]
        ↳ SINO WEALTH USB KEYBOARD                  id=14   [slave  keyboard (3)]
识别AT转换的Set 2键盘(内置键盘)的id和浮点数

要重新连接键盘,请使用:

$ xinput reattach 13 3
这里没有什么大问题,只是想写一个脚本来自动完成,到目前为止,我有一些模拟代码如下:

#bin/bash

val string = xinput | grep "AT keyboard"

if(parse(string).attached)
    val floatPartNumber = parse(string).id
    val reattachNumber = parse(string).slaveNumber

    # float keyboard
    xinput float floatPartNumber

    # create hidden file for storage
    file = makeFile(".reattachData")

    #add needed data to file
    writeTOFile(file)(floatPartNumber)
    writeTOFile(file)(reattachNumber)
fi
else
    val file = openFile(".reattachData") 
    val floatPartNumber = file.lineOne
    val reattachNumber = file.lineTwo

    # reattach keyboard
    xinput reattach floatPartNumber reattachNumber
end
我想将其转换为bash或perl脚本,这是一种易于运行的脚本,无需从shell跳转

或者,在创建适当的语法时,我可以参考哪些好的在线资源?
或者这个脚本的功能版本会是什么样子?

我不知道xinput的输出,但是基于它提供了与您提供的一样的行的假设,它将是沿着这些行的东西(现在无法测试它):

使用严格;
使用警告;
my$search='在翻译集2键盘上';
my@xout=qx(xinput)#或者使用反勾号代替qx
我的($id,$slave)=映射{#搜索匹配并捕获数字
/$search.*id=(\d+)\s+\[(?:浮动从机|从机键盘\(\d+)\]/
? ($1,$2) : ()
}@xout;
if(defined($slave)){#在“浮动slave”的情况下应该是未定义的
如果(打开(my$fh,“>”,“.reattachData”)){
$fh->打印($id.$/.$slave);
$fh->close;
}
qx(xinput float$id);
}
否则{
我的@line=do{local(@ARGV)='.reattachData';()};#读取文件
chomp(@line);
qx(xinput重新连接$line[0]$line[1]);
}

当然,这是假设您的搜索词总是被找到的。这里没有错误处理…

谢谢这就像一个符咒,对于我来说没有错误处理也没关系,因为这是一个非常可预测的特殊情况,当我运行这个脚本时,硬编码字符串几乎没有不确定性。
#bin/bash

val string = xinput | grep "AT keyboard"

if(parse(string).attached)
    val floatPartNumber = parse(string).id
    val reattachNumber = parse(string).slaveNumber

    # float keyboard
    xinput float floatPartNumber

    # create hidden file for storage
    file = makeFile(".reattachData")

    #add needed data to file
    writeTOFile(file)(floatPartNumber)
    writeTOFile(file)(reattachNumber)
fi
else
    val file = openFile(".reattachData") 
    val floatPartNumber = file.lineOne
    val reattachNumber = file.lineTwo

    # reattach keyboard
    xinput reattach floatPartNumber reattachNumber
end
use strict;
use warnings;

my $search = 'AT Translated Set 2 keyboard';
my @xout = qx(xinput); # or use backticks instead of qx

my ($id,$slave) = map { # search for match and capture the numbers
    /$search.*id=(\d+)\s+\[(?:floating slave|slave  keyboard \((\d+)\))\]/
    ? ($1,$2) : ()
    } @xout;

if ( defined($slave) ) { # should be undef in case of 'floating slave'
    if (open(my $fh, '>', '.reattachData')) {
        $fh->print($id.$/.$slave);
        $fh->close;
    }
    qx(xinput float $id);
}
else {
    my @line = do { local(@ARGV) = '.reattachData';(<>) }; # read file
    chomp(@line);
    qx(xinput reattach $line[0] $line[1]);  
}