Linux phase exercises (2)

Source: Internet
Author: User
Tags egrep

67. Display user name and UID of all system users on CentOS7

# cat/etc/passwd |grep '. *:x:\<[2-9][0-9][1-9]\>.* '

68, add User Bash, Testbash, basher, SH, nologin (its shell is/sbin/nologin), find the/etc/passwd user name and shell the same name row

# cat/etc/passwd |grep-e ' ^ (. *): x.*/\1$ ' Back to referenced \1$ has been able to determine the number of characters ^ (. *)

69, take advantage of DF and grep, remove the disk partition utilization, and from large to small sort

# df |egrep-o ' 1? [0-9]? [0-9]% ' |sort-nr

70. Display three user root, Mage, Wang's UID and default shell

# cat/etc/passwd |grep-e ' ^ (root|wang|mage) ' |cut-d:-f3,7

71. Find the line at the beginning of the/etc/rc.d/init.d/functions file that has a word (including an underscore) followed by a parenthesis

# cat/etc/rc.d/init.d/functions |grep-e ' ^ ([[: alpha:]]+|_+) [A-za-z0-9_]*\ ('

72. Use Egrep to remove its base name in/etc/rc.d/init.d/functions

# echo/etc/rc.d/init.d/functions |grep-eo ' [[: alpha:]]+$ ' (note-V is the selection of unmatched rows and cannot be taken in the same row that matches)

73. Use Egrep to remove the directory name of the path above

# echo/etc/rc.d/init.d/functions |grep-eo '. * *

74. Count the number of logins per host IP address that was logged in as root in the last command

# Last |grep-eo ' ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \.) {3} ([0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) ' |sort |uniq-c

75. Use extended regular expressions to represent 0-9, 10-99, 100-199, 200-249, 250-255, respectively

# [0-9]

# [1-9][0-9]

# 1[0-9][0-9]

# 2[0-4][0-9]

# 25[0-5]

76. Display all IPV4 addresses in ifconfig command results

# Ifconfig | Grep-eo ' ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \.) {3} ([0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) '

77, this string: Welcome to magedu each character in Linux to go to the weight and sort, repeat the number of rows to the front

# echo "Welcome to magedu Linux" |grep-o ' | Grep-o ' [[: Alpha:]] ' |sort |UNIQ-C|SORT-NR

78. Copy the/etc/profile to/tmp/directory and use the Find replacement command to remove the white space characters from the beginning of the/tmp/profile file

#:%s/^ +//(:%s/^[[:space:]]+//)

79. Copy the/etc/rc.d/init.d/functions file to the/tmp directory, and add a # number to the beginning of the line beginning with a blank character for each line of/tmp/functions with the Find replacement command

#:%s/^[[:space:]]/#&

80. Set tab indent to 4 characters in Vim

# set Ts=4

81. Copy the/etc/rc.d/init.d/functions file to the/tmp directory and replace the/etc/sysconfig/init in the/tmp/functions file with the/var/log

#:%[email protected]/etc/sysconfig/[email Protected]/var/[email protected]

82. Delete the # number at the beginning of all lines in the/tmp/functions file that begin with # and have at least one blank character after #

#:%s/^#\ (\+\)/\1/

83, write script/root/bin/systeminfo.sh, display the current host system information, including hostname, IPV4 address, operating system version, kernel version, CPU model, memory size, hard disk size

#!/bin/bash

Echo-e "\e[1;31mthe hostname:\e[0m ' hostname '"

Echo-e "\e[1;31mthe System IP address:\e[0m ' ifconfig ens33|head-2 |tail-1|tr-s ' |cut-d '"

Echo-e "\e[1;31mcurrent System version:\e[0m ' cat/etc/centos-release '"

Echo-e "\e[1;31mkernel version:\e[0m ' uname-r '"

Echo-e "\e[1;31mthe Mode of cpu:\e[0m ' lscpu |grep ' Model name ' |tr-s ' |cut-d:-f2 '"

ECHO-E "\e[1;31mthe total space of memory:\e[0m ' Cat/proc/meminfo |grep ' memtotal ' |tr-s ' |cut-d:-f2 '"

ECHO-E "\e[1;31mthe total space of disk:\e[0m ' lsblk |grep-w" SDA "|tr-s": ' |cut-d:-f5 ' "

84, Scripting/root/bin/backup.sh, can be implemented daily/etc/directory backup to/root/etcyyyy-mm-dd

#!/bin/bash

Echo-e "\e[1;31m Backup is starting ... \e[0m"

[-d/root/etc ' Date +%f '] && {echo "Backup dir has been exist"; Read-p "does you want to overwrite? Yes or No: "Key;}

