"Linux command line and Shell scripting Encyclopedia" 23 chapters 24 chapters

Source: Internet
Author: User
Tags first string save file disk usage truncated

Chapter 23rd using other shells

The bash shell is the most widely used shell in the Linux distribution. But it's not the only option, and there are other shells you can choose from.

23.1 What is dash shell

Baidu Encyclopedia said: Dash Shell is a Unix shell, much smaller than the bash shell, in line with the POSIX standard.

Here's the book:

The dash shell was developed as part of the Debian Linux distribution, mainly in the Ubuntu Linux distribution. It is a lite version of the Bourne Shell. So it doesn't support as many features as Bashshell, which can cause some problems with scripting.

Use less, will not expand the introduction.

23.4 zsh Shell

is an open source Unix shell developed by Paul Falstad. It draws on all of the shell's current design concepts and adds a lot of unique features to create an omnipotent, high-level shell for programmers.

Some of the unique features of the following zsh shell:

1) Improved shell option handling

2) Shell compatibility mode

3) loadable Modules (state-of-the-art features)

The zsh shell provides a set of core built-in commands and provides the ability to add additional command modules.

Each command module provides another set of built-in commands for a particular scenario. For example, network support and advanced math, you can just add modules that you think are useful.

The zsh shell is typically used in a programming environment because it provides a lot of good functionality for Shell scripting programmers.

It loads a separate code base with Loadable modules, which makes the use of advanced functions as simple as running commands on the command line.

From complex mathematical algorithms to Web applications such as FTP HTTP, loadable modules support many features.

There is no introduction here. I was asked to write it again.

24th. Writing a simple script utility

Demonstrate some useful tools

24.1 Archive

1, if backing up a large number of files is best to create a dedicated directory (below this wave operation is still very six)

$sudo Mkdir/xcybak

It would be a failure to copy something directly in this way, unless you add sudo.

WORKAROUND: You can authorize users who need to create files in the centralized archive directory by using the sudo command or by creating a user group. You can create a special user Xcybak

$sudo Groupadd Xcybak//Create a user group. Xcybak is a user group

$sudo chgrp Xcybak/xcybak//Change the user group to which the file or directory (here is the/xcybak directory) belongs

Can see

$ls-ld/xcybak

$sudo usermod–ag Xcybak xcy//The xcy here refers to the currently logged in user. Add Xcy to the Xcybak user group

$sudo chmod The permissions of the 775/xcybak//file Group 7. Xcy users can read and write to/xcybak.

You can check it again.

$ls-ld/xcybak

Add users to Xcybak Group, the user must log out before logging in in order for the membership to take effect.

I've restarted the virtual machine here.

Next, you can create the file directly in the/xcybak directory.

I'm a pro-test useful.

Supplemental: About Linux user groups, file permissions

1.linux Each user must have a group that cannot be separated from the group.

2. File owner: Typically a file creator

3. Group of files: After the user creates the file, the group that the file is in is the same group as the user. You can chgrp to change the group where the user is located. the group where the user resides . Can change

4. Other groups: except the two above

5. For a file, file permissions are divided into Owner permissions, group permissions, and other permissions. Can be changed with chmod

6. Change user groups:

When you add a user, you can specify which group to add the user to.

You can change the group in which a user resides by using root admin privileges

USERMOD–G Group name username//Usermod can also receive many other parameters

7. View group information: cat/etc/group (field interpretation Group name: password: Group identification number GID: List of users in the group)

8. View user information: CAT/ETC/PASSWD

9. You can also add users to a user group $sudo Usermod–ag groupname username

A user can be added to multiple groups.

The 24.1.1 code is as follows:

Configuration file (the file to be backed up). Write the directory or file you want to back up in the Files_to_backup file

$ cat Files_to_backup

/home/xcy/shell/

/home/xcy/shellxcy

The script is as follows:

[Email protected]:~/shell/24zhang$ Cat daily_archive.sh

#!/bin/bash

date=$ (Date +%y%m%d)

# Save File name

File=xcybak$date.tar.gz

# config file directory

Config_file=/xcybak/files_to_backup

destination=/xcybak/$FILE

If [-F $CONFIG _file]

Then

Echo

Else

Echo

echo "$CONFIG _file does not exist"

echo "Backup not completed due ti missing configuration File"

Echo

Exit

Fi

File_no=1

# Redirect STD input to name of config file

exec < $CONFIG _file

Read file_name

While [$?-eq 0]

