Shell script programming and file system Repair

Source: Internet
Author: User
Tags lfcs

Shell script programming and file system Repair
GuideThe Linux Foundation has initiated the LFCS certification (Linux Foundation Certification System Administrator) Linux Foundation Certified Sysadmin, which is a brand new certification system, the certification aims to enable people from all over the world to take the basic management operations of medium-level Linux systems. The certification includes: maintain the running system and service capabilities, comprehensive monitoring and analysis capabilities, and the decision-making capability to request support from upstream teams.

Linux Foundation certification program.
Video Player

This lecture focuses on simple Shell script programming and file system repair. Both of these are required test sites for LFCS certification.

Understanding terminal and Shell

First, declare some concepts.

  • Shell is a program that passes commands to the operating system for execution.
  • Terminal is also a program that allows end users to use it to interact with Shell. For example, the image below is GNOME Terminal.


Gnome Terminal

After the Shell is started, a command prompt (also called the command line) is displayed, prompting us that the Shell is ready to accept the commands entered by the standard input device, which is usually the keyboard. You can refer to "How to Use GNU sed and other commands to create, edit, and operate files on Linux" in this series of articles to learn some common commands.
Linux provides a lot of optional Shell, which are listed below:

Bash
Shell

Bash represents the Bourne Again Shell, which is the default Shell of the GNU project. It draws on the useful features in the Korn shell (ksh) and C shell (csh), and improves the performance at the same time. It is also the default Shell of each release covered by LFCS certification and will be used in this series of tutorials.

Sh
Shell

The Bourne SHell is an old shell, which has been the default shell for many Unix-like systems for many years.

Ksh
Shell

Korn SHell (ksh shell) is also a Unix shell developed by David Korn of Bell Labs in early 1880s. It is compatible with the Bourne shell and contains most of the C-shell features.
A shell script is only an executable text file, which contains an executable command.

Simple Shell script Programming

