-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