Considerations for writing shell scripts in Linux

Source: Internet
Author: User
Tags builtin echo command

    1. The operation of commands is analyzed and run from top to bottom, from left to right.
    2. The order is released as mentioned in the fifth chapter: multiple blanks between commands, options, and parameters are ignored;
    3. Blank lines are also ignored, and the [tab] button pushes the blank space as a blank key;
    4. If you read a Enter sign (CR), try to start running the line (or the string) command;
    5. If there is too much content on a line, you can use "\[enter" to extend to the next line;
    6. "#" can be annotated! Any data added to the # will be ignored as annotation text!

As a result, the program that we write in script is run by one line. Now let's assume that the program you wrote is/home/dmtsai/shell.sh, so how do you run this file? Quite simply, there are several ways to do it:

    • Direct command release: The shell.sh file must have readable and operational (RX) permissions, and then:
      • Absolute path: Use/home/dmtsai/shell.sh to release commands;
      • Relative path: If the working directory is/home/dmtsai/, use./shell.sh to run
      • Variable "path" function: Place shell.sh in the directory specified by PATH, for example: ~/bin/
    • Run as a bash program: Run through "bash shell.sh" or "sh shell.sh"

Anyway, the point is to let the shell.sh inside the command can be run meaning! Hey! So why do I need to use "./shell.sh" to give orders? Have you forgotten? Go back to the command search order in chapter 11th. Look, you'll know why! At the same time, the ~/bin directory under the CentOS Default user home directory will be configured to $PATH, so you can also create shell.sh under/home/dmtsai/bin/(the ~/bin directory needs to be configured yourself). At this point, if shell.sh within the ~/bin and has the RX permission, then directly enter the shell.sh can run the script!

So why can "sh shell.sh" also run? This is because/bin/sh is actually/bin/bash (link file), using SH shell.sh that is to tell the system, I want to run directly with bash function shell.sh This file of the relevant command meaning, so at this time your shell.sh as long as there is R's permission can be run Oh! And we can also use SH parameters, such as-N and-X to check and trace shell.sh syntax is correct! ^_^

Path and command search order

In the sixth and seventh chapters, we talked about the relationship between relative paths and absolute paths, and in the first few sections of this chapter we talked about the built-in commands for alias and bash. Now that we know there are quite a few LS commands in the system, or include the built-in echo command, think about which LS is being run when a command (for example, LS) is released? It's fun! Basically, the order in which the commands are run can look like this:

    1. Run the command in a relative/absolute path, such as "/bin/ls" or "./ls";
    2. Run by alias to find the command;
    3. Run by Bash built-in (BUILTIN) command;
    4. Run through the first command that is searched $PATH the order of this variable.

For example, you can give/bin/ls and pure LS to see, you will find the use of LS color but/bin/ls is not color. Because the/bin/ls is directly to take the command to release, and LS because "alias ls= ' ls--color=tty '" This command alias and first use! If you want to know the order of command search, in fact, through the type-a LS can also be queried! The above sequence is best understood first!

Example: Configure the command alias for Echo to become echo-n, and then observe the sequence of echo runs a:
Type-a Echoecho is aliased to ' echo-n ' echo is a shell Builtinecho Is/bin/echo
Look! It's clear! Alias again builtin again by $PATH find/bin/echo!

Considerations for writing shell scripts in Linux

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.