高度な技術をシェル - 入力情報の正当性を検証します

ここに示した例では、ユーザ情報が入力された数字や文字であることを確認することです。その理由は、それがトリッキーな部分を実装して、主に方法のため、連続して収集することに留意すべきです。

      [root@xieqichao ~]# cat > test15.sh
      #!/bin/sh
      echo -n "Enter your input: "
      read input
      #1. 事实上,这里的巧妙之处就是先用sed替换了非法部分,之后再将替换后的结果与原字符串比较。这种写法也比较容易扩展。    
      parsed_input=`echo $input | sed 's/[^[:alnum:]]//g'`
      if [ "$parsed_input" != "$input" ]; then
          echo "Your input must consist of only letters and numbers."
      else
          echo "Input is OK."
      fi
      CTRL+D
      [root@xieqichao ~]# ./test15.sh
      Enter your input: hello123
      Input is OK.
      [root@xieqichao ~]# ./test15.sh
      Enter your input: hello world
      Your input must consist of only letters and numbers.
      ```
公開された352元の記事 ウォン称賛52 ビュー30000 +

おすすめ

転載: blog.csdn.net/xie_qi_chao/article/details/105037470