Understanding and learning shell
The linux terminal executes commands through the bash environment. Bash includes setting and using variables, constructing the bash operating environment, and redirecting data streams. The following knowledge helps you maintain and manage hosts. The operating system kernel is used to manage the entire computer hardware. This kernel is protected. Generally, users can only communicate with the kernel through shell (shell communicates the input command with the kernel, the kernel can control the hardware to work correctly ). Commands such as man, chmod, chown, vi, and mkfs are independent applications. We can operate these applications through shell (that is, the command line mode, let these applications call the kernel to run the required work. Bash shell built-in command: type command to find executable files, rather than general file names. Floralin @ ubuntu :~ /Desktop/linux_study $ type ls # list the main use cases of ls when no parameters are added. ls is aliased to 'ls -- color = auto' floralin @ ubuntu :~ /Desktop/linux_study $ type-t ls # Add the parameter-t to list the basis for ls execution. aliasfloralin @ ubuntu :~ /Desktop/linux_study $ type-a ls # Add the parameter-a to list all the commands containing ls from the path variable definition, including aliasls is aliased to 'ls -- color = auto' ls is/bin/lsfloralin @ ubuntu :~ /Desktop/linux_study $ type cdcd is a shell builtin # cd is the execution of shell built-in command. Many times the input command is too long and will be added before line feed \, the purpose of this operation is to transfer the line break [enter. After successful transfer, the> symbol will appear at the beginning of the next line. You can continue to enter the command. The shell variable function variable uses a specific string to indicate unfixed content. To display the variable content in linux, you can use the echo command to display the variable. However, when the variable is displayed, you must add $. floralin @ ubuntu :~ /Desktop/linux_study $ echo $ variable floralin @ ubuntu :~ /Desktop/linux_study $ echo $ PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin: /usr/games:/usr/local/games setting variable floralin @ ubuntu :~ /Desktop/linux_study $ echo $ myname floralin @ ubuntu :~ /Desktop/linux_study $ myname = florafloralin @ ubuntu :~ /Desktop/linux_study $ echo $ mynameflora keep the original variable, and then set floralin @ ubuntu :~ /Desktop/linux_study $ myname = "$ myname lin" floralin @ ubuntu :~ /Desktop/linux_study $ echo $ mynameflora lin in the execution process, the unquoted single quotation marks ('), the commands in will be executed first, and the execution results will be used as external input information. The locate command can list all related file names, but if we want to know the permission for each crontab-Related File Name: floralin @ ubuntu :~ /Desktop/linux_study $ ls-l 'locate crontab '-rw-r -- 1 root 401 Feb 19 2014/etc/anacrontab-rw-r -- 1 root 722 Feb 8 2013/etc/crontab-rwxr-sr-x 1 root crontab 34824 Feb 8 2013/usr/bin/crontab-rw-r -- 1 root 1184 Apr 7 2014/usr/share/bash-completion/completions/crontab-rw-r -- 1 root 27684 Feb 8 2013/usr/share/doc/cron/examples/crontab2english. pl-rw-r -- 1 root 2521 Feb 8 2013/usr/share/man/man1/crontab.1.gz-rw-r -- 1 root 892 Feb 19 2014/usr/share/man/man5/anacrontab.5.gz-rw -r -- 1 root 5687 Feb 8 2013/usr/share/man/man5/crontab.5.gz first column the file name data with locate, then, use the ls command to process it. Environment variables can help us achieve many functions, including main folder transformation, prompt display, execution file search path, etc. Check the default environment variable: floralin @ ubuntu :~ /Desktop/linux_study $ env #...... _ =/usr/bin/env # the content of the RANDOM number RANDOM variable of the last parameter (or command itself) of the last command used is between 0 and ~ Between 32767, so a numerical value is required to obtain a single digit. Floralin @ ubuntu :~ /Desktop/linux_study $ declare-I number = $ RANDOM * 10/32768; echo $ number1floralin @ ubuntu :~ /Desktop/linux_study $ declare-I number = $ RANDOM * 10/32768; echo $ number4floralin @ ubuntu :~ /Desktop/linux_study $ declare-I number = $ RANDOM * 10/32768; echo $ number5set view all variables (including environment variables and custom variables) bash not only environment variables, there are also some important parameters and User-Defined variables. Floralin @ ubuntu :~ /Desktop/linux_study $ setPS1 command prompt whenever we press the Enter key to execute a command, the prompt will appear again, will actively read this variable value. Floralin @ ubuntu :~ /Desktop/linux_study $ cd/homefloralin @ ubuntu: /home $ PS1 = '[\ u @ \ h \ w \ A #\#] \ $' [floralin @ ubuntu/home 08:48 #46] $ lsfloralin [floralin @ ubuntu/ home 08:51 #47] $ \ w represents the complete working directory name, the name of the Directory written by a directory. But the main folder will use ~. \ A display time, in the 24-hour format of "HH: MM ". # Run the following commands. \ $ Prompt. If it is root, the prompt is #; otherwise, the prompt is $. Variable keyboard reading, array and declaration: read, array, and declare read variables input from the keyboard [floralin @ ubuntu/home #54] $ read atestThis is a test [floralin @ ubuntu/home #55] $ read -p "Please keyin your name: "-t 30 namedPlease keyin your name: floralin [floralin @ ubuntu/home #56] $ echo $ namedfloralin if no operation is performed for 30 seconds, the command will be skipped automatically. Declare or typeset: declare the type of the variable. If declare is followed by no parameters, bash automatically counts the variable names. [Floralin @ ubuntu/home #59] $ sum = 100 + 300 + 50 [floralin @ ubuntu/home #60] $ declare-I sum = 100 + 300 + 50 [floralin @ ubuntu/home #61] $ echo $ sum450 variable type: string by default, therefore, the variable type cannot be specified. 1 + 2 is a string instead of a formula. Therefore, the First Command will output 100 + 300 + 50 .. Change sum to an environment variable: [floralin @ ubuntu/home #57] $ declare-x sum [floralin @ ubuntu/home #58] $ export | grep sumdeclare-x sum converts sum to a read-only attribute, unchangeable: [floralin @ ubuntu/home #62] $ declare-r sum [floralin @ ubuntu/home #63] $ sum = testgtingbash: sum: readonly variable converts sum to a custom variable of a non-environment variable: [floralin @ ubuntu/home #64] $ declare + x sum [floralin @ ubuntu/home #65] $ declare-p sumdeclare-ir sum = "450 "Array variable type [floralin @ ubuntu/home #66] $ var [1] =" small min "[floralin @ ubuntu/home #67] $ var [2] = "big min" [floralin @ ubuntu/home 09:32 #68] $ var [3] = "nice min" [floralin @ ubuntu/home 09:32 #69] $ echo "$ {var [1]}, $ {var [2]}, $ {var [3]} "small min, big min, nice min restrictions on file systems and programs ulimit lists all the restricted data values of your current identity: [floralin @ ubuntu/home #70] $ ulimit-a limits that users can only create a 10 MB capacity file: [floralin @ ubuntu/home #74] $ ulimi T-f 10240 [floralin @ ubuntu/home #75] $ ulimit-a tries to create a 20 m file: [floralin @ ubuntu ~ /Desktop/linux_study #81] $ sudo dd if = ~ /Desktop/linux_study/of = 123 bs = 1 M count = 20 [floralin @ ubuntu ~ /Desktop/linux_study #82] $ ll total 8 drwxrwxr-x 2 floralin 4096 Oct 30. /drwxr-xr-x 3 floralin 4096 Oct 30 07:43 .. /-rw-r -- 1 root 0 Oct 30 123 Delete, replace, and replace the variable content Delete and replace the local directory: [floralin @ ubuntu ~ /Desktop/linux_study 09:47 #85] $ echo $ path/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin: /bin:/usr/games:/usr/local/games [floralin @ ubuntu ~ /Desktop/linux_study 09:49 #89] $ echo $ {path #/* local/bin:}/usr/sbin:/usr/bin:/sbin:/bin: /usr/games:/usr/local/games replace sbin with SBIN: floralin @ ubuntu ~ /Desktop/linux_study 13:06 #95] $ echo $ {path/sbin/SBIN}/usr/local/SBIN:/usr/local/bin:/usr/sbin: /usr/bin:/sbin:/bin:/usr/games:/usr/local/games [floralin @ ubuntu ~ /Desktop/linux_study 13:06 #96] $ echo $ {path/sbin/SBIN}/usr/local/SBIN:/usr/local/bin:/usr/SBIN: /usr/bin:/SBIN:/bin:/usr/games:/usr/local/games variable test and content replacement: [floralin @ ubuntu ~ /Desktop/linux_study #97] $ echo $ username # username may not exist or it is a Null String [floralin @ ubuntu ~ /Desktop/linux_study 13:09 #98] $ username =$ {username-root} [floralin @ ubuntu ~ /Desktop/linux_study #99] $ echo $ usernameroot # not set, so take the initiative to give the content named root [floralin @ ubuntu ~ /Desktop/linux_study #100] $ username = "floralin" [floralin @ ubuntu ~ /Desktop/linux_study #101] $ username =$ {username-root} [floralin @ ubuntu ~ /Desktop/linux_study #102] $ echo $ usernamefloralin username-root gets the variable, the option to be tested; username-root variable content, in this example, this part is for the content of the variable not set. Of course, username:-root can replace the following content if the variable content is empty or not set. If the old variable does not exist, replace it with the new variable content. If the old variable exists, replace the new variable content with the old one. [Floralin @ ubuntu ~ /Desktop/linux_study #103] $ unset str; var =$ {str-newvar} [floralin @ ubuntu ~ /Desktop/linux_study #104] $ echo var = "$ var", str = "$ str" var = newvar, str = # Because str does not exist, so var is newvar [floralin @ ubuntu ~ /Desktop/linux_study #105] $ str = "oldvar"; var =$ {str-newvar} [floralin @ ubuntu ~ /Desktop/linux_study #106] $ echo var = "$ var", str = "$ str" var = oldvar, str = oldvar # Because str exists, so var is equal to the str content. The subtraction does not affect the old variables. If you want to replace the old variables together, you need to use the equal sign =: [floralin @ ubuntu ~ /Desktop/linux_study #107] $ unset str; var =$ {str = newvar} [floralin @ ubuntu ~ /Desktop/linux_study #108] $ echo var = "$ var", str = "$ str" var = newvar, str = newvar [floralin @ ubuntu ~ /Desktop/linux_study #111] $ str = "oldvar"; var =$ {str = newvar} [floralin @ ubuntu ~ /Desktop/linux_study #112] $ echo var = "$ var", str = "$ str" var = oldvar, str = oldvar command alias and historical command alias setting alias and unalias command alias can change frequently-used but particularly long commands to concise commands, you can also add default options on some common commands to prevent accidental file deletion. For example, to query hidden files and to list them for a long time and view them on one page, you need to execute "ls-l | more". You can use the following command to simplify it to lm: [floralin @ ubuntu ~ #120] $ alias lm = "ls-l | more" In addition, the alias settings can also replace existing commands. For example, when you log on as a root user, you can use the rm command to delete all files. Therefore, be very careful. rm provides a parameter for us to confirm whether to delete the file-I: [floralin @ ubuntu ~ #123] $ alias rm = "rm-I" [floralin @ ubuntu ~ /Desktop/linux_study #127] $ rm 123rm: remove write-protected regular empty file '20140901 '? Y remove the alias: [floralin @ ubuntu ~ /Desktop/linux_study #128] $ unalias rm history command history query commands that have been executed, last 4: [floralin @ ubuntu ~ /Desktop/linux_study #131] $ history 4 Bash Shell operation environment path and command query sequence set echo alias to echo-n, and then view the echo execution sequence: [floralin @ ubuntu ~ /Desktop/linux_study #132] $ alias echo = "echo-n" [floralin @ ubuntu ~ /Desktop/linux_study #134] $ type-a echoecho is aliased to 'echo-n' echo is a shell builtinecho is/bin/echo bash login and welcome information: /etc/issue,/etc/motd terminal environment setting: stty. how does set check the current key content? You can use stty (setting tty terminal) to help set the input button of the terminal to indicate the meaning. We can use stty-a to list all the buttons in the current environment. Floralin @ ubuntu :~ $ Stty-aspeed 38400 baud; rows 37; columns 80; line = 0; intr = ^ C; quit = ^ \; erase = ^ ?; Kill = ^ U; eof = ^ D; eol = M-^ ?; Eol2 = M-^ ?; Swtch = M-^ ?; Start = ^ Q; stop = ^ S; susp = ^ Z; rprnt = ^ R; werase = ^ W; lnext = ^ V; flush = ^ O; min = 1; time = 0; -parenb-parodd cs8 hupcl-cstopb cread-clocal-extract-encoding brkint-ignpar-parmrk-inpck-istrip-inlcr-igncr unzip ixon-ixoff-iuclc ixany imaxbel extract-olcuc- ocrnl onlcr-onocr-onlret-ofill-ofdel nl0 cr0 tab0 bs0 vt0 ff0isig icanon iexten echo echoe echok-echonl-noflsh-xcase- Stop-echoprtechoctl echoke if you want to delete stty related characters: floralin @ ubuntu :~ $ Stty erase ^ h apart from stty, bash also has its own terminal setting value set. The pipeline command pipe uses the bash command to execute the output data. After a few pieces of data, we can get the desired format. This requires the pipeline. MPs queue uses the '|' definition symbol. In addition, pipeline commands are different from "continuous execution commands. Check the number of files in/etc/. You can use ls/etc to check the files. Using the lese pipeline, you can make the original long directory have the page turning capability: floralin @ ubuntu :~ $ Ls-al/etc | less SELECT command: cut/grep cut can make a piece of information cut out, the processing information is in the unit of "line" to capture the fifth line string: floralinfloralin @ ubuntu: ~ $ Echo $ PATH | cut-d': '-f 5 captures the third and fifth elements: floralinfloralin @ ubuntu :~ $ Echo $ PATH | cut-d': '-f 3. The main purpose of 5cut is to break down the data in the same row, it is most commonly used when analyzing some data or text data. This is because sometimes we use some strings as cut parameters and then cut the data to get the data we need. Grep analyzes a row of information. That is, grep can parse a line of text to obtain the keyword. If this line has a keyword, it will be displayed in the whole row. List the rows containing the "boot" Keyword: floralinfloralin @ ubuntu :~ $ Last | grep "boot": floralinfloralin @ ubuntu :~ $ Last | grep-v "boot" and cut to get the first column: floralinfloralin @ ubuntu :~ $ Last | grep "boot" | cut-d ""-f1 get the lines in/etc/man. config containing MANPATH: floralinfloralin @ ubuntu :~ $ Grep -- color = auto 'manpath'/etc/main. config command: sort the/etc/insserv. conf files by sort, wc, and uniq. The command is: floralinfloralin @ ubuntu :~ $/Etc/insserv. conf | for sorted data, sort only displays the duplicate data: floralinfloralin @ ubuntu :~ $ Last | cut-d ''-f1 | sort | uniq bidirectional redirection: tee transfers the entire data stream to a file or device. Tee sends data streams to files and screens at the same time, while stdout outputs to the screen. Save the last output to the last. list file: floralinfloralin @ ubuntu :~ $ Last | tee last. list Save the ls data ~ /Homefike | more: floralinfloralin @ ubuntu :~ $ Ls-l/home | tee ~ /Homefile | the file after the moretee will be overwritten. If the-a parameter is added, the information can be accumulated: floralinfloralin @ ubuntu :~ $ Ls-l/home | tee-~ /Homefile | more String Conversion command: tr, col, join, paste, expand tr can be used to delete text in a piece of information or replace text information. Col can be used for simple processing and is often used for converting man pages into plain text files for convenient query. The meaning of join is to process data between two files. Add the same row of data in the two files. Paste pasted the two rows together, separated by a tab key in the middle. Expand converts the tab key to a space key.