Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use vim and shell scripts in linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

This article shows how to use vim and shell scripts in linux, and the code is concise and easy to understand if you encounter this question in your daily work. I hope you can find a solution to your questions through this article.

1. Define a command alias that is valid for all users

Defining a command alias that works for all users requires changes to the global configuration file / etc/bashrc

For example, we edit / etc/bashrc as user root and add alias like='ls' to the last line of the file

When we start a new shell process and list the command aliases, we will find the alias like that we just defined

[root@localhost] # tail / etc/bashrc. "$I" > / dev/null fi fi done unset i unset-f pathmungefi#vim:ts=4:sw=4alias like='ls' / / shows what I appended to the end of the file here Define alias like [root@localhost ~] # aliasalias cls='clear'alias cp='cp-i'alias egrep='egrep-- color=auto'alias fgrep='fgrep-- color=auto'alias grep='grep-- color=auto'alias l.='ls-d. *-- color=auto'alias like='ls' / / alias likealias ll='ls-l-color=auto'alias ls='ls-- color=auto'alias mv='mv-i'alias rm='rm-i'alias which='alias | / usr/bin/which-- tty-only-- read-alias-- show-dot-- show-tilde' [root@localhost ~] # like-l Total dosage 12drwxr-xr-x. 2 root root 6 April 3 00:02 00-02-17 Murray RW Murray. 1 root root 1257 March 19 19:05 anaconda-ks.cfgdrwxr-xr-x. 2 root root 6 March 19 14:47 Desktopdrwxr-xr-x. 2 root root 6 March 19 14:47 Documentsdrwxr-xr-x. 2 root root 6 March 19 14:47 Downloads-rw-r--r--. 1 root root 132 April 30 16:44 grep.txt-rwxr-xr-x. 1 jacky docker 511 April 19 20:44 inittabdrwxr-xr-x. 2 root root 6 March 19 14:47 Musicdrwxr-xr-x. 2 root root 6 March 19 14:47 Picturesdrwxr-xr-x. 2 root root 6 March 19 14:47 Publicdrwxr-xr-x. 2 root root 6 March 19 14:47 Templatesdrwxr-xr-x. 2 root root 6 March 19 14:47 Videosdrwxr-xr-x. 2 root root 6 March 19 14:47 Desktop

2. Display lines in the / etc/passwd file that do not end with / bin/bash.

~] # grep-v "/ bin/bash$" / etc/passwd

3. Find the lines in the / etc/passwd file that contain two or three digits.

Grep: ~] # grep "\" / etc/passwdegrep: ~] # egrep "\" / etc/passwd

4. Display lines in the / proc/meminfo file that begin with uppercase or lowercase s; implemented in at least three ways

A: ~] # grep "^ [sS]" / proc/meminfo

B: ~] # egrep "^ (s | S)" / proc/meminfo

C: ~] # grep "^ s" / tmp/super.txt & & grep "^ S" / tmp/super.txt

D: ~] # grep-I "^ s" / proc/meminfo

5. Use echo to output an absolute path, use egrep to fetch the path name, and type to execute the result of dirname / etc/passwd

Grep implementation

[root@localhost ~] # echo / etc/rc.d/init.d/functions | grep-o "^ /. * /"

/ etc/rc.d/init.d/

Egrep implementation

[root@localhost ~] # echo / etc/rc.d/init.d/functions | grep-E-o "^ / (. *) /"

/ etc/rc.d/init.d/

6. Find out the Ip address in ifconfig. The result is required to show only the IP address.

A: parsing: because the range of IP address is 1.0.0.1-223.255.255.255.254, match according to 1-223.0-255.0-255.1-254

[root@localhost ~] # ifconfig | egrep-o "\. (\.) {2}\"

192.168.220.7

127.0.0.1

192.168.122.1

B: take out the ip address of the network card ens33, filter it to the line of Ip, and then use the cut command to take the 10th column with a space delimiter, which happens to be the IP address.

[root@localhost tmp] # ifconfig ens33 | grep "inet.*broad" | cut-d'-f10

192.168.220.7

7. Vim customizes automatic indentation of four characters.

In the last row mode

: set tabstop=4

8. Write a script to automatically add three users and calculate the sum of the uid of the three users.

[root@localhost tmp] # cat test.sh #! / bin/bashid neo | | useradd neoid jerry | | useradd jerryid louis | | useradd louisUID1= `id-u neo`UID2 = `id-u jerry`UID3 = `id-u louis`echo "$[$UID1+$UID2+$UID3]" [root@localhost tmp] #. Test.sh id: neo: no such userid: jerry: no such userid: louis: no such user15033

9. Sample demonstration of find usage and common usage.

