Bash functions and usage skills

Source: Internet
Author: User
Tags control characters
Bash functions and usage skills a complete computer architecture includes: hardware and software, and software is divided into system software and application software, only the kernel part of the system software is responsible for hardware management and operation. users cannot deal with the hardware or Kernel. Users can use applications or some systems... information & n Bash functions and usage tips a complete computer architecture includes: hardware and software, and software is divided into system software and application software, only the kernel of the system software is responsible for hardware management and operation. users cannot deal with the hardware or kernel, the user sends commands through applications or some system software (possibly by sending an email via a browser), which will be translated and sent to the kernel, the kernel schedules hardware resources to complete the operation (for example, using the NIC to send packets) after knowing the user's requirements ). Www.2cto.com in Linux, we usually use Shell to communicate with the kernel, and ultimately achieve the purpose of using computer resources. Due to the openness of Linux, there are many options for Shell in Linux, in CentOS 6.3, you can use the following Shell types:/bin/sh,/bin/bash,/bin/tcsh, and/bin/csh, the/etc/shells file shows which shells are available in the current system. Different shells have different features and operation methods. here we use the default Shell used in CentOS to explain the case of Bash. 1.2 BASH function introduction 1.2.1 command history Bash has the function of automatically recording command history. The commands executed by the user will be automatically recorded in their home directory during logout. bash_history is a hidden file. There are many ways to view the history of these commands. you can directly open the file to view the history, or you can use the keyboard's up and down keys to flip up or down the history, you can also use the history command to view the history. The history of all commands is numbered. The number of records that can be recorded in the command history is determined by the HISTSIZE variable. by default, CentOS 6.3 defines HISTSIZE = 1000 through the/etc/profile file, that is, up to 1000 recent commands can be recorded. when 1,001st commands are executed, the first command will be overwritten, and all historical records can be cleared by executing the history-c command. In addition to viewing the history, you can also directly call the history record to execute the command again when necessary: www.2cto.com 1. go through the previous commands with the up/down key, find the appropriate command, and press enter to execute it. 2. enter! String call Command History (string is a keyword), such! Vim will call the last executed command starting with vim. Or pass! N to accurately locate historical records, such! 242 The system will directly call 242nd records of the Command History and execute the command. 3. press Ctrl + r to enable the search function. Then, enter a keyword to search for related commands in the Command History. press enter to complete the operation. If no appropriate command is found, press ESC to exit the search. 1.2.2 command alias in CentOS 6.3, we can directly use the ll command to display the detailed information of the file. In fact, there is no such command in the system as ll, it is only an alias defined in advance. the advantage of an alias is that it can simplify the abbreviation of a long command, creating aliases for commonly used and complex commands and options can greatly improve daily work efficiency. [Root @ centos6 ~] # Alias # View all current aliases of the system alias cp = 'CP-I 'alias l. = 'ls-d. * -- color = auto'alias ll = 'ls-l -- color = Auto' alias ls = 'ls -- color = Auto' alias mv = 'MV-I 'alias rm =' rm-I '[root @ centos6 ~] # Alias h5 = 'head-5' # define a new alias [root @ centos6 ~] # Unalias h5 # cancel alias definition 1.2.3 the standard input device for MPs pipes and redirection Bash is a keyboard, mouse, tablet, and other devices. The standard output is a display. generally, we enter commands and execute them through the keyboard, the system displays the returned information on the screen. by default, both the correct and wrong information are output to the display. The file descriptor of the standard input is 0, the file descriptor of the standard output is 1, and the file descriptor of the error output is 2. But sometimes we need to change the standard input and output methods. in Linux, we can use the redirection operators (<,>, <,>, |) to redefine the input and output. The www.2cto.com pipeline allows us to connect multiple commands for use. it redirects the standard output of a command to the next command and serves as the standard input of the command. For example, if you use the ifconfig eth0 | grep 'inet addr 'command to filter rows containing IP addresses, ifconfig itself will output a large amount of network interface information, because the pipeline symbol (|) is used here) therefore, all the output of the ifconfig command will be used as the grep command input, and finally the line containing IP addresses will be filtered. The standard output information is output to the display, and sometimes you may not need the output information, or you do not need to see the information on the screen temporarily; in addition, the input is generally input through the keyboard, and sometimes you may want to read the input information from the file. For output redirection, you can use the> or> symbol and use> to import the output to a file. if the file does not exist, the file is created. if the file already exists, the content of the file is overwritten; use> to append the output to a file. In addition, use the 2> or 2> symbol to redirect the corresponding error message. Input redirection can be used <符号,它可以可以帮助我们从文件中提取输入信息。以下通过几个简单的实例演示重定向的使用方法:[root@centos6 ~]# rpm -qa |grep gcc    #查询计算机中是否安装了gcc软件 [root@centos6 echo “pass” | passwd --stdin tom #设置tom的密码为pass [root@centos6 ls #查看当前文件列表 anaconda-ks.cfg  install.log  install.log.syslog [root@centos6> List.txt outputs are saved to list.txt, and no output is displayed on the screen [root @ centos6 ~] # Hostname> list.txt add the alias to the end of the list.txt file [root @ centos6 ~] # Mail-s test xx@gmail.com <list.txt # send mail, mail content from the file [root @ centos6 ~] # Ls-l abc install. log # view the file details. abc does not have ls: cannot access abc: No such file or directory-rw-r --. 1 root 9064 Dec 4 install. log [root @ centos6 ~] # Ls-l abc install. log 2> error.txt # Only redirects errors. incorrect output-rw-r --. 1 root 9064 Dec 4 install. log [root @ centos6 ~] # Ls-l abc install. log> all 2> & 1 # import standard output and error output to all [root @ centos6 ~] # Ls-l abc install. log> all 2> & 1 # standard output and error output are appended to all [root @ centos6 ~] # Ls-l abc install. log &> all # standard output and error output are imported to all 1.2.4 shortcut keys. Bash provides a large number of shortcut keys for users, mastering all or part of these shortcuts will benefit you a lot in your work. in the beginning, you may need to spend a lot of time remembering these shortcuts, but once you remember them, your work efficiency will soar. Common shortcut keys and their function descriptions are listed below. Function description: Ctral + a move the cursor to the beginning of the line Ctrl + e move the cursor to the end of the line Ctrl + f move the cursor to the right one character Ctrl + B move the cursor to the left one character Ctrl + l clear screen, equivalent to the clear command Ctrl + u to delete the cursor to the beginning of the line character Ctrl + k to delete the cursor to the end of the line character Ctrl + c to terminate the process Ctrl + z to suspend the process (you can view through the jobs command) pending processes) ctrl + w delete the word before the cursor (separated by space) Alt + d delete the word after the cursor 1.3 BASH tips 1.3.1 redirection tips 1. if you write a script to automatically change the password of the system account, the system will prompt a message such as successfully after the password is successfully modified by default, if you want to change the password of 20 accounts, 20 such information is displayed. In such cases, a large number of successful prompts are not the focus of our attention, and administrators are more concerned about error prompts. Therefore, you can consider shielding the standard output correctly. Linux provides a special device/dev/null, which is like a sun black hole. writing anything into it will always disappear. A large amount of meaningless output information can be imported to/dev/null devices through pipelines. [Root @ centos6 ~] # Echo "pass" | passwd -- stdin root>/dev/null 2. standard output and error output separation redirection many times after the script that runs automatically is compiled, it is required to run in the unattended mode when the server load is low in the middle of the night, when the administrator checks the script running status, he wants to see which commands have been successfully executed and which commands have encountered problems, in addition, to facilitate management, correct output and error output must be divided and written in two different record files. Therefore, separation and redirection are particularly important. Next we will check whether there is a tom user in the system. if so, we will record tom-related information to the user file; otherwise, it will be recorded in the error File: [root @ centos6 ~] # Id tom> user 2> error 2.1.1 command sequence in Linux, we can use control characters (;, &, ||, &) to control the command execution method. The [&] control operator enables a sub-Shelll to be started and executed in the background. the [;] control character can be used to combine multiple commands, however, there is no logical relationship between multiple commands, and only the commands are executed in sequence. you can use the [&] control character to combine multiple commands, however, only when the current command is successfully executed will the command after the [&] control operator; [|] control characters play the opposite role, only if the current command fails to be executed will the command following the [|] Control Letter be executed. The following is an example. 1. the Firefox browser is started through the front-end so that the current Shell cannot be used for the moment: [root @ centos6 ~] # Firefox2. run the browser in the background without affecting the use of the current Shell: [root @ centos6 ~] # Firefox & 3. all commands are executed in sequence (whether or not the preceding command is successful, the subsequent commands must be executed normally): [root @ centos6 ~] # Ls/tmp; ls/root; ls/home4. if a file exists, the content of the file is displayed. Otherwise, an error is returned (cat is executed only when ls execution is successful ): [root @ centos6 ~] # Ls test.txt & cat test.txt 5. if the gedit editor is available, open the program. otherwise, open the vim editor: [root @ centos6 ~]. # Gedit | vim6. if id tom is successfully executed, the user's screen displays Hi and tom; otherwise, No such user: [root @ centos6 ~] # Id tom &>/dev/null & echo "Hi, tom "| echo" No such user "instance 5 first shields all output of the id command through the &> redirection, use & | to determine whether the id command is successfully executed. if the command is successful, a greeting is displayed. Otherwise, the user is not displayed. 2.1.2 job control when a process is started using commands in the Bash environment, use the & symbol to enable the process to go to the background for execution, you can also press Ctrl + z to place the process in the background and pause the execution. you can view the background processes at any time using the jobs command, in addition, a serial number is assigned to each of these processes. <编号> The background process can be re-called back to the foreground for execution. [Root @ centos6 ~] # Firefox & [root @ centos6 ~] # Jobs [1] + Running firefox & [root @ centos6 ~] # Fg 12.1.3 use www.2cto.com to extend the string that can be produced for command lines or scripts through brackets {}. The brackets can contain continuous sequences or multiple items separated by commas, a continuous sequence includes a starting point and an ending point and uses .. split. Next let's take a look at the specific syntax format case: [root @ centos6 ~] # Echo {a, B, c} a B c [root @ centos6 ~] # Echo user {1, 5, 8} user1 user5 user8 [root @ centos6 ~] # Echo {0 .. 10} 0 1 2 3 4 5 6 7 8 9 10 [root @ centos6 ~] # Echo {0... 10... 2} 0 2 4 6 8 10 [root @ centos6 ~] # Echo a {2 ..-1} a2 a1 a0 A-1 [root @ centos6 ~] # Mkdir/tmp/{dir1, dir2, dir3} [root @ centos6 ~] # Ls-ld/tmp/dir {1, 2, 3} [root @ centos6 ~] # Chmod 777/tmp/dir {1, 2} source http://manual.blog.51cto.com/3300438/1131800
Related Article

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.