if [[$key =~ [yy]+ ([Ee][ss])]; Then

Rm-rf/root/etc ' Date +%f '

Cp-av/etc/root/etc ' Date +%f '

elif [[$key =~ [Nn][oo]]; Then

ran_num= $RANDOM

Read-p "Backup name can be added a random number $ran _num, Yes or No:" Key1

if [[$key 1 =~ [YY][EE][SS]]; Then

Cp-av/etc/root/etc ' Date +%f ' $ran _num

echo "Backup with a name has been finished."

Else

Exit

Fi

Fi

Cp-av/etc/root/etc ' Date +%f '

Echo-e "\e[1;31m Backup has been finished.\e[0m"

85. Write script/root/bin/disk.sh, showing the maximum space utilization value in the current hard disk partition

#!/bin/bash

Echo-e "\e[1;31mthe The biggest partition usage: \e[0m ' df |grep '/dev/sd\+ ' |tr-s ': ' |cut-d:-f5 |sort-nr |head- 1 ' "

86. Write Script/root/bin/links.sh, show the IPV4 address and connection number of each remote host connecting to this host, and sort by the number of connections from large to small

#!/bin/bash

Netstat-nt |grep ' established ' |tr-s ': ' |cut-d:-f6|sort |uniq-c | Sort-nr

87. Write script/root/bin/sumid.sh, calculate the sum of the ID of the 10th user and the 20th user in the/etc/passwd file

#!/bin/bash

Uid10= ' cat/etc/passwd| head-10|tail-1|cut-d:-f3 '

Uid20= ' cat/etc/passwd| head-20|tail-1|cut-d:-f3 '

echo "SUM=$[UID10+UID20]"

88, write the script/root/bin/sumspace.sh, pass two file path as parameters to the script, calculate the sum of all the blank lines in the two files

#!/bin/bash

File1=$1

File2=$2

file1_blanks= ' grep ' ^[[:space:]]*$ ' ${file1} |wc-l '

file2_blanks= ' grep ' ^[[:space:]]*$ ' ${file2} |wc-l '

Echo-e "\e[1;31mtotal blanks:\e[0m $[file1_blanks+file2_blanks]"

89, Scripting/root/bin/sumfile.sh, Statistics/etc,/var,/usr directory total number of sub-directories and files

#!/bin/bash

etc_dirs= ' ls-al/etc/|grep ' ^d ' |wc-l '

usr_dirs= ' ls-al/usr/|grep ' ^d ' |wc-l '

var_dirs= ' ls-al/var/|grep ' ^d ' |wc-l '

etc_files= ' ls-al/etc/|grep ' ^-' |wc-l '

usr_files= ' ls-al/usr/|grep ' ^-' |wc-l '

var_files= ' ls-al/var/|grep ' ^-' |wc-l '

echo "etc_dirs= $etc _dirs"

echo "usr_dirs= $usr _dirs"

echo "var_dirs= $var _dirs"

echo "etc_files= $etc _files"

echo "usr_files= $usr _files"

echo "var_files= $var _files"

90, write script/root/bin/argsnum.sh, accept a file path as a parameter, if the number of parameters is less than 1, the user is prompted "at least one parameter should be given" and immediately exit, if the number of parameters is not less than 1, the number of blank lines in the file pointed to by the first parameter is displayed

#!/bin/bash

["$#"-lt 1] && echo "need a parameter" && exit

["$#"-ge 1] && echo "Total blanks: ' grep ' ^[[:space:]]*$ ' $1|wc-l '"

91, write script/root/bin/hostping.sh, accept the IPV4 address of a host as parameter, test whether can connect. If Ping is available, the user is prompted to "the IP address is accessible" and if it is not ping, the user is prompted "The IP address is inaccessible"

#!/bin/bash

Ping-c 1 &>/dev/null && echo "Host Up" | | echo "Host down"

92, write script/root/bin/checkdisk.sh, check the disk partition space and inode utilization, if more than 80%, the broadcast warning space will be full

#!/bin/bash

Inode_usage= ' df-i|tr-s ': ' |cut-d:-f5 |sort-nr |head-1 |grep-eo ' [[:d igit:]]{1,3} '

partition_usag= ' df |grep '/dev/sda* ' |tr-s ': ' |cut-d:-f5|sort-nr |head-1|grep-eo ' [[:d igit:]]{1,3} '

[$inode _usage-gt 80-o $partition _usag-gt] && ' Wall "warning ..."

93, write the script/bin/per.sh, determine whether the current user to the specified parameter file, is not readable and not writable

#!/bin/bash

[!-R $1-a!-W] && echo "The file can ' t read and write" | | echo "The file can read an write"

94, write script/root/bin/excute.sh, determine whether the parameter file is the sh suffix of the ordinary file, if it is, add everyone can execute permissions, otherwise prompt the user non-script file

#!/bin/bash

[[$ =~. *\.sh$]] && {echo ". sh"; chmod +x $;} | | echo "No. sh"

95, write scripts/root/bin/nologin.sh and login.sh, implement prohibit and allow ordinary user login system

#!/bin/bash

users= ' Cat/etc/passwd|grep-ew ' [[:d igit:]]{4} ' |cut-d:-f1 '

For i in $users; Do

Usermod-s/sbin/nologin $i

Done


#!/bin/bash

users= ' Cat/etc/passwd|grep-ew ' [[:d igit:]]{4} ' |cut-d:-f1 '

For i in $users; Do

Usermod-s/bin/bash $i

Done


Linux phase exercises (2)

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.