Linux-practice (23-24)

Source: Internet
Author: User
Tags memory usage uuid egrep

23. Grep/egrep Regular Expression exercises:

1) display lines in the/etc/passwd file that do not end with bash;

For:

# grep-v "bash$"/etc/passwd

2) Locate the two-bit or three-bit integer in the result of the ifconfig command execution;

For:

# Ifconfig | grep--color=auto "\<\ ([[:d igit:]]\{2,3\}\) \>"//00

# Ifconfig | grep--color=auto "\<\ ([1-9][0-9]\|[ 1-9][0-9][0-9]\) \> "//Perfect


3) Find/etc/grub2.cfg (in the CentOS6 is/etc/grub.conf) file, a non-blank line beginning with a blank character;

For:

# Cat/etc/grub.conf | grep "^[[:space:]]\+.*[^[:space:]]$"//CENTOS6

# grep "^[[:space]]\+[^[:space:]]\+"/etc/grub2.cfg//centos7

Default=0

Sets the default boot menu item, and all the title in the entire menu is numbered starting from 0;

Timeout=5

When the user does not make a selection, Grub automatically boots the default menu corresponding to the kernel's timeout period;

Splashimage= (hd0,0)/grub/splash.xpm.gz

The path of the grub background image;

Hiddenmenu

Hide Grub's boot menu

Password--md5 Crypted_password

To protect the password set by the menu, it is often used to prevent random entry into single-user mode;

Title CentOS 6 (2.6.32-573.el6.x86_64)

Define each title in a menu item to define multiple title items;

Root (hd0,0)

Specifies the root device of grub, usually the partition that is used to install grub;

kernel/vmlinuz-2.6.32-573.el6.x86_64 ro root=uuid=5ebb0e76-bb19-4a80-9c70-0d101c0778e1 nomodeset Rd_NO_LUKS KEYBOARDTYPE=PC keytable=us rd_no_md Crashkernel=auto lang=zh_cn. UTF-8 RD_NO_LVM rd_no_dm RHGB quiet

The menu corresponds to the kernel to be launched and the parameters passed to the kernel;

Initrd/initramfs-2.6.32-573.el6.x86_64.img

RAMDisk (RAMFS) file corresponding to the kernel

Password--md5 Crypted_password

Protect the boot of the operating system kernel;

4) Find the result of "Netstat-tan" command, the line with "LISTEN" followed by any whitespace character;

For:

# Netstat-tan | grep ". *listen[[:space:]]*$"

# Netstat-tan | grep "listen[[:space:]]*$"


5) Find the result of the "fdisk-l" command, including the line with/dev/followed by SD or HD and a lowercase letter;

For:

# Fdisk-l | grep "^/dev/[s|h]d[a-z]"


6) Find all the file paths in the results of the "ldd/usr/bin/vim" command;

For:

# Ldd/usr/bin/vim | Grep-o "/[^[:space:]]\+"

# Ldd/usr/bin/vim | Grep-o "/.*/.*"


7) Find out all the lines in the/proc/meminfo file that start with uppercase s or lowercase s, how many ways do you implement the task?

For:

# Cat/proc//meminfo | grep "^[ss].*"//Memory usage

# Cat/proc//meminfo | Grep-e "^ (s|s). *"

# Cat/proc//meminfo | Grep-i "^s"//Ignore Case

# Cat/proc/meminfo | grep "^s\|^s"//note explanation output "or"

# Cat/proc/meminfo | Grep-e "^s"-E "^s"//Multiple matches


8) Use Egrep to remove the base name of a path;

For:

# Ls/usr/bin/ldd | Grep-o "/[a-z]\+$"


9) displays an integer between 0-255 in the result of the ifconfig command;

For:

# Ifconfig | grep--color=auto "\<\ ([0-9]\|[ 1-9][0-9]\|1[0-9][0-9]\|2[0-4][0-9]\|25[0-5]\) \> "


10) Add User bash, Testbash, basher, Nologin, Unlogin, logining, the default shell for the first three users is/bin/bash, the default shell for the last three users is/sbin/nologin, And then find the user name and shell name of the users of the same information;

For:

# useradd-s/bin/bash Bash

# useradd-s/bin/bash basher

# useradd-s/bin/bash Testbash

# useradd-s/sbin/nologin Nologin

# useradd-s/sbin/nologin Unlogin

# useradd-s/sbin/nologin logining//Create multi-user available scripts to complete

# CAT/ETC/PASSWD | grep "^\<\ ([[: alnum:]]\+\) \>.*\1$"


Cases:

[Email protected] ~]# grep-v "bash$"/etc/passwd

Bin:x:1:1:bin:/bin:/sbin/nologin

Daemon:x:2:2:daemon:/sbin:/sbin/nologin

......

[Email protected] ~]# Ifconfig | grep--color=auto "\<\ ([[:d igit:]]\{2,3\}\) \>"

Eth0 Link encap:ethernet HWaddr 00:0c:29:ef:91:5e

