{"id":265,"date":"2010-08-23T11:07:00","date_gmt":"2010-08-23T03:07:00","guid":{"rendered":"http:\/\/www.genepeng.com\/?p=265"},"modified":"2010-08-23T11:07:00","modified_gmt":"2010-08-23T03:07:00","slug":"shell%e7%94%b5%e8%af%9d%e6%9c%ac-2","status":"publish","type":"post","link":"https:\/\/www.genepeng.com\/index.php\/265","title":{"rendered":"shell\u7535\u8bdd\u672c"},"content":{"rendered":"<p>\u524d\u51e0\u5929\u5728MSDN\u4e0a\u770b\u5230\u6709\u4eba\u7528shell\u5199\u4e86\u4e2a\u7535\u8bdd\u672c\u7a0b\u5e8f\uff0c\u4e3a\u4e86\u5de9\u56fa\u4e00\u4e0b\u4ee5\u524d\u5b66\u8fc7\u7684\uff0c \u81ea\u5df1\u4e5f\u6765\u5f04\u4e00\u4e2ashell\u7535\u8bdd\u672c[\u7edd\u975e\u6284\u88ad]\u3002\u5176\u5b9e\u4e0d\u80fd\u7b97\u4e00\u4e2a\u7535\u8bdd\u672c\uff0c\u56e0\u4e3a\u53ea\u6709\u4e24\u4e2a\u57df\uff1a\u540d\u5b57\u548c\u5730\u5740\uff0c\u6743\u5f53\u7ec3\u4e60\uff0c\u4e0d\u5c3d\u5b8c\u7f8e\u4e4b\u5904\uff0c\u8bf7\u6d77\u6db5:D<\/p>\n<p>#!\/bin\/sh<br \/>\n#Date: 2010-08-22<br \/>\n#QQ: 32281309<br \/>\n#MSN: pgj1207@hotmail.com<br \/>\n#Site: www.genepeng.com<\/p>\n<p>function AddUser()<br \/>\n{<br \/>\n  while :<br \/>\n  do<br \/>\n    echo -n &#8220;Enter user name: &#8221;<br \/>\n    read UserName<br \/>\n    if [ &#8220;$UserName&#8221; = &#8220;&#8221; ]; then<br \/>\n      continue<br \/>\n    else<br \/>\n      break<br \/>\n    fi<br \/>\n  done<\/p>\n<p>  while :<br \/>\n    do<br \/>\n      echo -n &#8220;Enter Address: &#8221;<br \/>\n      read Address<br \/>\n      if [ &#8220;$Address&#8221; = &#8220;&#8221; ]; then<br \/>\n        continue<br \/>\n      else<br \/>\n        break<br \/>\n      fi<br \/>\n    done<\/p>\n<p>  echo -e &#8220;$UserName\\t$Address&#8221; >> $FileName<br \/>\n}<br \/>\n<!--more--><br \/>\nfunction QueryUser()<br \/>\n{<br \/>\n  echo &#8220;Enter user name to query&#8221;<\/p>\n<p>  while :<br \/>\n  do<br \/>\n    echo -n &#8220;Enter user name: &#8221;<br \/>\n    read UserName<br \/>\n    if [ &#8220;$UserName&#8221; = &#8220;&#8221; ]; then<br \/>\n      continue<br \/>\n    else<br \/>\n      break<br \/>\n    fi<br \/>\n  done<\/p>\n<p>  User=`grep &#8220;$UserName\\>&#8221; < $FileName`\n  if [ \"$User\" = \"\" ]; then\n    echo \"$UserName not exist\"\n  else\n    echo \"$User\"\n  fi\n  \n}\n\nfunction DeleteUser()\n{\n  echo \"Enter user name to delete\"\n\n  while :\n  do\n    echo -n \"Enter user name: \"\n    read UserName\n    if [ \"$UserName\" = \"\" ]; then\n      continue\n    else\n      break\n    fi\n  done\n  \n  echo \"Do you really want to delete $UserName? Y\/N\"\n  read YN\n  if [ \"$YN\" = \"y\" -o \"$YN\" = \"Y\" ]; then\n    sed \"\/$UserName\/d\" $FileName > &#8220;$FileName.tmp&#8221;<br \/>\n    mv &#8220;$FileName.tmp&#8221; &#8220;$FileName&#8221;<br \/>\n  fi<br \/>\n}<\/p>\n<p>function EditUser()<br \/>\n{<br \/>\n  echo &#8220;Enter user name to edit&#8221;<\/p>\n<p>  while :<br \/>\n  do<br \/>\n    echo -n &#8220;user name: &#8221;<br \/>\n    read UserName<br \/>\n    if [ &#8220;$UserName&#8221; = &#8220;&#8221; ]; then<br \/>\n      continue<br \/>\n    else<br \/>\n      break<br \/>\n    fi<br \/>\n  done<\/p>\n<p>  if IsUserExist $UserName; then<br \/>\n    echo &#8220;$UserName not exist&#8221;<br \/>\n  else<br \/>\n    sed &#8220;\/$UserName\/d&#8221; $FileName > &#8220;$FileName.tmp&#8221;<br \/>\n    mv &#8220;$FileName.tmp&#8221; &#8220;$FileName&#8221;<\/p>\n<p>    while :<br \/>\n    do<br \/>\n      echo -n &#8220;Enter Address: &#8221;<br \/>\n      read Address<br \/>\n      if [ &#8220;$Address&#8221; = &#8220;&#8221; ]; then<br \/>\n        continue<br \/>\n      else<br \/>\n        echo -e &#8220;$UserName\\t$Address&#8221; >> $FileName<br \/>\n        break<br \/>\n      fi<br \/>\n    done<br \/>\n  fi<br \/>\n}<\/p>\n<p>function ListAllUser()<br \/>\n{<br \/>\n  cat $FileName<br \/>\n}<\/p>\n<p>function IsUserExist()<br \/>\n{<br \/>\n  User=`grep &#8220;$1\\>&#8221; < $FileName`\n  if [ \"$User\" = \"\" ]; then\n    return 0\n  else\n    return 1\n  fi\n}\n\nfunction ShowMenu()\n{\n  echo \"****** Employee Directory ****** \"\n  echo \"      A : AddUser         \"\n  echo \"      Q : QueryUser       \"\n  echo \"      D : DeleteUser      \"\n  echo \"      E : EditUser        \"\n  echo \"      L : ListAllUser     \"\n  echo \"      X : Exit            \"\n  echo \"******************************** \"\n}\n\n#Site: www.genepeng.com\nFileName=\"user_directory.txt\"\nif [ ! -f $FileName ]; then\n > $FileName<br \/>\nfi<\/p>\n<p>while :<br \/>\ndo<br \/>\n  ShowMenu<br \/>\n  read c<br \/>\n  case $c in<br \/>\n    A|a) AddUser ;;<br \/>\n    Q|q) QueryUser ;;<br \/>\n    D|d) DeleteUser ;;<br \/>\n    E|e) EditUser ;;<br \/>\n    L|l) ListAllUser ;;<br \/>\n    X|x) break ;;<br \/>\n    *) echo &#8220;Error: Wrong choice&#8221;<br \/>\n        continue ;;<br \/>\n  esac<br \/>\ndone<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u51e0\u5929\u5728MSDN\u4e0a\u770b\u5230\u6709\u4eba\u7528shell\u5199\u4e86\u4e2a\u7535\u8bdd\u672c\u7a0b\u5e8f\uff0c\u4e3a\u4e86\u5de9\u56fa\u4e00\u4e0b\u4ee5\u524d\u5b66\u8fc7\u7684\uff0c \u81ea\u5df1\u4e5f\u6765\u5f04\u4e00\u4e2ashell\u7535\u8bdd\u672c[\u7edd\u975e\u6284\u88ad]\u3002\u5176\u5b9e\u4e0d\u80fd\u7b97\u4e00\u4e2a\u7535\u8bdd\u672c\uff0c\u56e0\u4e3a\u53ea\u6709\u4e24\u4e2a\u57df\uff1a\u540d\u5b57\u548c\u5730\u5740\uff0c\u6743\u5f53\u7ec3\u4e60\uff0c\u4e0d\u5c3d\u5b8c\u7f8e\u4e4b\u5904\uff0c\u8bf7\u6d77\u6db5:D #!\/bin\/sh #Date: 2010-08-22 #QQ: 32281309 #MSN: pgj1207@hotmail.com #Site: www.genepeng.com function AddUser() { while : do echo -n &#8220;Enter user name: &#8221; read UserName if [ &#8220;$UserName&#8221; = &#8220;&#8221; ]; then continue else break fi done while : do echo -n &#8220;Enter Address: &#8221; read Address if [ &#8220;$Address&#8221; = &#8220;&#8221; ]; then continue &#8230; <a title=\"shell\u7535\u8bdd\u672c\" class=\"read-more\" href=\"https:\/\/www.genepeng.com\/index.php\/265\" aria-label=\"More on shell\u7535\u8bdd\u672c\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/265"}],"collection":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/comments?post=265"}],"version-history":[{"count":1,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/265\/revisions"}],"predecessor-version":[{"id":266,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/265\/revisions\/266"}],"wp:attachment":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/media?parent=265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/categories?post=265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/tags?post=265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}