Linux Seventh Week Micro jobs

Source: Internet
Author: User
Tags bz2 chmod

1. Write a script to determine whether the shell of all users on the current system is a login shell (that is, the user's shell is not/sbin/nologin), the number of these two types of users, and the string comparison;

1) Create file]# vim usershells.sh

#!/bin/bash

# Check current system All users are its login shells

#


Declare-i nologin_num=0

Declare-i login_num=0


For I in $ (cut-d:-F7/ETC/PASSWD); Do

If ["$i" = = "/sbin/nologin"]; Then

Let nologin_num++;

Else

Let login_num++;

Fi

Done


echo "The total number of the user shell, can ' t login is: $nologin _num"

echo "The total number of the user shell This can login is: $login _num"

2) Grant user execute permissions #] chmod u+x usershells.sh

3) Execute the file]#./usershells.sh The following tips: Of course, everyone is the system user number is not the same

The total number of user shells that can ' t login is:23

The total number of user shells that can login Is:5

4) Perform]# grep-o/sbin/nologin/etc/passwd | Wc-l to verify that the user cannot log on to the shell

5) Perform]# grep-v/sbin/nologin/etc/passwd | Wc-l to verify that the user can log in to the shell


2. Write a script

(1) Obtain the host name of the current host and save it in the hostname variable;

(2) Determine if the value of this variable is localhost, and if so, modify the current hostname to www.magedu.com;

(3) Otherwise, the current host name is displayed;

1) Edit File]# vim hostname.sh

#!/bin/bash

# Get current system hostname

Hostname= ' hostname ' #使用反引号获取

If ["$hostname" = = "localhost"]; Then

Hostname www.magedu.com

echo "Hostname have changed to Www.magedu.com"

Else

echo "Current hostname is $hostname"

Fi

2) Grant hostname.sh File execution rights]# chmod u+x hostname.sh

3) Execute file]#./hostname.sh

Hostname have changed to www.magedu.com


3, write a script to complete the following functions

(1) Pass a disk device file path to the script to determine whether the device exists;

(2) If present, displays all the partition information on this device;

1) Edit File #] Vim deviceinfo.sh

#!/bin/bash

# Check device is it isn't exists, it's exist that list deviceinfo


Read-p "Please input a device path:" DevicePath

If [-Z $devicepath];then

echo "Usage:please input a device path"

Exit 1

Fi

If [-B $devicepath];then

Fdisk-l $devicepath

Else

echo "No such device"

Fi

2) Grant user execute permission]# chmod u+x deviceinfo.sh

3) Execute file]#./deviceinfo.sh as follows:

Please input a device PATH:/DEV/SDA #在提示下输入/DEV/SDA equipment Path


Disk/dev/sda:21.5 GB, 21474836480 bytes, 41,943,040 sectors

Units = Sector of 1 * bytes

Sector size (logical/Physical): 512 Bytes/512 bytes

I/o size (min/best): 512 Bytes/512 bytes

Disk label Type: dos

Disk identifier: 0x00049c92


Device Boot Start End Blocks Id System

/DEV/SDA1 * 2048 1026047 512000 Linux

/dev/sda2 1026048 41943039 20458496 8e Linux LVM

[Email protected] tmp]#./deviceinfo.sh

Please input a device path:/dev/sdb


Disk/dev/sdb:85.9 GB, 85899345920 bytes, 167,772,160 sectors

Units = Sector of 1 * bytes

Sector size (logical/Physical): 512 Bytes/512 bytes

I/o size (min/best): 512 Bytes/512 bytes


Do not enter anything and then directly enter

[Email protected] tmp]#./deviceinfo.sh

Please input a device path:

Usage:please Input a device path

Enter a device path that does not exist

[Email protected] tmp]#./deviceinfo.sh

Please input a device Path:/dev

No such device


4, write a script to complete the following functions

The script can accept a parameter;

(1) If the parameter 1 is quit, the exit script is displayed, and the normal exit is performed;

(2) If the parameter 1 is yes, the execution script is displayed;

(3) Otherwise, the parameter 1 is any other value, performs an abnormal exit;

1) Edit the file

[email protected] tmp]# cat parametertest.sh

#!/bin/bash

#

Read-p "Please input a word (Quit|yes):" Parameter

While True;do

Case $parameter in

Quit

echo "Exit the script"

Exit 0;;

Yes

echo "continue to excute the script"

Read-p "Please input a word (Quit|yes):" parameter;;

*)

echo "Error Exit"

Exit 1;;

Esac

Done

2) Grant user execute file]# chmod u+x parametertest.sh

3) test script file

[Email protected] tmp]#./parametertest.sh

Please input a word (Quit|yes): Yes

Continue to excute the script

Please input a word (Quit|yes): Quit

Exit the script

[Email protected] tmp]#./parametertest.sh

Please input a word (Quit|yes): bye

Error exit


5, write a script to complete the following functions

Pass a parameter to the script, this parameter is one of gzip, bzip2 or XZ;

(1) If the value of parameter 1 is gzip, use the TAR and gzip archive to compress/etc directories into the/backups directory and name/backups/etc-20160613.tar.gz;

(2) If the value of parameter 1 is bzip2, use tar and bzip2 archive to compress/etc directory to/backups directory, and named/backups/etc-20160613.tar.bz2;

(3) If the value of parameter 1 is XZ, then the tar and XZ archives are used to compress/etc directories into the/backups directory and named/backups/etc-20160613.tar.xz;

(4) Any other value, the error compression tool is displayed, and an abnormal exit is performed;

1) Create a script file Vim compress.sh