As mentioned above, a shell script is a plain text file, so you can use your favorite text editor to create and edit it. You can consider using vi/vim (refer to "how to install and use the plain text editor vi/vim" in this series. Its syntax highlighting makes my editing work very convenient.
Run the following command to create a script file named myscript. sh:

# Vim myscript. sh

The first line of the shell script (the famous shebang line) must be as follows:

#!/bin/bash()

This statement "tells" the interpreter used by the operating system to run the script file and then run the command.
Now you can add the command to be executed. Through annotations, we can declare the specific meaning of each command or the entire script. Note: shell ignores the comment statements starting with.

#! /Bin/bashecho this is the tenth part of the LFCS certification series echo today is $ (date + % Y-% m-% d)

After writing and saving the script, run the following command to make the script file executable:

# chmod 755 myscript.sh

Before executing the script, let's talk about the environment variable ($ PATH) and run:

echo $PATH

We will see the specific content of the environment variable ($ PATH): This is the directory of the executable program searched by the system when the command is entered, and each item is separated by a colon. It is called an environment variable because it is a part of the shell environment-a series of information that can be obtained by the shell and its sub-processes at each startup of the shell.
When we enter a command and press enter, shell searches for the directories listed in the $ PATH variable and runs the first known instance. See the following example:

Environment Variable
If two executable programs with the same name exist, one is in/usr/local/bin, and the other is in/usr/bin, the first one listed in the environment variable will be executed, and ignore the other one.
If the script we wrote is not placed in the $ PATH variable to list any of the directories, enter./filename to execute it. If any directory in the $ PATH variable is stored, we can run the previously written script like other commands.

# Pwd #./myscript. sh # cp myscript. sh ../bin # cd ../bin # pwd # myscript. sh

Execute scripts


Whenever you need to take the appropriate action based on the running result of a command in the script, you should use the if structure to define the condition. The basic syntax is as follows: if Condition Statement
If CONDITION; then COMMANDS; else OTHER-COMMANDS fi

Here, CONDITION can be any of the following conditions (only common ones are listed), and true is returned when the following conditions are met:

  • [-A file]

    → The specified file exists.

  • [-D file]

    → The specified file exists and is a directory.

  • [-F file]

    → The specified file exists and is a common file.

  • [-U file]

    → The specified file exists and the SUID permission is set.

  • [-G file]

    → The specified file exists and the SGID permission bit is set.

  • [-K file]

    → The specified file exists and the "Sticky" bit is set.

  • [-R file]

    → The specified object exists and is readable.

  • [-S file]

    → The specified file exists, and the file is not empty.

  • [-W file]

    → The specified file exists and can be written.

  • [-X file]

    → The specified file exists and can be executed.

  • [String1 = string2]

    → The string is the same.

  • [String1! = String2]

    → Strings are different.

[Int1 op int2] is a part of the preceding list (for example, if-eq-> int1 and int2 both return true), the comparison item can also be a list subitem, op is the following comparison operator.

  • -Eq

    -> If int1 is equal to int2, true is returned.

  • -Ne

    -> If int1 is not equal to int2, true is returned.

  • -Lt

    -> If int1 is smaller than int2, true is returned.

  • -Le

    -> If int1 is smaller than or equal to int2, true is returned.

  • -Gt

    -> If int1 is greater than int2, true is returned.

  • -Ge

    -> If int1 is greater than or equal to int2, true is returned.

For Loop statement

Loop statements can repeatedly execute a command under a certain condition. The basic syntax is as follows:

For item in SEQUENCE; do COMMANDS; done

Here, item is the value matched in SEQUENCE when COMMANDS is executed each time.

While loop statement

The loop structure executes repeated commands until the exit status value of the Control Command (EVALUATION_COMMAND) is 0 (that is, the execution is successful. The basic syntax is as follows:

While EVALUATION_COMMAND; do EXECUTE_COMMANDS; done

Among them, EVALUATION_COMMAND can be any command that can return the exit status value of success (0) or failure (value other than 0). EXECUTE_COMMANDS can be any program, script, or shell struct, including other nested loops.

Comprehensive use

The following example demonstrates the if condition statement and for loop statement.In the release based on systemd, check whether a service creates a file before running to list the service names we want to view.

# Cat myservices.txt sshd mariadb httpd crond firewalld

Use scripts to monitor Linux servicesThe script we wrote should look like this:

#! /Bin/bash # This script iterates over a list of services and # is used to determine whether they are running or not. for service in $ (cat myservices.txt); do systemctl status $ service | grep -- quiet "running" if [$? -Eq 0]; then echo $ service "is [ACTIVE]" else echo $ service "is [INACTIVE or not installed]" fi done

Linux service monitoring script Let's explain the workflow of this script. 1) each time a for loop reads a record from the myservices.txt file, each record represents a common variable name of a service. The records are composed of the following:

# Cat myservices.txt

2). The preceding command is enclosed by parentheses and the dollar sign is added to the front, indicating that it needs to be taken from the record list of myservices.txt and passed as a variable to the for loop. 3). perform the following actions for each record in the record list (that is, the Service variable of each record:

# Systemctl status $ service | grep -- quiet "running"

In this case, you need to add a dollar sign before each common variable name (that is, the Service variable for each record) to indicate that it is passed as a variable. The output is sent to grep through the pipeline. Among them, the-quiet option is used to prevent the grep command from displaying the "running" lines to the screen. When grep captures "running", an exit status code "0" is returned (the if struct is expressed as $ ?), This confirms that a service is running. If the exit status code is a non-zero value (that is, "running" is not displayed in the echo in the systemctl status $ service command), it indicates that a service is not running.Service monitoring scriptWe can add a step to check whether myservices.txt exists before starting the loop.

#! /Bin/bash # This script iterates over a list of services and # is used to determine whether they are running or not. if [-f myservices.txt]; then for service in $ (cat myservices.txt); do systemctl status $ service | grep -- quiet "running" if [$? -Eq 0]; then echo $ service "is [ACTIVE]" else echo $ service "is [INACTIVE or not installed]" fi done else echo "myservices.txt is missing" fi

Ping a series of network or Internet host names to obtain response data. You may want to write your own host to a text file, run the script to check whether the hosts can be pinged (the myhosts in the script can replace the name you want ). The built-in read command of shell will tell the while loop to read myhosts from one row, pass the read content of each row to the host variable, and then the host variable to the ping command.

#! /Bin/bash # This script is used to demonstrate the use of a while loop while read host; do ping-c 2 $ host done & lt; myhosts

Use scripts to Ping the serverAdditional reading:

  • Learn Shell Scripting: A Guide fromNewbies to System Administrator
  • 5 Shell Scripts to Learn Shell Programming
File System troubleshooting

Although Linux is a very stable operating system, it will still crash for some reason (such as power failure), and you will have one (or more) if the file system fails to be correctly uninstalled, the system automatically detects possible errors when Linux is restarted. In addition, each time the system is started normally, their integrity will be verified before the file system is mounted. All of these depend on the fsck tool (file system check )). If fsck is set, in addition to verifying the integrity of the file system, it can also try to fix the error. Whether fsck can successfully repair errors depends on the degree of damage to the file system. If yes, the damaged files will be restored to the lost + found in the root directory of each file system. Last but not least, we must note that if you unplug the USB device that is writing data to the system, errors may occur, or even hardware damage may occur. The basic usage of fsck is as follows:

# Fsck [options] filesystem

Check the file system error and try to automatically fix it. To use fsck to check the file system, first uninstall the file system.

# Mount | grep sdg1 # umount/mnt # fsck-y/dev/sdg1

Check file system errorsIn addition to the-y option, we can also use the-a option to automatically fix file system errors without having to make interactive responses and force verification when the file system looks clean and unmounted.

# Fsck-af/dev/sdg1

If you only want to find out where an error has occurred (you do not need to fix it when an error is detected), you can use the-n option, this will only output file system errors to the standard output device.

# Fsck-n/dev/sdg1

Based on the error message output by fsck, we can determine whether the problem can be fixed by ourselves or whether the problem needs to be submitted to the engineer team for detailed hardware verification.

Summary

At this point, the tenth lecture of the series of tutorials is all over. The full series of tutorials cover the basic content required to pass the LFCS test. But obviously, the ten lectures in this series are not enough to fully describe a single topic. We hope this series of tutorials will become the basic materials for your study, and keep learning enthusiasm ).

We welcome you to raise any questions or suggestions, so you can contact us through the following link without hesitation: Become a Linux certified system engineer.

From: http://www.tecmint.com/linux-basic-shell-scripting-and-linux-filesystem-troubleshooting/

Address: http://www.linuxprobe.com/shell_file_repair.html


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.