Linux programmer benefits-chasing girlfriend Magic (Linux terminal running cool program)

Source: Internet
Author: User
Tags clear screen

Overview as IT staff, to colleagues feel stiff, will not be romantic, do not understand the fun. In fact, we can use our skills to create the unique romance of IT staff. Girllove scripts can be romantic for it people. Girllove is essentially a simple question-and-answer system that achieves a "romantic" effect by setting different questions and answers. By rewriting the script, the reader can easily implement a Linux-based research system or an examination system. Detailed code Download: http://www.demodashi.com/demo/12738.html

First, Function introduction

As IT staff, to colleagues feel stiff, will not be romantic, do not understand the fun. In fact, we can use our skills to create the unique romance of IT staff. Girllove scripts can be romantic for it people. Girllove is essentially a simple question-and-answer system that achieves a "romantic" effect by setting different questions and answers. By rewriting the script, the reader can easily implement a Linux-based research system or an examination system.

Second, the realization method

The Girlllove script can be shown in the following sections: Text effects (poetry), problems (question), problem options (Bakans), Answers (answer), and hints (tips). These are stored in the shell array (girlLove.txt file) and are one by one corresponding relationships that are displayed in the main program girllove.sh through a while loop. The specific contents of the above sections can be set in the GirlLove.txt file, set the number of options and the user's screen, if the reader display is too small, the number of options may have a position offset and other effects.

1. The word on the terminal is the output at the specified location, the terminal's X/Y axis information is recorded by the following code

# Pos_stdy: The output position is 2/3 x terminal y-axis length; POS_STDX: The output position is the x-axis length pos_stdy= "$ (($ (stty size|cut-d"-F1)/3 * 2) "pos_stdx=" $ (($ ( Stty size|cut-d "-f2)/2)" # TOTAL_STDX: Terminal x-axis length, Total_stdy: terminal y-axis length total_stdy= "$ (($ (stty size|cut-d"-f1)) "Total_std X= "$ (($ (stty size|cut-d '-f2))"

2. The PRINT_XY function is used to control the character output position according to the algorithm you set.

# This function is used to control the print position of the string # parameter 1: The string to be printed; parameter 2: According to the parameters and choose a different position calculation formula, different types of string, position calculation formula is different; # parameter 3: Used to control where the character is printed on the y-axis; parameter 4: Used to control where the character is printed function Print_xy () {    if [$#-eq 0]; then        return 1    fi     len= ' expr ${#1}/2 '    if [$#-lt 2]; then
   
    pos= "\e[${pos_stdy};$ ((${pos_stdx}-${len}) H"     elif [$ = "-"]; then        pos= "\e[$ ((${pos_stdy}-$ $)); $ (${pos_ STDX}-${len}) H "     elif [$ =" + "]; then        pos=" \e[$ ((${pos_stdy} + $ $)); $ ((${pos_stdx}-${len}) H "     Elif [$ 2 = "Lu"]; Then        pos= "\e[$ ((${pos_stdy}-$ $)), $ ((${POS_STDX}-$4)) H"     elif [$ = "LD"]; then        pos= "\e[$ ((${pos_stdy } + $ (${POS_STDX}-$4)) H "    fi     Echo-ne" ${pos}$1 "}
   

3. Cyclic output problems

