[英]simple bash script, how can I make it shorter and better?
I wanna a bash command to move to a directory, and then show 2 files' content for each entry, I made this script: 我想要一个bash命令移动到一个目录,然后为每个条目显示2个文件的内容,我制作了这个脚本:
cd /sys/class/net
for i in *; do echo $i; cat $i/{address,operstate}; done
is there anyway to do shorter and better? 无论如何要做得更短更好吗?
No need for the cd
. 不需要
cd
。 Try the following: 请尝试以下方法:
for i in /sys/class/net/*; do echo "${i##/*/}:"; cat $i/{address,operstate}; done
This should give you an output like this: 这应该给你一个像这样的输出:
eth2:
0f:ed:37:1f:6c:c7
up
lo:
00:00:00:00:00:00
unknown
wlan0:
94:58:29:b6:07:3e
down
The echo
above removes the leading path elements using bash parameter substitution to make the output prettier. 上面的
echo
使用bash参数替换来删除前导路径元素,以使输出更漂亮。
grep and sort can give a similar answer grep和sort可以给出类似的答案
(cd /sys/class/net/;grep -T '' */{addre,o}*|sort)
Example output 示例输出
eth0/address :10:9a:dd:6e:66:33
eth0/operstate :down
lo/address :00:00:00:00:00:00
lo/operstate :unknown
wlan0/address :e0:f8:47:20:b3:92
wlan0/operstate:up
I really liked the answer using tail, but unfortunately it keep the address and operstate next to each other. 我真的很喜欢使用tail的答案,但不幸的是它保持地址和operstate彼此相邻。 Using grep, I can fix this with sort.
使用grep,我可以通过排序来解决这个问题。 But it's not one command, sorry.
但抱歉,这不是一个命令。
The -T option to grep does the aligning (using a tab) grep的-T选项进行对齐(使用选项卡)
I just found tail can do simpler, 我刚发现尾巴可以做得更简单,
tail /sys/class/net/*/{address,operstate}
this is only one command, without pips and compound shell statements. 这只是一个命令,没有pips和复合shell语句。 :) – fluter just now edit
:) - 刚刚编辑的fluter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.