Learn more about Linux Shell Scripting Basics (eight)

Source: Internet
Author: User
Tags echo command
The previous instance of the Linux shell script is a description of the decimal and binary conversions, as well as an example of a file loop to end this part of the learning. Learners who believe that the basic learning of Linux shell scripts should be able to master some simple Linux shell script writing.
File Recycling Program
Maybe you're one of those people who want to save all the sent messages to a file, but after a few months, the file can become so large that it slows down the file. The following script Rotatefile can be
To solve this problem. This script can rename the message to save the file (assuming outmail) is OUTMAIL.1, and for outmail.1 it becomes outmail.2 and so on ...

#!/bin/sh# vim:set sw=4 ts=4 et:ver= "0.1" help () {cat <rotatefile--Rotate the File Nameusage:rotatefile [-h] filenameoptions:-H help textexample:rotatefile outthis would e.g rename out.2 to Out.3, O Ut.1 to Out.2, off to Out.1and create a empty out-filethe max number is 10version $verHELPexit 0}error () {echo "$" exit 1} While [-N "$"]; Docase $ in-h) Help;shift 1;; --) break;; -*) echo "error:no such option $. -H for Help "; exit 1;; *) break;; esacdone# input check:if [-z] "$"; Thenerror "Error:you must specify a file, use-h for help" fifilen= "$" # Rename any. 1,. 2 etc file:for N in 9 8 7 6 5 4 3 2 1; Doif [-F "$filen. $n"];  thenp= ' expr $n + 1 ' echo ' mv $filen. $n $filen. $p "MV $filen. $n $filen. $pfidone # Rename the original file:if [-F" $filen "]; Thenecho "MV $filen $filen. 1" MV $filen $filen. 1fiecho Touch $filentouch $filen 

How does this script work? After detecting a file name provided by the user, we perform a 9 to 1 loop. File 9 is named 10, file 8 is renamed to 9, and so on. After the loop is complete, we name the original file 1
Create an empty file with the same name as the original file.
Debugging
The simplest debug command, of course, is to use the echo command. You can use Echo to print any variable value in any place where you suspect it is wrong. That's why most shell programmers spend 80% of their time debugging programs. of the Shell program
The advantage is that there is no need to recompile, and it does not take much time to insert an echo command.
The shell also has a real debug mode. If there is an error in the script "Strangescript", you can debug it this way:

Sh-x Strangescript


This executes the script and displays the values of all variables.
The shell also has a pattern that does not need to execute a script just to check the syntax. This can be used:

Sh-n Your_script


This will return all syntax errors
Here, the basics of Linux shell scripting are all over. Thank you for your support.

The above is the detailed introduction of the Linux shell Script Basic Learning (eight) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.