Find: a real-time lookup tool that completes file lookup by traversing the file system hierarchy under the specified path

Working characteristics:

The speed of searching is a little slower.

Precise search

Real-time search

Usage:

Find [OPTIONS] [search start path] [search condition] [processing Action] processing action can be added to ls to indicate that the results are arranged by ls-l after search.

Find start path: specify the specific search target start path; default is the current directory

Search criteria: the specified search criteria can be carried out according to file name, size, type, dependency, permissions, etc.; the default is to find all files under the specified path

Processing action: actions on files that match the search for Tianjian, such as deletion, etc.; default is output to standard output

Search criteria: expressions: options and test tests: the results are usually Boolean ("true" or "false") look up by file name:-name "pattern"-iname "pattern" (wildcards that support glob style) *,?, [], [^]-regex pattern: finds files based on regular expression patterns, matching the entire path, not the base name. Search according to file dependency:-user USERNAME: find all files of the owner-specified user;-group GRPNAME: find all files of the specified group -uid UID: find all files in the UID specified by the owner;-gid GID: find all files in the GID specified by the group;-nouser: find files without owners;-nogroup: find files without groups Find according to the type of file:-type TYPE: F: ordinary file d: directory file l: symbolic link file b: block device file c: character device text Piece p: pipe file s: socket file combination test: with:-a Default combinational logic Must meet all the conditions or:-o, if you meet one of the conditions, it can not be:-notgrad!! a-a! B =! (A-o B)! a-o! B =! (A-a B)

Example.

Example1 [root@localhost tmp] # find / tmp-nouser-type f-ls67654200 4-rw-r--r-- 1 5010 5010 541 February 13 17:48 / tmp/fstab [root@localhost tmp] # find / tmp-nouser-a-type f-ls67654200 4-rw-r--r-- 1 5010 5010 541 February 13 17:48 / tmp/fstabexamle21, find all files under the / tmp directory that are mainly non-root ] # find / tmp-not-user root-ls2, find the files in the / tmp directory whose filenames do not contain the fstab string;] # find / tmp-not-iname "* fatab*" 3, find the files under the / tmp directory that are mainly non-root and the filenames do not contain the fstab string ] # find / tmp-not-user root-a-not-iname "* fstab*"-ls or] # find / tmp-not\ (- user root-o-iname "* fstab*"\)-ls

Find based on file size

-size [+ | -] # UNIT commonly used unit: KMagazine G # UNIT: (#-1 UNIT #] here it means that the value greater than-1 is less than or equal to # value-# UNIT: [0mai Mizhi 1] + # UNIT: (#, oo)

Find based on the timestamp:

In "days":-atime [+ | -] # / access time #: [#, #-1)-#: (#, 0] + #: (oo #-1] the # here is a negative number in the trailing expression-mtime / modification time-ctime / change time in minutes:-amin-mmin-cminexample. Find the file under / etc that was accessed a week ago #] find / etc-atime + 7-ls24 within hours / etc modified file #] find / etc-mtime-1-ls

Find based on permissions

-perm [/ | -] mode mode: exact permission matching / mode: any one of the permissions of any type of user (uMagnedPol x) satisfies the condition; there is an OR relationship between 9 permissions-mode: every one of the permissions of each type of user (uMagne gMago) satisfies the condition at the same time There is a "and" relationship between 9-bit permissions.

Example.

[root@localhost test] # touch a b c d e f g [root@localhost test] # [root@localhost test] # chmod 640a [root@localhost test] # chmod 666b [root@localhost test] # chmod 440c [root@localhost test] # chmod 775d [root@localhost test] # chmod 777e [root@localhost test] # ll Total usage 0Kuhran. 1 root root February 15 15:50 a murmur RW Murray. 1 root root February 15 15:50 bMurray Rafael. 1 root root February 15 15:50 c-rwxrwxr-x. 1 root root February 15 15:50 d-rwxrwxrwx. 1 root root Feb.15 15:50 eMeli RW Murray RMI Murray. 1 root root February 15 15:50 f murr RW Murray r Murray. 1 root root February 15 15:50 g [root@localhost test] # find. /-perm 644-ls372669 0-rw-r--r-- 1 root root 0 February 15 15:50. / f3726700 0-rw-r--r-- 1 root root 0 February 15 15:50. / g [root@localhost test] # find. /-perm / 2222-ls372648 0 drwxr-xr-x 2 root Root 69 February 15 15:50. / 372650 0-rw-r- 1 root root 0 February 15 15:50. / a372665 0-rw-rw-rw- 1 root root 0 February 15 15:50. / b372667 0-rwxrwxr-x 1 root root 0 February 15 15:50. / d372668 0-rwxrwxrwx 1 root root February 15 15:50. / e372669 0-rw-r--r-- 1 root root 0 February 15 15:50. / f3726700 0-rw-r--r-- 1 root root 0 February 15 15:50. / g [root@localhost test] # find. /-perm / 111-ls372648 0 drwxr-xr-x 2 root root 69 February 15 15:50. / 372667 0-rwxrwxr-x 1 root root 0 February 15 15:50. / d372668 0-rwxrwxrwx 1 root root 0 February 15 15:50. / e [root@localhost test] # find. /-perm / 001-ls372648 0 drwxr-xr-x 2 root root 69 February 15 15:50. / 372667 0-rwxrwxr-x 1 root root 0 February 15 15: 50. / d372668 0-rwxrwxrwx 1 root root 0 February 15 15:50. / e [root@localhost test] # find. /-perm / 002-ls372665 0-rw-rw-rw- 1 root root 0 February 15 15:50. / b