[email protected] tmp]# cat compress.sh

#!/bin/bash

# Check input parameter is it gzip or bzip2 or XZ


#


if [!-e/backups]; Then

Mkdir/backups

Fi

Read-p "Please choose a format of compression (GZIP|BZIP2|XZ):" Zip

Case $zip in

Gzip

tar-zcf/backups/etc-$ (date +%y%m%d). tar.gz/etc;

BZIP2)

tar-jcf/backups/etc-$ (date +%y%m%d). tar.bz2/etc;

Xz

tar-jcf/backups/etc-$ (date +%y%m%d). tar.xz/etc;

*)

echo "error compression format"

Exit 1;;

Esac

2) Grant user execute permission]# chmod u+x compress.sh

3) Execute script

[Email protected] tmp]#./compress.sh

Please choose a format of compression (GZIP|BZIP2|XZ): gzip

Tar: Remove the Beginning "/" from the member name

[Email protected] tmp]#./compress.sh

Please choose a format of compression (GZIP|BZIP2|XZ): bzip2

Tar: Remove the Beginning "/" from the member name

[Email protected] tmp]#./compress.sh

Please choose a format of compression (GZIP|BZIP2|XZ): XZ

Tar: Remove the Beginning "/" from the member name

[Email protected] tmp]#./compress.sh

Please choose a format of compression (GZIP|BZIP2|XZ): Tar

Error compression format

[Email protected] tmp]# ls-l/backups/

Total dosage 26328

-rw-r--r--1 root root 8577760 August 16:38 etc-20170826.tar.bz2

-rw-r--r--1 root root 9794015 August 16:35 etc-20170826.tar.gz

-rw-r--r--1 root root 8577760 August 16:38 etc-20170826.tar.xz


6. Write a script that accepts a path parameter:

(1) If it is an ordinary document, it can be accessed normally;

(2) If it is a catalog file, then the CD command is available for it;

(3) If the file is a symbolic link, the description is a access path;

(4) Other is impossible to judge;

1) Create a script file Vim pathtest.sh

[email protected] tmp]# cat pathtest.sh

#!/bin/bash

#

Read-p "Please input a path:" Path

If [-f $path]; Then

echo "${path} can be visited"

Cat $path

elif [-D $path]; Then

echo "${path} can use ' CD ' command"

elif [-H $path]; Then

echo "${path} is a access path"

Ls-l $path

Else

echo "Unknown file"

Exit 1

Fi

2) Grant user execute permission chmod u+x pathtest.sh

3) test File

[Email protected] tmp]#./pathtest.sh

Please input a path:/etc

/etc can use ' CD ' command

[Email protected] tmp]#./pathtest.sh

Please input a path:/ETC/FA

Unknown file

[Email protected] tmp]#./pathtest.sh

Please input a path:/etc/fstab

/etc/fstab can visited


#

#/etc/fstab

# Created by Anaconda on Mon Jul 3 01:42:15 2017

#

# Accessible filesystems, by reference, is maintained under '/dev/disk '

# See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info

#

/DEV/MAPPER/CENTOS-ROOT/XFS Defaults 0 0

Uuid=ddb0b178-1278-498e-acee-c1436685cfbc/boot XFS Defaults 0 0

/dev/mapper/centos-swap swap swap defaults 0 0

[Email protected] tmp]#./pathtest.sh

Please input a path:/dev/cdrom

/dev/cdrom is a access path

lrwxrwxrwx 1 root root 3 August 15:20/dev/cdrom-sr0


7, write a script, get the host name of the current host, judge

(1) If the host name is empty or localhost, or "(none)", it is named mail.magedu.com;

(2) Otherwise, display the existing host name can be;

1) Edit the script file vim hostnametest.sh

[email protected] tmp]# cat hostnametest.sh

#!/bin/bash

# Get current hostname

Hostname= ' hostname '

If [-Z $hostname-o $hostname = = "localhost"-o $hostname = = "None"]; Then

Hostname mail.magedu.com

Else

echo "Current hostname is $hostname"

Fi

2) Grant user execute permission]# chmod u+x hostnametest.sh

3) test script file

[Email protected] tmp]# hostname

Www.magedu.com

[Email protected] tmp]#./hostnametest.sh

Current hostname is www.magedu.com


8, write a script, accept a user name as a parameter;

(1) If the user's ID number is 0, it is displayed as an administrator;

(2) If the user's ID number is greater than 0 and less than 500, it is displayed as a system user;

(3) Otherwise, it is displayed as a normal user;

1) Create a script file Vim userstest.sh

[email protected] tmp]# cat userstest.sh

#!/bin/bash

# check UserID

Read-p "Please input a username:" username

If [-Z $username];then

echo "Usage:please input a username"

Exit 1

Fi

If!id $username &>/dev/null; Then

echo "User doesn ' t exist"

Else

userid=$ (id-u $username)

If [$userid-eq 0]; Then

echo "$username is a administrator"

elif [$userid-gt 0-a $userid-lt 500]; Then

echo "$username is a system user"

Else

echo "$username is a normal user"

Fi

Fi

2) Grant user execute permission chmod u+x userstest.sh

3) Testing

[Roo[email protected] tmp]#./userstest.sh

Please input a Username:centos

CentOS is a normal user

[Email protected] tmp]#./userstest.sh

Please input a username:root

Root is a administrator

[Email protected] tmp]#./userstest.sh

Please input a username:sshd

SSHD is a system user
























This article is from the "11822904" blog, please be sure to keep this source http://11832904.blog.51cto.com/11822904/1959502

Linux Seventh Week Micro jobs

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.