本帖最后由 smdx 于 2022-3-11 10:37 编辑
各位大佬好,Linux shell sed代码替换求助,请问如何用sed命令替换代码?
想实现把代码的
a=$(ip address | grep ^[0-9] | awk -F: '{print $2}' | sed "s/ //g" | grep '^[e]' | grep -v "@" | grep -v "\.")
替换成
a=$(ip address | grep ^[0-9] | awk -F: '{print $2}' | sed "s/ //g" | grep '^eth' | awk -F"@" '{print $1}')
命令用于GitHub Actions云编译openwrt固件sh脚本,请问要怎么做?谢谢
ethinfo原代码:- #!/bin/sh
-
- a=$(ip address | grep ^[0-9] | awk -F: '{print $2}' | sed "s/ //g" | grep '^[e]' | grep -v "@" | grep -v "\.")
- b=$(echo "$a" | wc -l)
- rm -f /tmp/state/ethinfo
-
- echo -n "[" > /tmp/state/ethinfo
-
- for i in $(seq 1 $b)
- do
- h=$(echo '{"name":' )
- c=$(echo "$a" | sed -n ${i}p)
- d=$(ethtool $c)
-
- e=$(echo "$d" | grep "Link detected" | awk -F: '{printf $2}' | sed 's/^[ \t]*//g')
- if [ $e = yes ]; then
- l=1
- else
- l=0
- fi
-
- f=$(echo "$d" | grep "Speed" | awk -F: '{printf $2}' | sed 's/^[ \t]*//g' | tr -d "Unknown!")
- [ -z "$f" ] && f=" - "
-
- g=$(echo "$d" | grep "Duplex" | awk -F: '{printf $2}' | sed 's/^[ \t]*//g')
- if [ "$g" == "Full" ]; then
- x=1
- else
- x=0
- fi
-
- echo -n "$h \"$c\", \"status\": $l, \"speed\": \"$f\", \"duplex\": $x}," >> /tmp/state/ethinfo
- done
-
- sed -i 's/.$//' /tmp/state/ethinfo
-
- echo -n "]" >> /tmp/state/ethinfo
-
- cat /tmp/state/ethinfo
复制代码
|