Processing action

-print: output to standard output; default action;-ls: similar to executing the "ls-l" command on the found file to output the file details;-delete: delete the found file -fls / PATH/TO/SOMEFILE: saves the long format information of all found files to the specified file (similar to saving the output of ls to the specified file);-ok COMMAND {}\;: executes the command represented by COMMAND for each file found; each operation is confirmed by the user-exec COMMAND {}\; each file found executes the command indicated by COMMAND Execute the command directly without user confirmation

Example.

[root@localhost tmp] # find. /-nouser. / fstab [root@localhost tmp] # find. /-nouser-a-nogroup. / fstab [root@localhost tmp] # find. /-nouser-a-nogroup-ls 67654200 4-rw-r--r-- 1 5010 5010 541 February 13 17:48. / fstab [root@localhost tmp] # find. /-nouser-a-nogroup-ok chown root:root {}\

< chown ... ./fstab >

? Y [root@localhost tmp] # total amount of ll 12-rw-r--r--. 1 root root 451 February 13 17:48 crontab-rw-r--r--. 1 root root 541 February 13 17:48 fstab [root@localhost test] # find. /-perm / 002. / b. / e [root@localhost test] # find. /-perm / 002-exec mv {} {}. Danger\ [root@localhost test] # Total amount of ll 4-rw-r-. 1 root root February 15 15:50 a-rw-r--r--. 1 root root 144 February 15 16:13 abc.test-rw-rw-rw-. 1 root root February 15 15:50 b.danger-Ruki Rafael. 1 root root February 15 15:50 c-rwxrwxr-x. 1 root root February 15 15:50 d-rwxrwxrwx. 1 root root February 15 15:50 e.danger-rw-r--r--. 1 root root February 15 15:50 f-rw-r--r--. 1 root root February 15 15:50 g Note: when find passes the found file path to the following command, it first finds out all the qualified file paths and passes them to the following command at one time; but some commands cannot accept excessively long parameters, and the command execution will fail; another way can avoid this problem; find | xargs COMMAND

Parameter substitution command: xargs

Xargs [option] COMMAND

