一個代人寫作業的script,後來寫多了一個加入User script
twf_cc
|
1#
twf_cc 发表于 2008-03-14 18:39
一個代人寫作業的script,後來寫多了一個加入User script
數天前,在網上跟人聊天,該網友要寫作業,用
shell 來寫,他不會,只好代他寫了一個,那是一個 Login script,要求如下 1) 只能登入五次,錯了多於五次便exit program 2) 以一個文本文件比對口令和名字,格式是 `名字 口令' joe 123 mary 456 peter abc bonjovi bonjovi 3) 口令或名字錯了要提示,還有多少次嘗試 4) 口令和名字對了便提示使用者已登入了 後來又寫了加 User,第一次發貼,拿些醜醜的script 交流一下,歡迎指正錯誤或優化 謝謝 :) 這是登入的 #! /bin/bash # a login script in pure bash, [email]twf_cc@yahoo.com.hk[/email] max=5 # init max try file="$HOME/sample_pass.txt" # the given file # function to check login and pass, compare # with the given file. Is_correct() { ret=flase # init a flag while read name pass # every name and pass in file do [ -z "$name" -a -z "$pass" ] && continue if [ "$x" = "$name" ] # compare with user input. then # if given name and passwd if [ "$y" = "$pass" ] # both are correct, then # flag will be set to `true' ret=true ;break fi fi done } # function, display message Login() { zz=$(printf '%*s\n' 45) # print 45 space line=${zz//' '/#} # convert space to `#' printf "\n\n$line\n\n" printf "%s\n" " Hello $x, welcome to Playground." printf "\n$line\n\n" } # main loop while [ $max -gt 0 ] # set timer in decrease method do printf "Your name? " # prompt for getting user input read x printf "Your password? " read -s y # -s will not be echoed user input Is_correct < $file # function call if [ "$ret" != "true" ] # test return value of `flag', then # decrease 1 if it is `false'. ((max--)) case "$max" in # reminding user how many tries are remained [1-4]) printf "\n\nLogin incorrect, $max times left.\n\n" ;; 0) printf "\n\nPermission denied.\n\n" exit 65 # exit program if user tries 5 times ;; esac else break # jump out loop if the `flag' is `true' fi done # function call while user logname and passwd are corrected Login # SCRIPT END HERE 這是加 User #! /bin/bash # add user script, [email]twf_cc@yahoo.com.hk[/email] # use at your own risk! dist="$HOME/sample_pass.txt" # user login pass to be stored in here # function, get user name GetUserName() { printf "Please enter name: " read name } # function, get user password GetUserPass() { oSTTY=$(stty -g) # save default setting x=flase # init a `flag' and null pass pass="" until [ "$x" = "true" ] do stty raw # set input to be raw input y=$(dd bs=1 count=1 2> /dev/null) # get one char stty -raw # everytime # check for `CR' if [ -z $(echo $y | tr -d "\015") ] then x=true # set flag be `true' if CR is pressed else stty echo # echo "*" each char printf "*" pass="$pass$y" # save password stty -echo # disable echo, password not shown fi # on screen done stty "$oSTTY" # return to default stty setting } ret="false" # init a flag while [ "$ret" != "true" ] do GetUserName # function call if [ -z "$name" ] # Is $name empty? then echo "Null string is not allowed. " echo "" continue # back to the looping top fi if [[ "$name" =~ [[:space:]]{1,} ]] # $name contains space, tab then echo "Name contains space is not allowed." echo "" continue fi if [ -f $dist ] # if we have sample_pass.txt, check user name then zz=$(grep -w "^$name" $dist |cut -d" " -f1 |tr -d " ") fi if [ "$name" = "$zz" ] # user name is used, sorry ;) then echo "$name is used by other user,try other name." echo "" continue fi ret="true" # everything ok, flag is set to `true' done unset ret # unset value of $ret ret=flase # init a flag again, ;) while [ "$ret" != "true" ] do printf "Enter password: " # prompt GetUserPass p1=$pass # save first password in p1 echo "" if [ -z "$p1" ] # Is password empty string? then echo "Empty password is not allowed, " echo "please retype password." echo "" continue fi printf "Confirm password: " # prompt for getting second pass GetUserPass p2=$pass # save password to another var echo "" if [ "$p1" != "$p2" ] # Is password same? then echo "Cannot confirm password, found different," echo "please retype password." echo "" continue else echo "$name $pass" >> $dist # everything ok, fi # write to file ret=true # flag is set to `true', exit looping done echo "" echo "Hello $name, you are registered." exit 0 #END OF SCRIPT [[i] 本帖最后由 twf_cc 于 2008-3-14 18:49 编辑 [/i]]
journalist
|