Shell script tracing and debug

Source: Internet
Author: User
Tags syslog
Shell script tracing and debug

Before scripts runs, the most feared problem is the syntax error! So how can we debug it? Is there a way to determine whether there is a problem without directly running the scripts? Haha! Of course! Let's take the bash-related parameters for determination!

[[Email protected] ~] # Sh [-nvx] scripts. sh option and parameter:-N: Do not run the script, only query syntax issues;-V: before running scloud, output the content of scripts to the screen;-X: displays the script content on the screen. This is a useful parameter! Example 1: Is there a syntax problem in the test sh16.sh? [[Email protected] ~] # Sh-N sh16.sh # If the syntax is correct, no information is displayed! Example 2: list all the running processes of sh15.sh ~ [[Email protected] ~] # Sh-x sh15.sh + Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin: /root/bin + export path + for animal in Dog Cat elephant + echo 'there are dogs .... 'There are dogs .... + for animal in Dog Cat elephant + echo 'there are cats .... 'There are cats .... + for animal in Dog Cat elephant + echo 'there are elephants .... 'There are elephants ....

In addition to the above practices, we can also:

1. We can add this command at the beginning of the script.
Set-x

2. You can add the following directly to the backend.
#! /Bin/KSh-x

3. You can do this when executing the script.
# KSh-x/locale/shell. Sh

Note that the running results in example 2 above are not displayed in color! For convenience, laruence adds color to the data after the plus sign! In the output information, the data after the plus sign is actually a command string. The command running process is also displayed in the sh-x mode, in this way, the user can determine which part of the Code will contain relevant information! This feature is amazing! By displaying the complete command string, you can correct your script based on the output error message!

Familiar with SH usage will help you easily manage Linux! As for the shell scripts learning method, you need to "read more, imitate more, and modify it into your own style !』 Is the fastest way to learn! There are quite a few friends on the network who are developing some very useful scripts. If you can use the other party's scripts, and change it to a suitable host! So the learning effect will be the fastest!

In addition, we have many service startup scripts in Linux. If you want to know what functions each script represents? You can directly use Vim to access the script. Generally, you will know the purpose of the script immediately. For example, we have mentioned the/etc/init. d/syslog file. Why is this script used? Use VI to check the first few lines of words. The following information appears:

# description: Syslog is the facility by which many daemons use to log # messages to various system log files.  It is a good idea to always # run syslog.### BEGIN INIT INFO# Provides: $syslog### END INIT INFO

 

Simply put, this script starts a daemon program named syslog, which can help many system services to record their log files ), we recommend that you always start syslog in Linux! Hey! You can see what it is!

In addition, all examples in this chapter can be found in http://vbird.dic.ksu.edu.tw/linux_basic/0340bashshell-scripts/scripts-v3.tar.bz2 Oh! Come on ~

 

From: http://vbird.dic.ksu.edu.tw/linux_basic/0340bashshell-scripts_6.php

 

Shell script tracing and debug

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.