linux基礎正則表達式、shell基礎、文件查找和壓縮

linux基礎正則表達式、shell基礎、文件查找和壓縮
1.shell編程顯示電腦的基本信息,初級基礎腳本。只適合6.7版本的。

COLOR="\033[1;36m"
COLOREND="\033[0m"
echo -e "CPU type is $COLOR lscpu |grep 'Model name'|tr -s ' '|cut -d: -f2$COLOREND"
echo -e "Disk space is $COLOR lsblk |grep 'disk' |tr -s ' '|cut -d' ' -f4|head -n 1 $COLOREND"
echo -e "Memory size is $COLOR free -h |grep 'Mem'|tr -s ' '|cut -d' ' -f2 $COLOREND"
echo -e "OS Verion is $COLOR cat /etc/redhat-release $COLOREND"
echo -e "kernel version is $COLOR uname -r $COLOREND"
echo -e "IPaddr is $COLOR ifconfig ens33 |grep 'netmask'|tr -s ' '|cut -d' ' -f3$COLOREND"
2.找出ifconfig “網卡名” 命令結果中本機的IPv4地址

[root@centos7 scripts38]#ifconfig ens33 |grep netmask|tr -s ' '|cut -d' ' -f3
192.168.11.131
[root@centos7 ~]#ifconfig ens33 |grep ask | grep -o "([0-9]{1,3}.){3}[0-9]{1,3}"|head -1
192.168.6.136
3.查出分區空間使用率的最大百分比值

[root@centos7 scripts38]#df|grep '/dev/sd'|tr -s ' '|cut -d' ' -f5|tr -d %|sort -nr|head -n 1
17
4.查出用戶UID最大值的用戶名、UID及shell類型

[root@centos7 scripts38]#cat /etc/passwd |cut -d: -f1,3,7 |sort -t: -k2 -nr|head -n 1
nfsnobody:65534:/sbin/nologin
5.查出/tmp的權限,以數字方式顯示

[root@centos7 ~]#stat /tmp |grep 'drwx'|cut -d' ' -f2|tr -dc [0-9]
1777
6.統計當前連接本機的每個遠程主機IP的連接數,並按從大到小排序

netstat -tun|grep ESTAB|tr -s " " : |cut -d: -f6|sort -nr|uniq -c
1 192.168.6.1
7.顯示/proc/meminfo文件中以大小s開頭的行(要求:使用兩種方法)

[root@centos7 ~]#grep ^[sS] /proc/meminfo
SwapCached: 2288 kB
SwapTotal: 2097148 kB
SwapFree: 2058044 kB
Shmem: 16796 kB
Slab: 88744 kB
SReclaimable: 39980 kB
SUnreclaim: 48764 kB
[root@centos7 ~]#grep '^s|^S' /proc/meminfo
SwapCached: 2288 kB
SwapTotal: 2097148 kB
SwapFree: 2058044 kB
Shmem: 16796 kB
Slab: 88744 kB
SReclaimable: 39980 kB
SUnreclaim: 48764 kB
[root@centos7 ~]#grep ^[s|S] /proc/meminfo
SwapCached: 2288 kB
SwapTotal: 2097148 kB
SwapFree: 2058044 kB
Shmem: 16796 kB
Slab: 88744 kB
SReclaimable: 39980 kB
SUnreclaim: 48764 kB
[root@centos7 ~]#grep -w \<'s|S.*' /proc/meminfo
SwapCached: 2288 kB
SwapTotal: 2097148 kB
SwapFree: 2058044 kB
Shmem: 16796 kB
Slab: 88716 kB
SReclaimable: 39980 kB
SUnreclaim: 48736 kB
8.顯示/etc/passwd文件中不以/bin/bash結尾的行

[root@centos7 ~]#cat /etc/passwd |grep -v '/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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
9.顯示用戶rpc默認的shell程序

[root@centos7 ~]#cat /etc/passwd |grep '\<rpc\>'|cut -d: -f7
/sbin/nologin
10.找出/etc/passwd中的兩位或三位數

[root@centos7 ~]#cat /etc/passwd |grep "\<[0-9]{2,3}\>"
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:998:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
11.顯示CentOS7的/etc/grub2.cfg文件中,至少以一個空白字符開頭的且後面有非
空白字符的行