-o: if the input stdin contains special characters, such as `,\, space characters, etc., this parameter can restore it to a normal character, and this parameter can be used in a special state. -e: this means EOF (end of file), followed by a string char. When xargs parses this string, it stops working. -p: when executing the parameters of each command, the user is asked what he means. -n: followed by the number of times, each time the command command is executed, several parameters are used. When there is no command followed by xargs, the default is to output with echo.

Example

1. Find all files or directories under / var directory that belong to root and belong to mail; [root@localhost ~] # find / var-user root-group mail-ls67161923 0 drwxrwxr-x 2 root mail 219 February 14 14:43 / var/spool/mail2, find all files or directories under / usr directory that do not belong to root,bin or hadoop; use two methods A. [root@localhost ~] # find / usr-not-user root-a-not-user bin-a-not-user hadoop-ls 96954 0 drwx- 2 polkitd root 287 March 19 2019 / usr/share/polkit-1/rules.d1000398 16-rwsr-sr-x 1 abrt abrt 15432 November 14 2018 / usr/libexec/abrt-action-install-debuginfo-to-abrt-cacheB. [root@localhost ~] # find / usr- Not\ (- user root-o-user bin-o-user hadoop\)-ls 96954 0 drwx- 2 polkitd root 287 March 19 2019 / usr/share/polkit-1/rules.d1000398 16-rwsr-sr-x 1 abrt abrt 15432 November 14 2018 / usr/libexec/abrt-action-install-debuginfo-to-abrt-cache3, Find / etc directory and its contents have been modified in the last week. A file or directory that is not owned by a root user or hadoop user [root@localhost ~] # find / etc-mtime-7-a-not\ (- user root-o-user hadoop\)-ls4, find files or directories that do not have owners or groups on the current system and have been accessed in the last week [root@localhost] # find /\ (- nouser-o-nogroup\)-atime-7-ls 105 0 drwx- 3 5010 5010 78 February 14 14:43 / home/neo67108939 0 drwxr-xr-x 4 5010 5010 39 March 19 2019 / home/neo/.mozilla134284490 0 drwxr-xr-x 2 5010 5010 6 June 10 5010 / home/neo/. Mozilla/extensions201326666 0 drwxr-xr-x 2 5010 5010 June 10 2014 / home/neo/.mozilla/pluginsfind:'/ proc/17459/task/17459/fd/5': does not have that file or directory find:'/ proc/17459/task/17459/fdinfo/5': does not have that file or directory find:'/ proc/17459/fd/6': does not have that file or directory find:'/ proc / 17459 mail fdinfoswap: there is no such file or directory 67654182 0-rw-rw---- 1 5010 Universe 0 February 14 14:43 / var/spool/mail/neo5, Find all files in the / etc directory that are larger than 1m and have a normal file type [root@localhost] # find / etc-size + 1m-type f-exec ls-lh {}\; 1 root root 7.2m March 19 2019 / etc/udev/hwdb.bin-rw-r--r--. 1 root root 1.4m March 19 2019 / etc/selinux/targeted/contexts/files/file_contexts.bin-rw-r--r--. 1 root root 3.7m March 19 2019 / etc/selinux/targeted/policy/policy.31-rw-. 1 root root 3.7m March 19 2019 / etc/selinux/targeted/active/policy.kern-rw-. 1 root root 3.7m March 19 2019 / etc/selinux/targeted/active/policy.linked-rw-r--r--. 1 root root 1.4m April 11 2018 / etc/brltty/zh-tw.ctb6, find files in the / etc directory where all users do not have write permission [root@localhost] # find / etc-not-perm / 222r-ls33615593 183421 March 19 2019 / etc/pki/ca-trust/extracted/java/cacerts67181253 328-Rafael-1 root root 334001 March 19,2019 / etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt100708941 248r Mustang-1 root root 251593 March 19 2019 / etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem100708942 Rafael-1 root root 201168 March 19 2019 / etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem100708943 Rafael-1 root root 171863 March 19 2019 / etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem235701 4- Root 531 May 3 2017 / etc/lvm/profile/cache-mq.profile235702 4-root root 339 May 3 2017 / etc/lvm/profile/cache-smq.profile235703 4-etc/lvm/profile/cache-smq.profile235703 4-root root 3020 August 5 2017 / etc/lvm/profile/command_profile_template.profile235704 4-root-1 root root 2309 May 3 2017 / root root 4-root root 4-May 3 2017 / etc/lvm/profile/thin-generic.profile235707-1 root root 828 August 5 2017 / etc/lvm/profile/metadata_profile_template.profile235706 4-etc/lvm/profile/thin-generic.profile235707 4-root root-1 root root 80 May 3 2017 / etc/lvm/profile/thin-performance.profile100769850 4-r-1 root root 45 March 19 2019 / etc/openldap/certs/password67183454 4-1 root root 975 February 16 11:25 / etc/gshadow34025845 4-r--r--r -- 1 root root 460 April 11 2018 / etc/dbus-1/system.d/cups.conf67183449 4-1 root root 1608 Feb 16 11:25 / etc/shadow67294134 4-1 root root 1579 Feb 15 12:18 / etc/shadow-100861801 4-Ruki-1 root root 63 August 23 2017 / etc/ld.so.conf.d/kernel-3.10.0-693.el7.x86_64.conf100821074 4-Rafael-1 root root-63 Mar 18 2019 / etc/ld.so.conf.d/kernel-3.10.0-957.10.1.el7.x86_64.conf102850752 7348-Ruki-1 root root 7522390 March 19 2019 / Etc/udev/hwdb.bin67294386 4-etc/pam.d/cups67191422 4-1 root root-1 root root 33 March 19 2019 / etc/machine-id790438 4-r Muhashi-1 root root 146 April 11 2018 / etc/pam.d/cups67191422 4-1 root root 964 February 15 12:18 / etc/gshadow-67513217 4-r Murray- -1 root root 3938 June 7 2017 / etc/sudoers7, Find at least one class of files in the / etc directory for which the user does not have execute permission [root@localhost ~] # find / etc-not-perm-1111111-ls8, find / etc/init.d/ directory, all files for which all users have execute permissions, and other users have some permissions; [root@localhost ~] # find / etc/init.d/-perm-11311-ls

This is the end of the use of vim and shell scripts in linux. I hope the above content can be helpful to you and learn more. If you like this article, you might as well share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report