inet addr:172.16.67.2 bcast:172.16.255.255 mask:255.255.0.0

Inet6 ADDR:FE80::20C:29FF:FEEF:915E/64 Scope:link

RX bytes:638912 (623.9 KiB) TX bytes:587812 (574.0 KiB)

eth1 Link encap:ethernet HWaddr 00:0c:29:ef:91:68

inet addr:192.168.222.136 bcast:192.168.222.255 mask:255.255.255.0

Inet6 ADDR:FE80::20C:29FF:FEEF:9168/64 Scope:link

TX packets:11 errors:0 dropped:0 overruns:0 carrier:0

RX bytes:112446 (109.8 KiB) TX bytes:1334 (1.3 KiB)

Eth2 Link encap:ethernet HWaddr 00:0c:29:ef:91:72

inet addr:192.168.222.137 bcast:192.168.222.255 mask:255.255.255.0

Inet6 ADDR:FE80::20C:29FF:FEEF:9172/64 Scope:link

TX packets:175 errors:0 dropped:0 overruns:0 carrier:0

RX bytes:120635 (117.8 KiB) TX bytes:17648 (17.2 KiB)

inet addr:127.0.0.1 mask:255.0.0.0

Inet6 addr::: 1/128 scope:host

RX packets:46 errors:0 dropped:0 overruns:0 frame:0

TX packets:46 errors:0 dropped:0 overruns:0 carrier:0

[Email protected] ~]#

[Email protected] ~]# cat/etc/grub.conf

# grub.conf generated by Anaconda

#

# Note that you don't have the to rerun grub after making changes to the This file

# notice:you has a/boot partition. This means

# all kernel and INITRD paths is relative to/boot/, eg.

# root (hd0,0)

# kernel/vmlinuz-version RO root=/dev/sda2

# initrd/initrd-[generic-]version.img

#boot =/dev/sda1

Default=0

Timeout=5

Splashimage= (hd0,0)/grub/splash.xpm.gz

Hiddenmenu

Title CentOS 6 (2.6.32-573.el6.x86_64)

Root (hd0,0)

kernel/vmlinuz-2.6.32-573.el6.x86_64 ro root=uuid=1e8379ed-1b71-4c70-b97a-4d2121654719 nomodeset Rd_NO_LUKS KEYBOARDTYPE=PC keytable=us rd_no_md Crashkernel=auto lang=zh_cn. UTF-8 RD_NO_LVM rd_no_dm RHGB quiet

Initrd/initramfs-2.6.32-573.el6.x86_64.img

[Email protected] ~]#

[Email protected] ~]# cat/etc/grub.conf | grep "^[[:space:]]\+.*[^[:space:]]$"

Root (hd0,0)

kernel/vmlinuz-2.6.32-573.el6.x86_64 ro root=uuid=1e8379ed-1b71-4c70-b97a-4d2121654719 nomodeset Rd_NO_LUKS KEYBOARDTYPE=PC keytable=us rd_no_md Crashkernel=auto lang=zh_cn. UTF-8 RD_NO_LVM rd_no_dm RHGB quiet

Initrd/initramfs-2.6.32-573.el6.x86_64.img

[Email protected] ~]#

[Email protected] ~]# Netstat-tan | grep ". *listen[[:space:]]*$"

TCP 0 0 0.0.0.0:36239 0.0.0.0:* LISTEN

TCP 0 0 0.0.0.0:111 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

TCP 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN

TCP 0 0::: 111:::* LISTEN

TCP 0 0::: +:::* LISTEN

TCP 0 0::: 33813:::* LISTEN

TCP 0 0::: $:::* LISTEN

TCP 0 0:: 1:631:::* LISTEN

TCP 0 0:: 1:25:::* LISTEN

TCP 0 0:: 1:6010:::* LISTEN

[Email protected] ~]#

[Email protected] ~]# Fdisk-l | grep "^/dev/[s|h]d[a-z]"

/DEV/SDA1 * 1 204800 Linux

/dev/sda2 6553 52428800 Linux

/dev/sda3 6553 6814 2097152, Linux Swap/solaris

[Email protected] ~]#

[Email protected] ~]# Ldd/usr/bin/vim | Grep-o "/.*/.*"

/lib64/libm.so.6 (0x0000003634800000)

/lib64/libselinux.so.1 (0x0000003635400000)

/lib64/libtinfo.so.5 (0x000000363d000000)

/lib64/libacl.so.1 (0x0000003c76800000)

/usr/lib64/libgpm.so.2 (0x0000003634c00000)

/usr/lib64/perl5/core/libperl.so (0x0000003634400000)

/lib64/libdl.so.2 (0x0000003633800000)

/lib64/libpthread.so.0 (0x0000003634000000)

/lib64/libc.so.6 (0x0000003633c00000)

/lib64/ld-linux-x86-64.so.2 (0x0000003633400000)

/lib64/libattr.so.1 (0x0000003643800000)

