-3
我试图实现一个简单的shell程序,显示文件中包含的法国电话号码。逻辑或| Unix
这里是我的基本壳
#!/bin/bash
#search of phone numbers
t=(\+ | 00)33[1-9][0-9]{8}
t2=(\+ | 00)33[1-9][0-9]-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}
t3=(\+ | 00)33[1-9][0-9].[0-9]{2}.[0-9]{2}.[0-9]{2}.[0-9]{2}
grep -e $1 ($t | $t2 | $t3)
这里是我的输入文件:
phone_number.txt
+33143730862
00335.45.45.45.45
+332-45-45-45-45
+334545454554454545
我不断收到此错误:
./script_exo2.sh: line 5: syntax error near unexpected token `|'
./script_exo2.sh: line 5: `t=(\+ | 00)33[1-9][0-9]{8}'
./script_exo2.sh: line 6: syntax error near unexpected token `|'
./script_exo2.sh: line 6: `t2=(\+ | 00)33[1-9][0-9]-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}'
./script_exo2.sh: line 7: syntax error near unexpected token `|'
./script_exo2.sh: line 7: `t3=(\+ | 00)33[1-9][0-9].[0-9]{2}.[0-9]{2}.[0-9]{2}.[0-9]{2}'
./script_exo2.sh: line 9: syntax error near unexpected token `('
./script_exo2.sh: line 9: `grep -e $1 ($t | $t2 | $t3)'
你可能想要在字符串和变量之间加一些引号。 http://www.shellcheck.net是你的朋友。 – Biffen
OS X上的'bash'与任何你能找到的Linux上的'bash'都一样。您提供的脚本在任何脚本中都是无效的,因为它使用shell元字符和控制操作符在需要将它们作为数据(特别是'(',')','''和空格字符)。 –
'00'不是一个通用的国际通话前缀,但在许多国家都是正确的。这就是为什么我们需要普遍的'+'。 – tripleee