如何把mac地址“da85ece34c7a”转变为“da:85:ec:e3:4c:7a”的格式

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
jiandan23
帖子: 86
注册时间: 2010-12-17 22:31
系统: Mint 19.2

如何把mac地址“da85ece34c7a”转变为“da:85:ec:e3:4c:7a”的格式

#1

帖子 jiandan23 » 2016-11-25 14:43

有没有简单点的办法作这样的转换?
还有,我想问一下为什么以下命令会报错:
-------------------------------------------
[root@mgt ~]# a=da:85:ec:e3:4c:7a
[root@mgt ~]# echo ${a:{0..10..2}:2}
-bash: a: {0..10..2: syntax error: operand expected (error token is "{0..10..2")
[root@mgt ~]#
-------------------------------------------
头像
vickycq
帖子: 4507
注册时间: 2011-03-20 13:12
系统: Debian
来自: 山东省寿光县
联系:

Re: 如何把mac地址“da85ece34c7a”转变为“da:85:ec:e3:4c:7a”的格式

#2

帖子 vickycq » 2016-11-25 14:49

Debian 中文论坛 - forums.debiancn.org
欢迎所有 Debian GNU/Linux 用户
头像
astolia
论坛版主
帖子: 6450
注册时间: 2008-09-18 13:11

Re: 如何把mac地址“da85ece34c7a”转变为“da:85:ec:e3:4c:7a”的格式

#3

帖子 astolia » 2016-11-25 16:34

上面的说了该怎么做,我来说一下你的写法为什么错
本质上就是一个不看文档想当然乱写

代码: 全选

       ${parameter:offset:length}
              Substring  Expansion.  Expands to up to length characters of the
              value of parameter starting at the character specified by  off‐
              set.  If parameter is @, an indexed array subscripted by @ or *,
              or an associative array name, the results  differ  as  described
              below.   If  length  is omitted, expands to the substring of the
              value of parameter starting at the character specified by offset
              and  extending  to  the end of the value.  length and offset are
              arithmetic expressions (see ARITHMETIC EVALUATION below).
上面是bash的manpages原文摘抄,注意最后一句,要求offset必须是算术表达式,也就是说,不管怎么样offset最后总能得到一个数

而你再查查{0..10..2}是得到的是什么

代码: 全选

   Brace Expansion
       ...略去关系不大的两段...
       A  sequence expression takes the form {x..y[..incr]}, where x and y are
       either integers or single characters, and incr, an optional  increment,
       is  an  integer.  When integers are supplied, the expression expands to
       each number between x and y, inclusive.  Supplied integers may be pre‐
       fixed  with 0 to force each term to have the same width.  When either x
       or y begins with a zero, the shell  attempts  to  force  all  generated
       terms  to contain the same number of digits, zero-padding where neces‐
       sary.  When characters are supplied, the  expression  expands  to  each
       character  lexicographically  between  x  and  y,  inclusive, using the
       default C locale.  Note that both x and y must be  of  the  same  type.
       When  the  increment  is supplied, it is used as the difference between
       each term.  The default increment is 1 or -1 as appropriate.

       Brace expansion is performed  before  any  other  expansions,  and  any
       characters special to other expansions are preserved in the result.  It
       is strictly textual.  Bash does not apply any syntactic  interpretation
       to the context of the expansion or the text between the braces.
看明白了吗?重点在最后两句话,{0..10..2}展开得到的0 2 4 6 8 10就只是一个单纯的文本串,不是bash需要的数值,当然是个再明白不过的语法错误
头像
jiandan23
帖子: 86
注册时间: 2010-12-17 22:31
系统: Mint 19.2

Re: 如何把mac地址“da85ece34c7a”转变为“da:85:ec:e3:4c:7a”的格式

#4

帖子 jiandan23 » 2016-11-26 9:02

-----------------------------------------------------
The order of expansions is: brace expansion, tilde expansion, parameter, variable and arithmetic expansion
and command substitution (done in a left-to-right fashion), word splitting, and pathname expansion.
-----------------------------------------------------
按照我的理解,brace expansion会先于variable expansion执行:
step.1 ${a:{0..10..2}:2}会被扩展成:${a:0:2} ${a:2:2} ${a:4:2} ${a:6:2} ${a:8:2} ${a:10:2}
step.2 ${a:0:2}->da, ${a:2:2}->85, ...
这个语法到底错在哪了?
头像
vickycq
帖子: 4507
注册时间: 2011-03-20 13:12
系统: Debian
来自: 山东省寿光县
联系:

Re: 如何把mac地址“da85ece34c7a”转变为“da:85:ec:e3:4c:7a”的格式

#5

帖子 vickycq » 2016-11-26 10:41

瞎试出来了

代码: 全选

$ a=da:85:ec:e3:4c:7a                           
                              
$ echo {a:{0,2,4,6,8,10}:2}                     
{a:0:2} {a:2:2} {a:4:2} {a:6:2} {a:8:2} {a:10:2}

$ echo ${a:{0,2,4,6,8,10}:2}                    
-bash: a: {0,2,4,6,8,10: syntax error: operand expected (error token is "{0,2,4,6,8,10")

$ echo '$'{a:{0,2,4,6,8,10}:2}                  
${a:0:2} ${a:2:2} ${a:4:2} ${a:6:2} ${a:8:2} ${a:10:2}

$ eval echo '$'{a:{0,2,4,6,8,10}:2}             
da :8 5: ec :e 3:

$ b=da85ece34c7a

$ eval echo \${b:{0,2,4,6,8,10}:2}
da 85 ec e3 4c 7a

$ eval echo \${b:{0,2,4,6,8,10}:2} | tr ' '  :
da:85:ec:e3:4c:7a
Debian 中文论坛 - forums.debiancn.org
欢迎所有 Debian GNU/Linux 用户
回复