Do

# Make sure file or directory exists

If [-F $FILE _name-o-D $FILE _name]

Then

# Add its name to the list

file_list= "$FILE _list $FILE _name"

Else

Echo

echo "$FILE _name, does not exist"

echo "Obviously, I won't include it in this Xcybak"

echo "It is listed in line $FILE _no of the config FILE"

Echo

Fi

file_no=$[$FILE _no + 1]

Read file_name

Done

echo "Starting Xcybak ..."

Echo

Tar-czf $DESTINATION $FILE _list 2>/dev/null

echo "Xcybak completed"

echo "resulting Xcybak file is: $DESTINATION"

Echo

Exit

The thing that this script does is to compress the files or directories contained in the Files_to_backup. The saved name is the current time.

It can also be added to the cron table . Create a file that is backed up on a daily or hourly basis.

24.2 Managing user Accounts

Not only add modifications to delete accounts, but also consider security issues, keep the job requirements, and manage the accounts accurately.

When deleting an account, you need at least four steps:

1) Get the correct delete user account name

2) Kill the process that is part of the account that is running on the system

3) Confirm all files belonging to the account in the system

4) Delete the user account

It can be designed as an interactive script. Should not be placed in the Cron table.

The script is long and I won't write it.

24.3 Detecting disk space

This tool will help you find the top 10 disk space users in your directory. A date-named report is generated.

1. Use the DU command to display disk usage for individual files and directories

-S is used to summarize the directory and the overall usage status

-S provides total information for each directory and subdirectory, respectively. This is what we want.

$sudo du–s/home/xcy/

You can also sort in descending order:

$sudo du–s/home/xcy/| SORT–RN//-R is reverse,-n is allowed to sort by number

The list of top ten users is filtered below:

The output of the SED editor is piped out to the gawk command and then printed out

[Email protected]:/var$ sudo du-s/var/log/| sort-rn |

> sed ' {one, $D; =} ' | # = is an add line number, one line to the end of all delete

> sed ' N; s/\n//' | # N command to place line numbers and text on the same line

> gawk ' {printf $ ': "\ T" $ "t" $ "\ n"} '

1:4940/var/log/

2:4028/var/log/vmware-caf/pme

3:804/var/log/installer

4:544/var/log/upstart

5:184/var/log/samba

6:120/var/log/apt

7:40/var/log/lightdm

8:16/var/log/cups

9:12/var/log/mysql

10:12/var/log/fsck

[Email protected]:/var$

Here's the real script:

[Email protected]:~/shell/24zhang$ Cat big_users.sh

#!/bin/bash

Check_directories= "/var/log/home"

date=$ (date +%m%d%y) # Date for report file

exec > Disk_space_$date.rpt # make report file STDOUT

echo "Top Ten Disk Space Usage"

echo "for $CHECK _directories Directory"

For Dir_check in $CHECK _directories

Do

echo ""

echo "The $DIR _check Directory:"

Du-s $DIR _check 2>/dev/null |

Sort-rn |

Sed ' {one, $D; =} ' |

Sed ' N; s/\n//' |

Gawk ' {printf $ ': "\ T" $ "t" $ "\ n"} '

Done

Exit

[Email protected]:~/shell/24zhang$

Run:

./big_user.sh

Only two directories/var/log/home are specified above, and the generated report is disk_space_$date.rpt here.

This script can be implemented with a cron table, allowing it to run automatically.

Add:

1. I always get an error when I install the software recently

An error occurred while processing:

Open-vm-tools

open-vm-tools-dkms

open-vm-dkms

E:sub-process/usr/bin/dpkg returned an error code (1)

The solution: the equivalent of removing vm-tools. No full screen after removal, it's not good to watch.

sudo apt-get autoremove open-vm-dkms open-vm-tools--purge

2. What does "%", "#", "*" mean in the shell, such as ${a%#.}, ${a%%.*}, ${a##.*}

${varible##*string} The string after the last string is truncated from left to right

${varible#*string} A string after the first string is truncated from left to right

${varible%%string*} to intercept the string after the last string from right to left

${varible%string*} The string {usb2%:*} after the first string is intercepted from right to left

"* "Just a wildcard can sometimes not

Example:

$ myvar=foodforthought.jpg

$ echo ${myvar##*fo}

Rthought.jpg

$ echo ${myvar#*fo}

Odforthought.jpg

"Linux command line and Shell scripting Encyclopedia" 23 chapters 24 chapters

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.