Shell script learning-2

Source: Internet
Author: User
Tags ranges

1. Use shell programming to determine whether a file is a block or character device file. If it is copied to the/dev directory.

#! /Bin/bash
# 1.sh
# Determine whether a file is a character or a block device file. If it is copied to the/dev directory
# File executable: chmod 755 1.sh
Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export path
Echo-e "the program will judge a file is or not a device file./n"
Read-P "input a filename:" filename
If [-B "$ FILENAME"-o-c "$ FILENAME"]
Then
Echo "$ filename is a device file" & CP $ filename/dev /&
Else
Echo "$ filename is not a device file" & Exit 1
Fi

 

1.5 use shell programming to determine whether all files in the directory are block or character device files. If they are copied to the/laji directory.
For file in 'LS-L/dev | awk '/^ [BC]/{print $10 }''
Do
File = "/dev/" $ File
Echo-e "$ file is block file or character file" & CP $ File/laji-RF
Done

Or

Ls-L/dev | awk '/^ [BC]/{print "/dev/" $10}' | xargs-I CP {} laji-RF

---------------------------------

2. Design a shell program, add a new group named class1, and add 30 users to the group. The username format is stdxx, where xx ranges from 01 to 30.

#! /Bin/bash
# 2.sh
# Design a shell program, add a new group named class1, and add 30 users to the group. The username format is stdxx, where xx ranges from 01 to 30.
# File executable: chmod 755 2.sh
Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export path
Groupadd class1
For I in {9901... 9930}
Do
Xx = 'echo $ I | SED's/99 // g''
Useradd-G class1 STD $ XX
Echo STD $ XX | passwd STD $ XX -- stdin
Echo-e "User STD $ XX password is STD $ XX">/root/newuser.txt
Done

--------------------------------
3. Write a shell program to automatically delete 50 accounts. The account name is stud1 to stud50.

#! /Bin/bash
# 4.sh
# Compile a shell program to automatically delete 50 accounts. The account name is stud1 to stud50.
# File executable: chmod 755 3.sh
Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export path
I = 1
While [I-Le 50]
Do
Let I ++
Userdel-r stud $ I
Done

-------------------------------
4. A system administrator needs to do some repetitive work every day. Prepare a solution according to the following requirements:
(1) Delete All subdirectories and all files under the/ABC directory;
(2) From ~ At pm, all data in the first domain of each line in the X1 file under the/XYZ directory is read to the bak01.txt file under the/backupdirectory;
(3) archive and compress all directories and files under the/data directory as files at every Monday: backup.tar.gz;
(4) uninstall the CD-ROM of the IDE interface at PM (assuming the device name of the CD-ROM is HDC );
(5) Start the instance before a.m.

In Vim/etc/crontab, add the following content:

50 16 * root Rm-RF/ABC/* 2> & 1 &
00 8-18 *** root CAT/XYZ/X1 | awk '{print $1}'>/backup/bak01.txt 2> & 1 &
50 17 ** 1 root CD/data; tar-zcvf backup.tar.gz * 2> & 1 &
55 17 *** root umount/HDC 2> & 1 &

5) Start the instance before a.m. --> I don't quite understand what it means. I don't know if it is set to start the instance before a.m, you don't need to start it after.
Run the following command.
Chkconfig -- level 2345 crond on

----------------------------

5. Design a shell program, back up and compress all the content in the/etc directory on the first day of every month, store it in the/root/bak directory, and file name
The format is yymmdd_etc. yy is year, mm is month, and DD is day. The shell program fileback is stored in the/usr/bin directory.

Vim/usr/bin/fileback. Sh

#! /Bin/bash
# Fileback. Sh
# File executable: chmod 755 fileback. Sh
Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export path
Filename = 'date when policyunci d'_etc.tar.gz
CD/etc/
Tar-zcvf $ filename *
MV $ filename/root/bak/
------------------------------------------------------
Vim/etc/crontab
** 1 ** root./fileback. Sh &

-----------------------------------
6. What should a common user do to regularly back up/user/backup to the/tmp directory at 00:00 every Sunday?
You can use contab-E, which is similar to the above question.

7. Design a shell program and create 50 directories under the/userdata directory, that is, user1 ~ User50, and set permissions for each directory.
The permission of other users is: read; the permission of the file owner is: read, write, and execute; the permission of the group where the file owner is located is: read and execute.

#! /Bin/bash
# 7.sh
# File executable: chmod 755 7.sh
Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export path
For (I = 1; I <= 50; I ++ ))
Do
Mkdir-P/usrdata/user $ I
CD/usrdata
Chmod 754 user $ I
Done

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.