Script programming on the graphic desktop in Linux command line and shell script programming

Source: Internet
Author: User
Linux command line and shell script programming Daquan graphic desktop script programming create text menu create menu layout [plain] #! /Bin/bashclear # clear screen echo-e & quot; \ t \ tMenu & quot; echo-e & quot; 1. \ tDisplaydiskspace & quot; echo-e & quot; Linux command line and shell script programming Daquan create text menu creation menu layout [plain] #! /Bin/bash clear # clear screen echo-e "\ t \ tMenu" echo-e "1. \ tDisplay disk space "echo-e" 2. \ tDisplay logged on user "echo-e" 3. \ tDisplay memory usage "echo-e" 0. \ tExit menu \ n "echo-en" \ t \ tEnter option: "#-n indicates not to print the carriage return read-n1 option # read one character echo" "display effect: [plain] Menu 1. display disk space 2. display logged on user 3. display memory usage 0. exit menu Enter option: to create a menu function, you only need to wrap the above code with a function to add the menu logic [plain] #! /Bin/bash option = "" create_menu () {clear echo-e "\ t \ tMenu" echo-e "1. \ tDisplay disk space "echo-e" 2. \ tDisplay logged on user "echo-e" 3. \ tDisplay memory usage "echo-e" 0. \ tExit menu \ n "echo-en" \ t \ tEnter option: "read-n1 option echo" "} create_menu case $ option in 0) echo "Display disk space"; 1) echo "Display logged on user"; 2) echo "Display memory usage"; 3) echo "Exit menu ";;*) e Cho "error"; esac: add the corresponding case. then, implement the corresponding function. To integrate the shell script menu, first add the corresponding case implementation function [plain] disk_space () {clear df-k} whoseon () {clear who} menu_usage () after {clear cat/proc/meminfo}, process the case section [plain] deal_menu () {case $ option in 1) disk_space; 2) whoseon; 3) menu_usage; 0) echo "Exit menu"; *) # Input Error. re-enter echo-e "\ nSorry, wrong selection. "echo-en" \ n \ t \ tHit any key to continue. "read-n1 option # Re-read menu option deal_menu; esac} deal_menu use select command sel The ect command allows you to create a menu from a single command line, then extract the input answer and automatically process it. [Plain] The select variable in list do commands done list parameter is a list of text options that constitute space-separated menus. The select command displays each option in the list as a prepared number option, and then displays a special prompt defined by the PS3 environment variable for the option. (The default prompt is #?) [Plain] PS3 = "Enter option: "select option in" Display disk space "" Display logged on user "" Display memory usage "" Exit menu "do case $ option in" Display disk space ") disk_space ;; "Display logged on user") whoseon; "Display memory usage") menu_usage; "Exit menu") echo "Exit menu" break ;;*) echo-e "\ nSorry, wrong selection. "echo-en" \ n \ t \ tHit any key to continue. "read-n1 option deal_menu ;; Esac done running result: [plain] $ menu_test 1) Display disk space 3) Display memory usage 2) Display logged on user 4) Exit menu Enter option: select automatically generates menu notes: the result stored in the variable is the entire string, not the number before the menu option. use the window to install the dialog: sudo apt-get install dialogdialog package dialog part description calendar provides calendar checklist for date selection to display multiple options (each option can be turned on or off) from build a form (filled with tags and text fields) fselect provides a file selection window to browse the selection file gauge display completion percentage progress bar infobox display a message, however, you do not have to wait for a response to inputbox to provide a text form for the input text. Inputmenu provides an editable menu to display optional column options. msgbox displays a message, the user needs to select the OK button pause to display a progress bar to display the status of the specific tentative time. passwordbox shows a text box to enter the text, however, the input text passwordform is hidden and a form with labels and hidden text input is displayed. radiolist provides a set of menu options, however, you can only select one tailbox and use the tail command to display the file content in the rolling window. tailboxbg is the same as tailbox, however, in background mode, run textbox to display the file content in the rolling window. timebox provides a window for selecting hours, minutes, and seconds. yesno provides a simple message dialog with the "Yes" and "No" buttons -- widget parameterswidget is the component above, parameters is the component window size and required text. each dialog component provides two output formats: use STDERR The button selected by the user with the exit status code determines the exit status code of the dialog command. If Yes or OK is selected, the dialog command returns the exit status code 0. If Cancel and NO are selected, the dialog command returns the exit status code 1, which can be standard $? Variable to view the exit status code. if the widget returns any data, the dialog command then sends the data to the STDERRmsgbox part dialog -- msgbox text height width. you can specify titledialog -- title Testing -- msgbox "This is a test" 10 20yesno part dialog -- title "Do not lie me "-- yesno" Are you a girl? "10 20inputbox part dialog -- title" Do not lie to me "-- inputbox" Enter you age: "10 20 2> age.txt textbox component dialog -- textbox/etc/passwd 15 45 can display a large number of text, you can also use the arrow keys, Home, End, PgUp, PgDn, and other operations on the menu component dialog -- menu "Menu" 20 30 5 1 ". "2" B. "3" C. "The red parameter indicates that the height of the menu bar is 5 items, the first two parameters are the high and wide fselect component dialog -- title "select a file" -- fselect/etc/10 50 2> file.txt dialog option description -- add-widget to continue the next dialog box, until you press Esc or Cancel -- aspec T ratio specifies the aspect ratio of the window width and height -- backtitle title specifies the title displayed on the background at the top of the screen (2) -- begin x y specifies the start position in the upper left corner of the window -- cancel-label specifies the replacement label of the Cancel button -- clear uses the default dialog background color to clear the display -- colors embeds ANSI color encoding in the dialog box text. -- cr-wrap allows line breaks and force line breaks in the dialog text -- create-rc file copies the content of the sample configuration file to the specified file (1) -- defaultno: set the default answer of the yes/no dialog to no -- default-item string to set the check list, the default item -- exit-label in the form or menu dialog specifies the replacement label of the Exit button -- extra-button displays an additional button in the OK button and the Cancel button -- extra-label refers to the quota Button replacement label -- help displays the help information of the dialog command -- Help-button displays a help button after the OK button and the Cancel button -- Help-label specifies the replacement label of the help button -- help-status when the Help button is selected, the multi-choice list, single-choice list, or form information will be written after the help information -- ignore ignores the unrecognized option of dialog -- input-fd specifies another file descriptor, instead of STDIN -- insecure, the asterisk -- item-help is displayed as the multiple-choice list when you type the password component, add a help bar at the bottom of the screen for each label in the Radio list or menu -- keep-window do not clear the display part -- max-input size specifies the maximum length of characters entered on the screen. The default value is 2048 -- nocancel to hide the Cancel button -- no-collapse. do not convert the tabs into spaces in the dialog text -- no-kill to put the tailboxbg dialog to the background, disable the SIGHUP signal of the process -- no-label specifies the replacement label for the No button -- no-shadow does not display the projection effect of the dialog window -- OK-label specifies the replacement label of the OK button -- output-fd specifies another output file descriptor, instead of STDERR -- print-maxsize, print the maximum size of the dialog window to the output -- print-size print the size of each dialog window to the output -- print-version print the dialog version in the output, -- separate-output outputs the results of the checklist component one by one without quotation marks. -- separator string specifies the output string for each component. -- separate- Widget string specifies the split output string for each part -- shadow draws a shadow in the lower right corner of each window -- single-quoted uses single quotation marks for output of multiple-choice lists when required -- sleep sec finishes processing the dialog window the specified number of seconds after the delay -- stderr sends the output to STDERR (this is the default) -- stdout sends the output to STDOUT -- tab-correct to convert the tab to space -- tab-len n to specify the number of spaces occupied by a tab (8 by default) -- timeout sec: if no user input is specified, the sec exits in seconds and returns the error code -- title specifies the title of the dialog window -- trim deletes the leading space and line break from the dialog text -- visit-items modifies the position of the tabs in the dialog window, make it include the option list -- yes-label is Yes button to execute the replace label (1) dialog command supports runtime configuration. When dialog is started, check whether the DIALOGRC environment variable is set. this variable saves the configuration file path. If not, $ HOME/. dialogrc is used. If this does not exist, search for the GLOBALRC (/etc/dialogrc) specified during compilation. if this does not exist, use the default value for compilation. (2) -- The backtitle option is an easy way to create a public title for the menu in the script. If this option is specified for each dialog window, it will be consistent in the application. To use the dialog command in the script, pay attention to the following two points: 1. if the Cancel or No button exists, check the exit status code of the dialog Command 2. redirect STDERR to get the output value. The following code uses diain to implement [plain] #! /Bin/bash temp = 'mktemp-t temp. XXXXXX 'temp2 = 'mktemp-t temp2.xxxx' disk_space () {df-k> $ temp dialog -- textbox $ temp 20 60} whoseon () {who> $ temp dialog -- textbox $ temp 20 50} menu_usage () {cat/proc/meminfo> $ temp dialog -- textbox $ temp 20 50} dialog -- menu "menu" 20 30 10 1 "Display disk space" 2 "Display logged on user" 3 "Display memory usage" 0 "Exit menu" 2> $ temp2 if [$? -Ne 1] then selection = 'cat $ temp2' case $ selection in 1) disk_space; 2) whoseon; 3) menu_usage; 0 );;*) dialog -- msgbox "Sorry, invalid selection "10 30 esac fi rm-f $ temp $ temp2>/dev/null install the kdialogsudo apt-get install kdebase-binkdialog component similar to the dialog component in the graphic KDE environment, command format: kdialog display-options window-options argumentswindow-options allows you to specify the type of window to be used. Description of options in the kdialog window -- checklist title [tag item status] multi-choice list menu. the status indicates whether the option is selected. -- error text error message box -- inputbox text [init] indicates the input text box. You can use init to specify the default value -- menu title [tag item] menu selection box with a title, and the option list identified by tags -- msgbox text to display the simple message box of the specified file -- password text to hide the password input text box -- radiolist title [tag item status] single-choice list menu, status indicates whether this option is selected. -- separate-output is the multiple-choice list and the single-choice list menu returns the line-separated option -- sorry text "sorry" message box -- textbox file [width] [height] text box that displays the file content, in addition, width and height -- title are specified as the TitleBar area of the dialog window, and a warning message box with the title -- warningyesno text with the Yes and No buttons -- warningcontinuecancel text with the Cont is specified. Warning message boxes for inue and Cancel buttons-warning message boxes for warningyesnocancel text with the Yes, No, and Cancel buttons-question boxes for yesno text with the Yes and No buttons-yesnocancel text with Yes, No the checklist and radiolist boxes of the and Cancel buttons allow you to define separate options in the list and whether they are selected by default. The biggest difference between kdialogkdialog and dialog is that kdialog redirects the output to STDOUT. Zenity component zenity window part option description in GNOME -- calendar display calendar for the whole month -- entry display text input dialog window -- error DISPLAY error message dialog window -- file-selection display complete path name and file name dialog window -- info display information dialog window -- list Display multi-choice list or single-choice list dialog window -- notification display notification icon -- process Display progress bar dialog window -- question display yes/no dialog window -- scale display adjustable size window -- text-info display text box -- warning display warning dialog window zenity provides a better window than the previous dialog effect zenity -- after the date is selected, zenity returns the value to STDOUT. like kdialog, zenity [plain] # is used in the script. /Bin/bash temp = 'mktemp-t temp. XXXXXX 'temp2 = 'mktemp-t temp2.xxxx' disk_space () {df-k> $ temp zenity -- text-info -- title "Disk space" -- filename = $ temp -- width 750 -- height 300} whoseon () {who> $ temp zenity -- text-info -- title "Logged on user" -- filename = $ temp -- width 500 -- height 200} menu_usage () {cat/proc/meminfo> $ temp zenity -- text-info -- title "Memory usage" -- filename = $ temp -- width 3 00 -- height 500} zenity -- list -- radiolist -- title "Menu" -- column "Select" \ -- column "Menu Item" FALSE "Display disk space" FALSE "Display logged on user" FALSE "Display memory usage" FALSE "Exit"> $ temp2 if [$? -Ne 1] then selection = 'cat $ temp2' case $ selection in "Display disk space") disk_space; "Display logged on user") whoseon ;; "Display memory usage") menu_usage; "Exit"); *) zenity -- info "Sorry. invalid selection. "esac fi rm-f $ temp $ temp2 2>/dev/null zenity does not support the menu dialog box

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.