Linux Shell Common Skills (i)

Source: Internet
Author: User
Tags alphabetic character control characters exit in

I. Special documents : /dev/null and/dev/tty

The Linux system provides two special files that are very useful for Shell programming,/dev/null and/dev/tty. Where/dev/null will lose all the data written to it, in other words, when the program writes the data to this file, it thinks it has completed the operation of writing the data successfully, but actually does nothing. This is useful if you want the exit status of the command, not its output, as shown in the shell code below:
/> VI test_dev_null.sh

#!/bin/bash
If grep Hello testfile >/dev/null
Then
echo "Found"
Else
echo "Not Found"
Fi
After saving and exiting in VI, execute the following command:
/> chmod +x test_dev_null.sh#使该文件成为可执行文件
/> Cat > Testfile
Hello my friend
CTRL + D #退出命令行文件编辑状态
/>./test_dev_null.sh
Found #这里并没有输出grep命令的执行结果.
Make the following changes to the shell script:
/> VI test_dev_null.sh

#!/bin/bash
If grep Hello Testfile
Then
echo "Found"
Else
echo "Not Found"
Fi
After you save the exit in VI, execute the script again:
/>./test_dev_null.sh
Hello my friend #grep命令的执行结果被输出了.
Found

Now let's look at the use of/dev/tty. When the program opens the file, Linux automatically redirects it to a terminal window, so the file is especially useful for reading human input. See the following shell code:
/> VI test_dev_tty.sh

#!/bin/bash
printf "Enter New password:" #提示输入
Stty-echo #关闭自动打印输入字符的功能
Read Password </dev/tty #读取密码
printf "\nenter again:" #换行后提示再输入一次
Read Password2 </dev/tty #再读取一次以确认
printf "\ n" #换行
Stty Echo #记着打开自动打印输入字符的功能
echo "Password =" $password #输出读入变量
echo "Password2 =" $password 2
echo "All Done"

After saving and exiting in VI, execute the following command:
/> chmod +x test_dev_tty.sh#使该文件成为可执行文件
/>./test_dev_tty
Enter New Password: #这里密码的输入被读入到脚本中的password变量
Enter again: #这里密码的输入被读入到脚本中的password2变量
Password = Hello
Password2 = Hello
All done

two. Simple command tracking:

The Linux shell provides two ways to track the commands in a shell script to help us pinpoint problems in the program. The following code is the first method that prints all executed commands in the shell script to the terminal and adds "+" before the command: A space followed by a plus sign.
/> Cat > Trace_all_command.sh
W.H.O. | Wc-l #这两条Shell命令将输出当前Linux服务器登录的用户数量
CTRL + D #退出命令行文件编辑状态
/> chmod +x trace_all_command.sh
/> sh-x./trace_all_command.shThe #Shell执行器的-x option opens the execution tracking feature of the script.
+ wc-l #被跟踪的两条Shell命令
+ Who
2 #实际输出结果.
Another way the Linux shell provides is to print only partially executed shell commands, which are especially useful when debugging more complex scripts.
/> Cat > Trace_patial_command.sh
#! /bin/bash
Set-x #从该命令之后打开跟踪功能
Echo 1st Echo #将被打印输出的Shell命令
Set +x #该Shell命令也将被打印输出, however, after the command is executed, all commands will no longer print out
Echo 2nd Echo #该Shell命令将不再被打印输出.
CTRL + D #退出命令行文件编辑状态
/> chmod +x trace_patial_command.sh
/>./trace_patial_command.sh
+ Echo 1st Echo
1st Echo
+ Set +x
2nd Echo

three. Regular expression basic Syntax description:

Two regular expression rules are available in the Linux shell environment, one is the basic regular expression (BRE) and the other is an extended regular expression (ERE).
The following is a list of the syntax for both expressions, and it is important to note that if you do not have a meta character that is explicitly stated, it will be used for both Bre and Ere, otherwise it will apply to the specified pattern.

