Skip to content

Instantly share code, notes, and snippets.

@liyang85
Created November 25, 2017 12:49
Show Gist options
  • Save liyang85/039edc5c9830197ad4d35f22af2535ba to your computer and use it in GitHub Desktop.
Save liyang85/039edc5c9830197ad4d35f22af2535ba to your computer and use it in GitHub Desktop.
另一种验证IP的方法。要点: ①直接把变量值包含的空格替换为下划线 ②匹配IP的正则表达式要更具体 ③在Bash中测试regex **不要** 使用任何引号! https://stackoverflow.com/a/13015572/3025050
#!/bin/bash
read name
while [[ ! "$name" =~ '[A-Za-z ]' ]]; do
read -p "Wrong name format. Re-enter: " name
done
name="${name// /_}"
read ip
while [[ ! "$ip" =~ '^((25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9]{1,2})[.]){3}(25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9]{1,2})$' ]]; do
read -p "Not an IP. Re-enter: " ip
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment