How to execute a script in Linux

Source: Internet
Author: User

---restore content starts---

how to automatically execute an execution script on Linux at system startup
    1. If the script is executed immediately, the script can be written to rc.local;

    2. If the user log on after the automatic execution of the script, you can write the script to the appropriate user directory "~/.bash_profile", if the script "~/.bash_profile" does not exist, you can directly copy "/etc/profile" named "~/.bash_ Profile ";

    3. If you want to automatically execute a script after any user logs in, you can write the script to "/etc/profile".

4 ways to execute shell scripts in Linux summary

This article mainly introduces the 4 methods of executing shell scripts in Linux, that is, the 4 ways to run shell scripts in Linux, and the friends you need can refer to the following:

There are several ways to bash a shell script, and now make a summary. Suppose we write a good shell script with the file name hello.sh, the file location in the/data/shell directory and have execute permissions.

Method One: Switch to the directory where the shell script is located (at this time, called the working directory) to execute the shell script:

Cd/data/shell

./hello.sh

The./means to execute hello.sh under the current working directory. If you do not add./,bash may respond to an error message that could not be found hello.sh. Because the current working directory (/data/shell) may not be the default search path for the executing program, that is, it is not in the contents of the environment variable pash. The Echo $PASH command is available to view the contents of the path. Now the/data/shell is not in the environment variable pash, so you must add./To execute.

Method Two: Execute the Bash shell script in an absolute path

/data/shell/hello.sh

Method Three: Use bash or sh directly to execute the Bash shell script:

Cd/data/shell

Bash hello.sh

Or

Cd/data/shell

SH hello.sh

Note that if you do this in method three, you do not have to set the execution permissions of the shell beforehand, or even write the first line in the shell file (specify the bash path). Because method three is performed by passing hello.sh as a parameter to the SH (bash) command. This is not hello.sh to execute itself, but is called by others to execute, so do not execute permissions. Then you don't have to specify the bash path. Naturally, you understand.

Method Four: Execute the Bash shell script in the current shell environment:

Cd/data/shell

. hello.sh

Or

Cd/data/shell

SOURCE hello.sh

The first three ways to execute a shell script are to open a child shell environment in the current shell (called the parent Shell), which is executed in this child shell environment. When the shell script finishes executing, the child shell environment closes and then goes back to the parent shell. The method four is executed in the current shell.

---restore content ends---

How to execute a script in Linux

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.