Introduction to basic Linux shell script Learning (8)

Source: Internet
Author: User
Tags echo command

The example above the Linux shell script illustrates the conversion of decimal and binary, and ends the learning of this Part with an instance related to file loops. I believe that the basic learning of Linux shell scripts should be able to master the compilation of some simple Linux shell scripts.

File loop Program

Maybe you want to save all emails to one of the people in a file, but after a few months, this file may become so large that the access to this file may be slowed down. The following script rotatefile can be

To solve this problem. This script can rename the email storage file as outmail) to outmail.1, while outmail.1 is changed to outmail.2 and so on...

#! /Bin/sh

# Vim: set sw = 4 ts = 4 et:

Ver = "0.1"

Help ()

{

Cat <

Rotatefile -- rotate the file name

USAGE: rotatefile [-h] filename

OPTIONS:-h help text

EXAMPLE: rotatefile out

This will e. g rename out.2 to out.3, out.1 to out.2, out to out.1

And create an empty out-file

The max number is 10

Version $ ver

HELP

Exit 0

}

Error ()

{

Echo "$1"

Exit 1

}

While [-n "$1"]; do

Case $1 in

-H) help; shift 1 ;;

--) Break ;;

-*) Echo "error: no such option $1.-h for help"; exit 1 ;;

*) Break ;;

Esac

Done

# Input check:

If [-z "$1"]; then

Error "ERROR: you must specify a file, use-h for help"

Fi

Filen = "$1"

# Rename any. 1,. 2 etc file:

For n in 9 8 7 6 5 4 3 2 1; do

If [-f "$ filen. $ n"]; then

P = 'expr $ n + 1'

Echo "mv $ filen. $ n $ filen. $ p"

Mv $ filen. $ n $ filen. $ p

Fi

Done

# Rename the original file:

If [-f "$ filen"]; then

Echo "mv $ filen $ filen.1"

Mv $ filen $ filen.1

Fi

Echo touch $ filen

Touch $ filen

How does this script work? After the user provides a file name, we perform a 9-1 loop. File 9 is named 10, file 8 is renamed to 9, and so on. After the loop is completed, we name the original file as file 1.

Create an empty file with the same name as the original file.
Debugging

The simplest debugging command is the echo command. You can use echo to print any variable value in any suspected error. This is why most shell Programmers spend 80% of their time debugging programs. Shell Program

The advantage is that it does not need to be re-compiled, and it does not take much time to insert an echo command.

Shell also has a real debugging mode. If an error occurs in the script "strangescript", you can debug it as follows:

Sh-x strangescript

This will execute the script and display the values of all variables.

Shell also has a mode that only checks the syntax without executing the script. It can be used as follows:

Sh-n your_script

This will return all syntax errors

Here, the basic learning of Linux shell scripts is all over. Thank you for your support.

  1. Linux shell script BASICS (1)
  2. Detailed analysis of hierarchical defense against Linux Server attacks
  3. Detailed introduction to Linux USB flash drive boot and installation I) self-guided mode
  4. Detailed modification of Linux system firewall
  5. Linux shell script basics 7)

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.