While [${seq}-lt ${#poetry [@]}]do    ... done

4. For more details, please refer to the more detailed comments in the program.

# Loop 18 Questions for effect, number of questions more than $poetry variable line number 1while [${seq}-lt ${#poetry [@]}]do    sleep 0    isanswer=0     # printing problem    Print_  XY "Q: ${question[$seq]}" LD 2 $offset     # printing problem option    Print_xy "${bakans[$seq]}" + 3     # print answer bar    print_xy "Answer:" LD 4 $offset     # Read the terminal input into the variable ans    read ans ...    .

5. The complete procedure is as follows

#!/bin/sh: << EOF usage:./girllove.sh girlfriend name eof# set the name of the girlfriend, used in the terminal to show Name= "$" # Read the variables set in the GirlLove.txt file. ./girllove.txt# script Closing declaration= "$name Let me guard you forever! "# Pos_stdy: The output position is 2/3 x terminal y-axis length pos_stdx: The output position is the x-axis length pos_stdy=" $ (($ (stty size|cut-d "-F1)/3 * 2)" pos_stdx= "$ (($ (Stty size|cut-d "-f2)/2)" # TOTAL_STDX: Terminal x-axis length, Total_stdy: terminal y-axis length total_stdy= "$ (($ (stty size|cut-d"-f1)) "Total_st Dx= "$ (($ (stty size|cut-d '-f2))" # When you start the quiz, give the message (the tip at the bottom of the screen) info= "$name This is the gift for you. Select 1-4 and press ENTER to start answering the question" # The cue message that appears before the progress bar head= "$name Current Progress:" # The information set in the terminal output declaration variable, animate function Waiting () {I=1 # through while loop and for loop) The loop realizes the animation effect of//////circle while [$i-GT 0] does for J in '-"\ \" | '/' Do # Print the contents of the previous 6/symbol + declaration variable Echo-ne "\033[1m\033[$ (($ (stty size|cut-d"-f1)/ 3 * 2)); $ (($ (stty size|cut-d '-f2)/2-${#declaration}-6)) H$j$j$j$j$j$j\033[4m\033[32m${declaration} "# Print back six/symbol Echo-ne "\033[24m\033[?25l$j$j$j$j$j$J "Usleep 100000 Done ((i++)) done}# This function is used to control the print position of the string # parameter 1: The string to be printed; parameter 2: Select a different position calculation formula according to the parameter and different type of Word    string, the position calculation formula is different; # parameter 3: Used to control where the character is printed on the y-axis; parameter 4: Used to control the print position of the character on the x-axis; function Print_xy () {if [$#-eq 0]; then return 1 fi len= ' expr ${#1}/2 ' If [$#-lt 2]; Then pos= "\e[${pos_stdy};$ ((${pos_stdx}-${len}) H" elif [$ = "-"]; then pos= "\e[$ ((${pos_stdy}-$ $) ); $ ((${pos_stdx}-${len}) H "elif [$ =" + "]; Then pos= "\e[$ ((${pos_stdy} + $ $)), $ ((${pos_stdx}-${len}) H" elif [$ = "Lu"]; then pos= "\e[$ ((${pos_ STDY}); $ ((${pos_stdx}-$4)) H "elif [$ =" LD "]; Then pos= "\e[$ ((${pos_stdy} + $ $)), $ ((${POS_STDX}-$4)) H" fi echo-ne "${pos}$1"}# call clear Clear screen clear# in the x-axis, 2 /3 The location of the y-axis prints the following string (formatted interface) Print_xy "*&*&*&*&*&*&*&*&*&*&*&*&*&* &*&*&*&*&*&*&*&*&*&*&*&*&* "# Print $info information printf in the middle of the terminal bottom \r\e [${TOTAL_STDY};$ (((${total_stdx}-${#info}*2)/2) H${info} "offset=14seq=0# Loop 18 questions, for effect, the number of questions is more than the number of $poetry variable rows 1while [${seq}-lt ${# poetry[@]}]do sleep 0 isanswer=0 # printing problem print_xy "Q: ${question[$seq]}" LD 2 $offset # printing problem option Print_xy "${bakans[$seq]}" + 3 # Print answer Bar Print_xy "A:" LD 4 $offset # Read terminal input to variable ans read ans # cursor up 3 lines, and clear the contents from the cursor to the end of the line (clear ask Echo-e "\033[3a\r\033[k" # clears the question option line character echo-e "\033[k" # clears the answer bar character echo-e "\033[k" # If the value entered differs from the preset answer , the problem continues to loop if ["$ans"! = "${answer[$seq]}"; Then # Print-----, format the interface. -----The tip print_xy "---------------------------------------" + 5 # that shows the problem is displayed below the tip print_xy "${tip s[$seq]} "+ 7 # wait 1s sleep 1 # move the cursor to the beginning of the line, and clear the cursor to the end of the lines of the character echo-e" \r\033[k "# cursor up 3 lines, and clear the cursor to the end of the line The character echo-e "\033[3a\r\033[k" continue fi # Problem number + 1 seq= ' expr ${seq} + 1 ' # get poetry's penultimate seq + 1 Line curseq= ' expr ${#poetry [@]}-${seq} ' ... # set red background printf "\e[41m"# clear Screen clear# output declaration variable information waiting 

6. The relevant question and answer file is designed as follows:

poetry= ("㊣━━ ^-^ O People's Republic of China O^-^━━㊣  " "┃ Wish:            ┃  " "┃    $name small pot friends every day happy!   ┃  "" ┃              XXX ┃ "" ㊣━━ ^-^ O People's Republic of China O^-^━━㊣ "" "                         Doodle oοо0 wow by!!! Get out of the way "" ╭══╮ wife! Drive Luo \ "Sit well" "╭╯ five ║ husband!" Slow down \ ' I'm excited ', ╰⊙═⊙╯. Oо0 is dead! Question= (  "Who are you sitting next to you?") "What             town is your husband's hometown?" "             what kind of meal does the husband make?" "             Lili likes watching TV dramas or movies?" "             question 5" "             question 6"             "question 7"             "Question 8"             "Question 9"             "Question             " "Question one") bakans= (  "1 boyfriends; 2 objects; 3 Husbands 4 above are not"       "1 fragrant flowers; 2 Mao Tang; 39 weight; 4 above are not"           "1 porridge; 2 noodles; 3 Rice 4 above are"           "1 TV 2 Movie "           1 option1 2 option2 3 option3 4 Option4"           "1 option1 2 option2 3 option3 4 Option4"           "1 option1 2 op Tion2 3 Option3 4 Option4 "           " 1 option1 2 option2 3 option3 4 Option4 "  ... 

Third, installation method

1. Preparatory work

    • 1 Linux Servers

2. Installation Steps

1. Download the attachment:

Girllove.zip

2. Unzip the Girllove.zip:

# Unzip Girllove.zip

3. Enter the Girllove directory to execute the./girllove.sh command

# CD girllove#./girllove.sh

Answer: 3,3,4,1,1,1,1,1,1,1

3. How to use

1. Modify GirlLove.txt Inside: question (problem), Bakans (option), Answer (answer), tips (hint)

2. Execute girllove.sh command

./girllove.sh

3. DIY your own questions and answers

Only need to modify the GirlLove.txt file, you need to modify: question (problem), Bakans (option), Answer (answer), tips (hint). Note that the number of changes should be the same. Poetry variable number of rows needs to be equal to the number of questions +1, if not meet the need to DIY poetry variable line number.

Four, the Operation effect
V. Compress package files

The entire program has only three files, as shown in:

Vi. Other Supplements

No

Code Download: http://www.demodashi.com/demo/12738.html Note: This copyright belongs to the author, published by the demo master, refused to reprint, reprint needs the author authorization

Linux programmer benefits-chasing girlfriend Magic (Linux terminal running cool program)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.