/lib64/libresolv.so.2 (0x0000003635800000)

/lib64/libnsl.so.1 (0x0000003644c00000)

/lib64/libcrypt.so.1 (0x000000363f800000)

/lib64/libutil.so.1 (0x0000003643c00000)

/lib64/libfreebl3.so (0x000000363f400000)

[Email protected] ~]#

[Email protected] ~]# Ldd/usr/bin/vim | Grep-o "/[^[:space:]]\+"

/lib64/libm.so.6

/lib64/libselinux.so.1

/lib64/libtinfo.so.5

/lib64/libacl.so.1

/usr/lib64/libgpm.so.2

/usr/lib64/perl5/core/libperl.so

/lib64/libdl.so.2

/lib64/libpthread.so.0

/lib64/libc.so.6

/lib64/ld-linux-x86-64.so.2

/lib64/libattr.so.1

/lib64/libresolv.so.2

/lib64/libnsl.so.1

/lib64/libcrypt.so.1

/lib64/libutil.so.1

/lib64/libfreebl3.so

[Email protected] ~]#

[Email protected] ~]# Ldd/usr/bin/vim | Grep-o "/[^[:space:]]\+" | Wc-l

16

[Email protected] ~]# Ldd/usr/bin/vim | Grep-o "/.*/.*" | Wc-l

16

[Email protected] ~]# Cat/proc//meminfo | grep "^[s|s].*"

swapcached:48 KB

swaptotal:2097148 KB

swapfree:2094960 KB

shmem:3136 KB

slab:85056 KB

sreclaimable:15576 KB

sunreclaim:69480 KB

[Email protected] ~]# LS/USR/BIN/LDD | Grep-o "/[a-z]\+$"

/ldd

[Email protected] ~]#

[Email protected] ~]# Ifconfig | grep--color=auto "\<\ ([0-9]\|[ 1-9][0-9]\|1[0-9][0-9]\|2[0-4][0-9]\|25[0-5]\) \> "

[Email protected] ~]#

[Email protected] ~]# CAT/ETC/PASSWD | grep "^\<\ ([[: alnum:]]\+\) \>.*\1$"

Sync:x:5:0:sync:/sbin:/bin/sync

Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

Halt:x:7:0:halt:/sbin:/sbin/halt

[Email protected] ~]#



24. Scripting Exercises:

1) Write a script that requires:

Statistics/etc,/Var, the sum of all non-hidden primary subdirectories and files in the/usr directory;

For:

#!/bin/bash

#

a=$ (ls/etc | wc-l)

b=$ (Ls/var | wc-l)

c=$ (ls/usr | wc-l)

count=$[$a + $b + $c]

echo "$count"


[Email protected] ~]# VI 24p1.sh

[Email protected] ~]#

[Email protected] ~]# chmod +x 24p1.sh

[Email protected] ~]#

[Email protected] ~]#./24p1.sh

303


2) Write a script that requires:

For directory/tmp/testdir1, if the current valid user has read, write, and Execute permissions, then a file is created in that directory, otherwise the message "No permissions cannot be created" is displayed.

For:

#!/bin/bash

#

Fpath=/tmp/testdir1

[-R $FPATH-W $FPATH-X $FPATH] && touch $FPATH/newdir | | echo "Don ' t right to create Useradd"


[Email protected] ~]# VI 24p2.sh

[Email protected] ~]#

[Email protected] ~]# Ll/tmp/testdir1

Total 0

[Email protected] ~]# chmod +x 24p

24p1.sh 24p2.sh

[Email protected] ~]# chmod +x 24p2.sh

[Email protected] ~]#

[Email protected] ~]#./24p2.sh

[Email protected] ~]#

[Email protected] ~]# Ll/tmp/testdir1

Total 0

-rw-r--r--. 1 root root 0 Nov 11:29 newdir

[Email protected] ~]#



3) Write a script that requires:

Select a user randomly from the same UID and GID in/etc/passwd to determine whether the user name and basic group name are the same;

For:

#!/bin/bash

#

username=$ (grep "\<\ ([[:d igit:]]\+\) .*\1\>"/etc/passwd | sort-r | head-1 | cut-d ":"-f1)

groupname=$ (id-gn $username)

[$username = = $groupname] && echo "username and group is same." | | "Username and GroupName is different."


[Email protected] ~]# VI 24p3.sh

[Email protected] ~]#

[Email protected] ~]# chmod +x 24p3.sh

[Email protected] ~]#

[Email protected] ~]#./24p3.sh

Cut:invalid option--' 1 '

Try ' Cut--help ' for more information.

./24p3.sh:line 5: [: = =: unary operator expected

./24p3.sh:line 5:username and GroupName are different.: Command not Found

[Email protected] ~]#

[Email protected] ~]# VI +5 24p3.sh

[Email protected] ~]#

[Email protected] ~]#./24p3.sh

Username and group is same.

[Email protected] ~]#


Linux-practice (23-24)

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.