[root@centos7 ~]#cat /etc/grub2.cfg |grep ^[[:space:]].*
load_env
set default="${next_entry}"
set next_entry=
save_env next_entry
set boot_once=true
set default="${saved_entry}"
menuentry_id_option="--id"
menuentry_id_option=""
set saved_entry="${prev_saved_entry}"
save_env saved_entry
set prev_saved_entry=
save_env prev_saved_entry
set boot_once=true
if [ -z "${boot_once}" ]; then
saved_entry="${chosen}"
save_env saved_entry
fi
if [ x$feature_all_video_module = xy ]; then
insmod all_video
else
insmod efi_gop
insmod efi_uga
insmod ieee1275_fb
insmod vbe
insmod vga
insmod video_bochs
insmod video_cirrus
fi
set timeout_style=menu
set timeout=5
set timeout=5
source ${prefix}/user.cfg
if [ -n "${GRUB2_PASSWORD}" ]; then
set superusers="root"
export superusers
password_pbkdf2 root ${GRUB2_PASSWORD}
fi
load_video
set gfxpayload=keep
insmod gzio
insmod part_msdos
insmod xfs
set root='hd0,msdos1'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1' b6888a3b-3755-49af-b1db-5b412eda8657
else
search --no-floppy --fs-uuid --set=root b6888a3b-3755-49af-b1db-5b412eda8657
fi
linux16 /vmlinuz-3.10.0-957.el7.x86_64 root=UUID=05bdb265-31b6-4a0f-942b-f69d4b103e8d ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8
initrd16 /initramfs-3.10.0-957.el7.x86_64.img
load_video
insmod gzio
insmod part_msdos
insmod xfs
set root='hd0,msdos1'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1' b6888a3b-3755-49af-b1db-5b412eda8657
else
search --no-floppy --fs-uuid --set=root b6888a3b-3755-49af-b1db-5b412eda8657
fi
linux16 /vmlinuz-0-rescue-55bf685d782642d9bcbb1eaf123db175 root=UUID=05bdb265-31b6-4a0f-942b-f69d4b103e8d ro crashkernel=auto rhgb quiet
initrd16 /initramfs-0-rescue-55bf685d782642d9bcbb1eaf123db175.img
source ${config_directory}/custom.cfg
source $prefix/custom.cfg;
12.找出“netstat -tan”命令結果中以LISTEN後跟任意多個空白字符結尾的行

[root@centos7 ~]#netstat -tan|grep 'LISTEN[[:space:]]+'
tcp 0 0 0.0.0.0:111 0.0.0.0: LISTEN
tcp 0 0 0.0.0.0:6000 0.0.0.0:
LISTEN
tcp 0 0 192.168.122.1:53 0.0.0.0: LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:
LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0: LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:
LISTEN
tcp6 0 0 :::111 ::: LISTEN
tcp6 0 0 :::6000 :::
LISTEN
tcp6 0 0 :::22 ::: LISTEN
tcp6 0 0 ::1:631 :::
LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
13.顯示CentOS7上所有UID小於1000以內的用戶名和UID

[root@centos7 ~]#cat /etc/passwd |cut -d':' -f1,3 |grep -v '\<[0-9]{4}\>'
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
systemd-network:192
dbus:81
polkitd:999
libstoragemgmt:998
colord:997
rpc:32
gluster:996
saslauth:995
abrt:173
rtkit:172
pulse:171
radvd:75
rpcuser:29
nfsnobody:65534
unbound:994
chrony:993
qemu:107
tss:59
usbmuxd:113
geoclue:992
ntp:38
sssd:991
setroubleshoot:990
saned:989
gdm:42
gnome-initial-setup:988
sshd:74
avahi:70
postfix:89
tcpdump:72
[root@centos7 ~]#cat /etc/passwd |cut -d':' -f1,3 |grep '\<[0-9]{1,3}\>'
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
systemd-network:192
dbus:81
polkitd:999
libstoragemgmt:998
colord:997
rpc:32
gluster:996
saslauth:995
abrt:173
rtkit:172
pulse:171
radvd:75
rpcuser:29
unbound:994
chrony:993
qemu:107
tss:59
usbmuxd:113
geoclue:992
ntp:38
sssd:991
setroubleshoot:990
saned:989
gdm:42
gnome-initial-setup:988
sshd:74
avahi:70
postfix:89
tcpdump:72
14.添加用戶bash、testbash、basher、sh、nologin(其shell爲/sbin/nologin),找 出/etc/passwd用戶名和shell同名的行

