简体   繁体   English

根据目录中的现有文件运行linux命令

[英]Run linux command based on existing file in a directory

In /dev/usb directory sometimes I have lp0 and sometimes lp1 . /dev/usb目录中有时我有lp0 ,有时lp1 How can I change the following command to change the --name based on the content of /dev/usb ? 如何更改以下命令以根据/dev/usb的内容更改--name Ie if I have lp0 in the /dev/usb directory I get: 即如果我在/dev/usb目录中有lp0 ,我得到:

TEST=`udevadm info --attribute-walk --name=/dev/usb/lp0 | grep "serial}==\"VL" | sed -n -e 's/^.*serial}==\"VL//p' | sed 's/\"//g'`

and if there is lp1 in /dev/usb/ directory: 如果/ dev / usb /目录中有lp1:

TEST=`udevadm info --attribute-walk --name=/dev/usb/lp1 | grep "serial}==\"VL" | sed -n -e 's/^.*serial}==\"VL//p' | sed 's/\"//g'`

How can I save the content of /dev/usb in a variable (lp0 or lp1) and then use it in the above command? 如何将/ dev / usb的内容保存在变量(lp0或lp1)中,然后在上面的命令中使用它?

You can test for readability of the lpX devices as follows: 您可以按如下方式测试lpX设备的可读性:

 if test -r /dev/usb/lp0; then
    NAME=/dev/usb/lp0
 elif test -r /dev/usb/lp1; then
    NAME=/dev/usb/lp1
 else
    printf '%s\n' "could not find lp0 or lp1" >&2
    exit 1
 fi
 TEST=$(udevadm info --attribute-walk --name=$NAME ...)

You could write: 你可以写:

lp=( /dev/usb/lp? )
TEST=$( udevadm info --attribute-walk --name=${f[0]} ... )

the ? ? wildcard matches any 1 character. 通配符匹配任何1个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM