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

时间:2021-05-26 01:02:35

In /dev/usb directory sometimes I have lp0 and sometimes lp1. How can I change the following command to change the --name based on the content of /dev/usb? I.e. if I have lp0 in the /dev/usb directory I get:

在/ dev / usb目录中,有时候我有lp0,有时候有lp1。如何更改以下命令以根据/ dev / usb的内容更改--name?即如果我在/ 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)中,然后在上面的命令中使用它?

2 个解决方案

#1


0  

You could write:

你可以写:

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

the ? wildcard matches any 1 character.

的?通配符匹配任何1个字符。

#2


0  

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 ...)

#1


0  

You could write:

你可以写:

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

the ? wildcard matches any 1 character.

的?通配符匹配任何1个字符。

#2


0  

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 ...)