Regular meta characters Mode meaning Case
\ Usually used to turn off the special meaning of its subsequent characters and restore its original intent. \ (... \), where the parentheses represent only parentheses.
. matches any single character. A.B, will match ABB, ACB, etc.
* A single character that matches the 0-n before it. A*b, will match AB, AAB, Aaab and so on.
^ Matches the immediately preceding regular expression, at the beginning of the row. ^ab, will match the ABC, ABD, etc., but does not match the cab.
$ Matches the regular expression immediately following the line at the end of the row. ab$, will match AB, cab, etc., but does not match ABC.
[...] A square bracket expression that matches any character inside it. Where-the range of consecutive characters is represented, and the first character in square brackets has a reverse meaning, which matches any character that is not within the list (square brackets). If you want the] and-to indicate its original intent, you need to place it at the first character position of the square brackets, such as []ab] or [-ab], if both characters exist at the same time, it will be] placed at the first character position--placed at the tail end, such as []ab-]. [a-ba-z0-9!] Represents all uppercase and lowercase letters, numbers, and exclamation marks. [^ABC] represents all characters except A, B, and C. [Tt]om, can match Tom and Tom.]
\{n,m\} An interval expression that matches the number of occurrences of a single character in front of it, \{n\} means repeated n times, and \{n,\} means at least repeated n times, and \{n,m\} means repeating n to M times. Ab\{2\} indicates that abb;ab\{2,\} represents ABB, ABBB, and so on. Ab\{2,4\} represents ABB, ABBB and ABBBB.
\(...\) The pattern between parentheses is stored in a special "reserved space". You can store up to 9 independent sub-patterns in a single mode. Text that matches the sub-pattern can be reused in the same pattern by \1 the escape sequence to \9. \ (ab\). *\1 indicates that AB combinations occur two times, and there can be any number of characters between two times, such as Abcdab, Abab, and so on.
{N,m} (ERE) Its function is equivalent to the above \{n,m\}, just no longer write \ Escape character. ab+ matches AB, ABBB, etc., but does not match a.
+ (ERE) Compared to the preceding asterisk, + matches the 1-n instance of the preceding regular expression.
? (ERE) Matches 0 or 1 of the preceding regular expression. AB only matches a or AB.
| (ERE) Matches the regular expression before and after the | symbol. (AB|CD) match ab or CD.
[: Alpha:] Matches an alphabetic character. [[: alpha:]!] The ab$ matches the cab, Dab, and!ab.
[: Alnum:] Matches alphabetic and numeric characters. [[: Alnum:]]ab$ matches 1ab, AaB.
[: Blank:] Matches the space and tab characters. [[: Alnum:]]ab$ matches 1ab, AaB.
[: Cntrl:] Match control characters.
[:d Igit:] Match numeric characters.
[: Graph:] Matches a non-whitespace character.
[: Lower:] Matches lowercase alphabetic characters.
[: Upper:] Matches uppercase characters.
[:p UNCT:] Match punctuation characters.
[: Space:] Matches a blank (whitespace) character.
[: Xdigit:] Matches a hexadecimal number.
\w Matches any character that consists of letters and numbers, equivalent to [[: Alnum:]_]
\w Matches any character that is not a letter or number, equivalent to [^[:alnum:]_]
\<\> Matches the beginning and end of a word. \<read matching readme,me\> matching readme.


The following list shows the types of regular expressions supported by tools or commands that are commonly used in Linux shells.

Grep Sed Vi Egrep Awk
BRE * * *
Ere * *


four. Use the Cut command to select a field:

The Cut command is used to cut down the data in a text file, which can be either a field type or a character type. Here is an example of an application:
/> cat/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
... ...
/> cut-d:-F 1,5/etc/passwd The colon after the #-d represents the delimiter between fields, and-F indicates which fields are taken after the split
Root:root #这里取出的是第一个和第五个字段.
Bin:bin
Daemon:daemon
Adm:adm
... ...
/> cut-d:-F 3-/etc/passwd #从第三个字段开始显示 until the last field.
0:0:root:/root:/bin/bash
1:1:bin:/bin:/sbin/nologin
2:2:daemon:/sbin:/sbin/nologin
3:4:adm:/var/adm:/sbin/nologin
4:7:lp:/var/spool/lpd:/sbin/nologin
... ...
It is further explained here that the cut command can also be used to cut partial characters with a scalar number of characters, which is implemented with the-C option and cannot coexist with the-D option.
/> cut-c 1-4/etc/passwd #取每行的前1-4 characters.
/> cut-c-4/etc/passwd#取每行的前4个字符.
Root
Bin
Daem
Adm:
... ...
/> cut-c4-/etc/passwd#取每行的第4个到最后字符.
T:x:0:0:root:/root:/bin/bash
: X:1:1:bin:/bin:/sbin/nologin
Mon:x:2:2:daemon:/sbin:/sbin/nologin
: X:3:4:adm:/var/adm:/sbin/nologin
... ...
/> cut-c1,4/etc/passwd#取每行的第一个和第四个字符.
Rt
B:
Dm
A:
... ...
/> cut-c1-4,5/etc/passwd#取每行的1-4 and 5th characters.
Root
Bin:x
Daemo
Adm:x

Five. Count the number of rows, words, and characters:

Linux provides a simple tool for the WC to complete this function, see the following use cases:
/> Echo This is a test of the Emergency Broadcast System | WC
1 9 #1行, 9 words, 49 characters
/> Echo Testing one and three | wc-c
#22个字符
/> Echo Testing one and three | wc-l
1 #1行
/> Echo Testing one and three | wc-w
4 #4个单词
/> Wc/etc/passwd/etc/group#计算两个文件里的数据.
1933/etc/passwd
906/etc/group
101 133 2839 Total Dosage

Six. Extract the beginning or end of the line:

Sometimes you'll need to extract a few lines from a text file, mostly near the beginning or the end. such as viewing the work of the logbook. The Linux shell provides two commands for head and tail to do the work. See the following use cases:
/> head-n 5/etc/passwd#显示输入文件的前五行.
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

/> tail-n 5/etc/passwd#显示输入文件的最后五行.
Sshd:x:74:74:privilege-separated Ssh:/var/empty/sshd:/sbin/nologin
Mysql:x:27:27:mysql Server:/var/lib/mysql:/bin/bash
Pulse:x:496:494:pulseaudio System Daemon:/var/run/pulse:/sbin/nologin
Gdm:x:42:42::/var/lib/gdm:/sbin/nologin
Stephen:x:500:500:stephen:/home/stephen:/bin/bash
If the user wants to view the uninterrupted growth of the log (such as the Service program output), you can use the tail-f option, so that the tail command does not automatically exit and must be forced to exit with the CTRL + C command, so this option is not suitable for use in shell scripts, see the following case:
/> tail-f-N 5 my_server_log
... ...
^c #CTRL +c exits to the command line prompt state.

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.