回答
#!/bin/bash
string="$1"
if [[ $(type "$string" 2>&1) == "$string is a shell"* ]]; then
echo "Keyword $string is reserved by shell"
fi
我只是爲了個人的完全右手比賽。它不可能傷害並使事情更清楚。可能甚至與'$ string'完全匹配。 –
@Etan Reisner:問題是:'type echo' =>'echo是一個shell內建函數',而'if if' =>'if是一個shell關鍵字'。 –
@EtanReisner如果顯示在該目錄中,將顯示什麼內容? – 123
如果你只想要Shell關鍵字,則:
#!/bin/bash
string="$1"
[[ $(type -t "$string" 2>&1) == "keyword" ]] && echo reserved || echo not reserved
內建不會通過這項測試(僅適用於關鍵字)。
與在各種情況下延伸的可能性這樣做的一種方式:
#!/bin/bash
string="$1"
checkfor=('keyword' 'builtin')
for ((i=0;i<${#checkfor[@]};i++))
do
[[ $(type -t "$string" 2>&1) == "${checkfor[$i]}" ]] && reserved=true && break || reserved=false
done
[[ $reserved == true ]] && echo reserved || echo not reserved
command
,hash
,alias
,type
等。(內建)將通過上述試驗以及關鍵字。
您可以通過添加元素到數組checkfor
添加其他可能的測試條件:
checkfor=('keyword' 'builtin' 'file' etc...)
- 1. 如何在POSIX shell中測試命令輸出是否爲非空?
- 2. Shell腳本測試命令
- 3. 如何運行adb shell命令並保留在shell中?
- 4. 測試是否支持命令選項
- 5. shell腳本測試命令混淆
- 6. 命令行保留令牌
- 7. 如何測試命令anacron?
- 8. 在ifelse命令中測試R命令是否存在錯誤
- 9. 如何測試shell腳本中的行是否爲空?
- 10. 如何從命令行測試服務是否正在運行
- 11. 如何測試是否輸入命令行參數?
- 12. 如何在appium測試用例中運行android shell命令?
- 13. 批量測試一個命令的結果是否爲空
- 14. 如果shell命令爲空
- 15. 嘗試使用RichTextBox命令使命令變爲「Shell」
- 16. 如何確定我是否需要在MQ中保留命令服務器?
- 17. 爲答案保留失敗測試
- 18. 如何使用hdfs shell命令檢查文件是否存在
- 19. 如何找出shell命令是否存在於cshell
- 20. 如何檢查命令是否存在於shell腳本中?
- 21. 如何檢查命令在Windows shell中是否可用?
- 22. 如何檢查是否一個shell命令的結束在Python
- 23. 如何檢查shell命令是否正確執行?
- 24. 使用shell命令如何提取從命令(ZDB)字段
- 25. 如何組合shell命令
- 26. 如何從shell命令
- 27. 如何測試CComBSTR是否爲空
- 28. 如何測試ManagedObject是否爲
- 29. 如何測試UITextField是否爲零?
- 30. 如何測試是否$值爲$ NUM + -3
對正式名單檢查? http://www.gnu.org/software/bash/manual/html_node/Reserved-Word-Index.html –