[root@centos7 ~]#grep '(.+):.\<\1$' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nologin:x:1026:1032::/home/nologin:/sbin/nologin
[root@centos7 ~]#egrep "(.+):.\<\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nologin:x:1026:1032::/home/nologin:/sbin/nologin
15.利用df和grep,取出磁盤各分區利用率,並從大到小排序

[root@centos7 ~]#df |grep '/dev/sd'|tr -s ' '|cut -d' ' -f5 |sort -nr
17%
5%
1%
[root@centos7 ~]#df |grep '/dev/sd'|grep -o '[0-9]{0,3}%' |sort -nr
17%
5%
1%
16.查看Linux大版本。

[root@centos7 data]#cat /etc/redhat-release |cut -d. -f1|grep -o '[[:digit:]]'
7
17.顯示三個用戶root、mage、wang的UID和默認shell

[root@centos7 scripts38]#cat /etc/passwd |grep -w '^root|^wang|^mage'|cut -d: -f1,3,7
root:0:/bin/bash
wang:1027:/bin/bash
mage:1028:/bin/bash
18.找出/etc/rc.d/init.d/functions文件中行首爲某單詞(包括下劃線)後面跟一個小括號的行

[root@centos7 data]#cat /etc/rc.d/init.d/functions |grep '^[[:alpha:]].(.).*'
systemctl_redirect () {
checkpid() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
echo_success() {
echo_failure() {
echo_passed() {
echo_warning() {
update_boot_stage() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
is_ignored_file() {
convert2sec() {
is_true() {
is_false() {
apply_sysctl() {
strstr "$(cat /proc/cmdline)" "rc.debug" && set -x
19.使用egrep取出/etc/rc.d/init.d/functions中其基名和⽬錄名

目錄名
[root@centos7 data]#echo /etc/rc.d/init.d/functions |egrep -o "^[/].*/"
/etc/rc.d/init.d/
20.統計last命令中以root登錄的每個主機IP地址登錄次數

[root@centos7 data]#last |grep 'root' | egrep -o ([0-9]{1,3}.){3}[0-9]{1,3}|sort|uniq -c|sort -nr
70 192.168.6.1
1 172.18.37.37
21.利用擴展正則表達式分別表示0-9、10-99、100-199、200-249、250-255

[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]
22.顯示ifconfig命令結果中所有IPv4地址

[root@centos7 data]#ifconfig |egrep -o "(([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])"
192.168.6.136
255.255.255.0
192.168.6.255
172.18.7.7
255.255.0.0
172.18.255.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255
23.將此字符串:welcome to magedu linux 中的每個字符去重並排序,重複次數多的排到前面

[root@centos7 data]#echo welcome to magedu linux |grep -o "."|sort|uniq -c|sort -nr
3 e
3
2 u
2 o
2 m
2 l
1 x
1 w
1 t
1 n
1 i
1 g
1 d
1 c
1 a
24.編寫腳本 backup.sh,可實現每日將/etc/目錄備份到/backup/etcYYYYmm-dd中

COLOR="\033[1;36m"
COLOREND="\033[0m"
echo -e "${COLOR}Starting backup ...$COLOREND"
cp -av /etc/ /data/etc_backup-date +%F
echo -e "${COLOR}Backup is finished$COLOREND"
25.編寫腳本 disk.sh,顯示當前硬盤分區中空間利用率最大的值

COLOR="\033[1;36m"
COLOREND="\033[0m"
echo -e "${COLOR}Starting Hard Disk Inspection ...$COLOREND"
echo -e "${COLOR}df|grep "/dev/sd" |tr -s " "|cut -d ' ' -f5|sort -nr|head -1$COLOREND"
echo -e "${COLOR}Hard Disk Inspection is finished$COLOREND"
26.編寫腳本 links.sh,顯示正連接本主機的每個遠程主機的IPv4地址和連接數,並按連接數從大到小排序

COLOR="\033[1;36m"
COLOREND="\033[0m"
echo -e "${COLOR}Linsk ...$COLOREND"
echo -e "${COLOR}netstat -tun|grep ESTAB|tr -s " " : |cut -d: -f6|sort -nr|uniq -c$COLOREND"
echo -e "${COLOR}Linsk$COLOREND"
27.通用版信息存放地點

[root@centos7 data]#cat /proc/partitions
硬盤大小存放位置
[root@centos7 data]#cat /proc/meminfo
內存大小存放位置
[root@centos7 data]#cat /proc/cpuinfo
CPU等存放位置
28.遠程執行腳本。

先開啓服務,6上面自帶服務
[root@centos6 ~]#service httpd start
Starting httpd: httpd: apr_sockaddr_info_get() failed for centos6.10.localdomain
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]
把腳本拷貝到6的主機上
[root@centos7 scripts38]#scp 2systeminfo.sh 192.168.6.129:/var/www/html/
The authenticity of host '192.168.6.129 (192.168.6.129)' can't be established.
RSA key fingerprint is SHA256:7SNWeID7Jr5cvOTZkLQWElsa/MbwymmEUZRhTd8qxJ4.
RSA key fingerprint is MD5:3e:fa:86:dd:c6:b8:41:34:a0:50:f3:f6:b3:17:82:9a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.6.129' (RSA) to the list of known hosts.
[email protected]'s password:
2systeminfo.sh 100% 964 581.7KB/s 00:00
查看是否拷貝到了6的主機上
[root@centos6 ~]#ls /var/www/html/
2systeminfo.sh
在其他主機上執行就可以實現了
[root@centos7 scripts38]#curl 192.168.6.129/2systeminfo.sh |bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 100 964 100 964 0 0 335k 0 --:--:-- --:--:-- --:--:-- 470k
29.找出“ldd /usr/bin/cat”命令的結果中的⽂件路徑

[root@centos7 data]#ldd /usr/bin/cat | grep -o '/[^[:space:]]+'
/lib64/libc.so.6
/lib64/ld-linux-x86-64.so.2
30.複製/etc/profile⾄/tmp/⽬錄,⽤查找替換命令刪除/tmp/profile⽂件中的⾏⾸的空⽩字符

[root@centos7 data]#cp /etc/profile /tmp/
[root@centos7 data]#vim /tmp/profile
:%s/^[[:space:]]+//g
31.複製/etc/rc.d/init.d/functions⽂件⾄/tmp⽬錄,⽤查找替換命令爲/tmp/functions的每⾏開頭爲空⽩字符的⾏的⾏⾸添加⼀個#號

[root@centos7 data]#cp /etc/rc.d/init.d/functions /tmp/
[root@centos7 data]#vim /tmp/functions
:%s/^[[:space:]]/#/g
31.編寫腳本 sumid.sh,計算/etc/passwd文件中的第10個用戶和第20用戶的UID之和

NUM1=cat /etc/passwd |head -1 |cut -d: -f3
NUM10=cat /etc/passwd |head -10 |tail -1|cut -d: -f3
SUM=$[NUM1+NUM10]
echo "num1 user UUID + num10 user UUID = $SUM"
32.編寫腳本 sumspace.sh,傳遞兩個文件路徑作爲參數給腳本,計算這兩個文件中所有空白行之和

NUM1=grep '^$' $1 |wc -l
NUM2=grep '^$' $2 |wc -l
SUM=$[NUM1+NUM2]
echo "The sum of blank lines =$SUM"
33.編寫腳本 sumfile.sh,統計/etc, /var, /usr 目錄中共有多少個一級子目錄和文件

ETC=ls -l /etc/ | wc -l
VAR=ls -l /var/ | wc -l
USR=ls -l /usr/ | wc -l
SUM=$[ETC+VAR+USR]
echo "sum=$SUM"
34.編寫腳本 argsnum.sh,接受一個文件路徑作爲參數;如果參數個數小於1,
則提示用戶“至少應該給一個參數”,並立即退出;如果參數個數不小於1,則顯示第一個參數所指向的文件中的空白行數

[ $# -lt 1 ] && { echo "At least one parameter should be given"; exit 10; }
NUM1=grep '^$' $1 |wc -l
echo "The blank lines =$NUM1"
35.編寫腳本 hostping.sh,接受一個主機的IPv4地址做爲參數,測試是否可連
通。如果能ping通,則提示用戶“該IP地址可訪問”;如果不可ping通,則提示用戶“該IP地址不可訪問”

. /data/scripts38/color.sh
read -p "Please enter a correct IP:" IP

[[ $# -lt 1 ]] && { echo "Please enter a IP"; exit 10; }

[[ "$IP" =~ ^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]] || { echo -e "${COLOR} Please enter a IP${COLOREND}" ; exit 20;}
ping -c1 -w2 $IP &> /dev/null && echo -e "${COLOR}該IP地址可訪問${COLOREND}" ||echo -e "${COLOR}該IP地址不可訪問${COLOREND}"
36.編寫腳本 checkdisk.sh,檢查磁盤分區空間和inode使用率,如果超過80%,就發廣播警告空間將滿

USE=df|grep "/dev/sd" |tr -s " " %|cut -d "%" -f5|sort -nr|head -1
INODE=df -i|grep "/dev/sd" |tr -s " " %|cut -d "%" -f5|sort -nr|head -1
test $USE -gt 80 && echo " disk will be full " &&echo disk will be full | mail -s warning root || echo "disk is normal"
[[ "$INODE" -gt 80 ]] && echo " inode will be full " &&echo inode will be full | mail -s warning root || echo "inode is normal"
unset USE INODE
37.編寫腳本 per.sh,判斷當前用戶對指定參數文件,是否不可讀並且不可寫

. /data/scripts38/color.sh
#read -p "Please enter a file:" FILE
[[ $# -lt 1 ]] && { echo -e "${COLOR}No input file${COLOREND}" ; exit 10;}
[[ ! -e $@ ]] && { echo -e "${COLOR}file does not exist${COLOREND}" ; exit 20;}
[ ! -r $@ -a ! -w $@ ] && { echo -e "${COLOR}The file is unreadable and not writable${COLOREND}" ;}
[ -r $@ -a ! -w $@ ] && { echo -e "${COLOR}The file is readable and not writable${COLOREND}" ;}
[ -r $@ -a -w $@ ] && { echo -e "${COLOR}The file is readable and writable${COLOREND}" ;}
38.編寫腳本 excute.sh ,判斷參數文件是否爲sh後綴的普通文件,如果是,添加所有人可執行權限,否則提示用戶非腳本文件

. /data/scripts38/color.sh
[[ $# -lt 1 ]] && { echo -e "${COLOR}No input file${COLOREND}" ; exit 10;}
[[ ! -e $@ ]] && { echo -e "${COLOR}file does not exist${COLOREND}" ; exit 20;}
[ ! -f $@ ] && { echo -e "${COLOR}Not an ordinary document${COLOREND}" ; exit 30;}
[[ $@ =~ .sh$ ]] && (chmod a+x $@ ; echo -e "${COLOR}$@是一個腳本文件${COLOREND}" ) || { echo -e "${COLOR}User Non-script Files${COLOREND}" ; exit 40;}
39.編寫腳本 nologin.sh和 login.sh,實現禁止和允許普通用戶登錄系統

. /data/scripts38/color.sh
[ -f /etc/nologin ] && echo -e "${COLOR}Normal users are prohibited from logging in${COLOREND}"|| (touch /etc/nologin ; echo -e "${COLOR}Normal users are prohibited from logging in${COLOREND}")
. /data/scripts38/color.sh
[ -f /etc/nologin ] && (rm -f /etc/nologin ; echo -e "${COLOR}Ordinary users can log in${COLOREND}")||echo -e "${COLOR}Ordinary users can log in${COLOREND}"
40.年齡

read -p "Input your age: " AGE
if [[ ! "$AGE" =~ ^[0-9]+$ ]]; then
echo "Please input a digit age"
elif [ $AGE -lt 18 ];then
echo "GOOD good study"
elif [ $AGE -lt 80 ];then
echo "goot goot work"
elif [ $AGE -le 120 ];then
echo "enjoy life"
else
echo "Welcome to the earth"
fi
41.雞兔同籠,頭37,腳94

read -p "please input the head number:" HEAD
read -p "please input the foot number:" FOOT
Rabbit=$[FOOT/2-HEAD]
Chook=$[HEAD-Rabbit]
echo "Number of rabbits:" $Rabbit
echo "Number of chook:" $Chook
把ens33改爲eth0

[root@centos7 ~]#sed -ri '/^[[:space:]]+linux16/s#(.*)#\1 net.ifnames=0#' /boot/grub2/grub.cfg
[root@centos7 ~]#sed -ir '/GRUB_CMDLINE_LINUX/s@"$@ net.ifnames=0"@' /etc/default/grub
關閉selinux

[root@centos7 data]#sed -i 's/SELINUX=enforcing/SELINUX=disabled/ ' /etc/selinux/config
42.

cat <<EOF
1.不知道想幹啥
2.幹什麼都不容易
3.乾點什麼不好
4.吃飯飯
5.睡覺覺
6.打豆豆
EOF
read -p "抽獎啦啦,輸入一個數字(1-6)):" NUM
case $NUM in
1)
echo "幹什麼都不如睡覺"
;;
2)
echo "夢裏啥都有,要什麼自行車"
;;
3)
echo "吃飯,睡覺,打豆豆,一天又一天"
;;
4)
echo "少吃點,該減肥了"
;;
5)
echo "一睡就是一整天"
;;
6)
echo "打什麼豆豆"
;;
*)
echo "歡迎來地球"
esac
43.編寫腳本createuser.sh,實現如下功能:使⽤⼀個⽤戶名做爲參數,如果指定參數的⽤戶存在,就顯⽰其存在,否則添加之;顯⽰添加的⽤戶的id號等信息

if [ -z "$1" ];then
read -p "please input user:" USER
if
[ -z $USER ] ;then
echo "Password cannot be empty"
exit 10;
else
NAME=$USER
fi
else
NAME=$USER
fi

if id $NAME &> /dev/null ; then
echo "$NAME 賬戶已經存在"
exit 20
else
useradd $NAME && echo "密碼創建成功"
echo magedu | passwd --stdin $NAME &> /dev/null && echo "初始密碼爲:magedu"
passwd -e $NAME &> /dev/null
echo "userinfo:id $NAME"
fi
44.編寫腳本/root/bin/yesorno.sh,提⽰⽤戶輸⼊yes或no,並判斷⽤戶輸⼊的是yes還是no,或是其它信息

read -p "Do you agree? (yes or no): " ANSWER
case $ANSWER in
[Yy]|[Yy][Ee][sS])
echo YES
;;
[Nn]|[Nn][Oo])
echo NO
;;
*)
echo input false
esac
45.編寫腳本/root/bin/filetype.sh,判斷⽤戶輸⼊⽂件路徑,顯⽰其⽂件類型(普通,⽬錄,鏈接,其它⽂件類型)

read -p "輸入一個文件:" A
if [ -z $A ];then
echo "輸入不能爲空"
exit
elif [ ! -e $A ];then
echo "文件不存在"
exit
elif [ -d $A ];then
echo "這是一個目錄"
exit
elif [ -L $A ];then
echo "這是一個鏈接文件"
exit
elif [ -f $A ];then
echo "這是一個普通文件"
exit
else
echo "other file"
exit

fi
46.編寫腳本/root/bin/checkint.sh,判斷⽤戶輸⼊的參數是否爲正整數

. /data/scripts38/color.sh
read -t 30 -p "please input a number:" NUM
if [ -z $NUM ] ; then
echo -e "${COLOR}輸入不能爲空${COLOREND}"
exit 10
elif [[ "$NUM" =~ ^[0-9]+$ ]] ;then
echo "這是一個正整數"
exit 20
else
echo "這不是一個正整數"
exit 30
fi
47.讓所有⽤戶的PATH環境變量的值多出⼀個路徑,例如:/usr/local/apache/bin

echo 'PATH=/usr/local/apache/bin:$PATH' >> /etc/profile.d/env.sh
source /etc/profile.d/env.sh
48.⽤戶root登錄時,將命令指⽰符變成紅⾊,並⾃動啓⽤如下別名:
rm=‘rm –i’
cdnet=‘cd /etc/sysconfig/network-scripts/’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eth0’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 或
ifcfg-ens33 ’ (如果系統是CentOS7)

echo "PS1='[\e[1;31m][\u@\h \W]\$[\e[0m]'" >> /etc/profile.d/env.sh
source /etc/profile.d/env.sh
echo "alias rm='rm –i'" >> ~/.bashrc
echo "alias cdnet='cd /etc/sysconfig/network-scripts/'" >> ~/.bashrc
echo "alias editnet1='vim /etc/sysconfig/network-scripts/ifcfg-eth0'" >> ~/.bashrc
echo "alias editnet2='vim /etc/sysconfig/network-scripts/ifcfg-ens33'" >> ~/.bashrc
source .bashrc
49.查找/var⽬錄下屬主爲root,且屬組爲mail的所有⽂件。

[root@centos7 data]#find /var/ -user root -group mail
/var/spool/mail
/var/spool/mail/root
50.查找/var⽬錄下不屬於root、 lp、 gdm的所有⽂件。

[root@centos7 data]#find /var/ -not ( -user root -o -user lp -o -user gdm )
/var/tmp/abrt
/var/lib/colord
/var/lib/colord/icc
51.查找當前系統上沒有屬主或屬組,且最近⼀個周內曾被訪問過的⽂件。

[root@centos7 data]#find /var -atime -7 -nouser -o -nogroup
52.查找/var⽬錄下最近⼀周內其內容修改過,同時屬主不root,也不是 postfix的⽂件。

[root@centos7 data]#find /var -mtime -7 -not ( -user root -o -group postfix )
/var/log/cups
/var/spool/mail/test
/var/spool/mail/bash
/var/spool/mail/testbash
/var/spool/mail/basher
/var/spool/mail/sh
/var/spool/mail/nologin
/var/spool/mail/wang
/var/spool/mail/mage
/var/spool/mail/lll
/var/spool/mail/aaa
/var/spool/mail/aaaaa
/var/spool/mail/jiaoyu
/var/spool/mail/chen
/var/spool/postfix/active
/var/spool/postfix/incoming
/var/spool/postfix/maildrop
/var/spool/postfix/private
/var/spool/postfix/public
53.查找/etc⽬錄下⼤於1M且類型爲普通⽂件的所有⽂件。

[root@centos7 data]#find /etc/ -size +1M -type f
/etc/udev/hwdb.bin
/etc/selinux/targeted/active/policy.kern
/etc/selinux/targeted/contexts/files/file_contexts.bin
/etc/selinux/targeted/policy/policy.31
/etc/brltty/zh-tw.ctb
54.查找/etc⽬錄下所有⽤戶都沒有寫權限的⽂件。

[root@centos7 data]#find /etc/ -not -perm /222
/etc/pki/ca-trust/extracted/java/cacerts
/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
/etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem
/etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem
/etc/udev/hwdb.bin
/etc/openldap/certs/password
/etc/gshadow
/etc/shadow
/etc/gshadow-
/etc/ld.so.conf.d/kernel-3.10.0-957.el7.x86_64.conf
/etc/lvm/profile/cache-mq.profile
/etc/lvm/profile/cache-smq.profile
/etc/lvm/profile/command_profile_template.profile
/etc/lvm/profile/lvmdbusd.profile
/etc/lvm/profile/metadata_profile_template.profile
/etc/lvm/profile/thin-generic.profile
/etc/lvm/profile/thin-performance.profile
/etc/dbus-1/system.d/cups.conf
/etc/pam.d/cups
/etc/shadow-
/etc/machine-id
/etc/sudoers
55.查找/etc⽬錄下⾄少有⼀類⽤戶沒有執⾏權限的⽂件。

[root@centos7 data]#find /etc/ -not -perm -111
56.查找/etc/init.d⽬錄下,所有⽤戶都有執⾏權限,且其它用戶有寫權限的⽂件。

[root@centos7 data]#find /etc/init.d/ -perm -113
57.私人配置腳本,還是未完成版,還在完善中

export PS1="[\e[1;32m][\u@\h \W]\$[\e[0m]"
export PATH=/app/bin:$PATH
export EDITOR=vim
export HISTTIMEFORMAT="%F %T "
export HISTCONTROL=ignoreboth
EOF
cat > ~/.vimrc << EOF
set ignorecase
set cursorline
set autoindent
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
if expand("%:e") == 'sh'
call setline(1,"#!/bin/bash")
call setline(2,"#")
call setline(3,"#****")
call setline(4,"#Author: zhangfeng")
call setline(5,"#QQ: 553645084")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName: ".expand("%"))
call setline(8,"#URL: https://blog.51cto.com/14451122")
call setline(9,"#Description: The test script")
call setline(10,"#Copyright (C): ".strftime("%Y")." All rights reserved")
call setline(11,"#****")
call setline(12,"")
endif
endfunc
autocmd BufNewFile normal G
EOF
#關閉selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/ ' /etc/selinux/config
#把ens33改爲eth0
sed -ri '/^[[:space:]]+linux16/s#(.
)#\1 net.ifnames=0#' /boot/grub2/grub.cfg
#7版本關閉防火牆
systemctl disable firewalld.service
#6版本關閉防火牆
chkconfig iptables off

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章