Several methods of shell script execution

Source: Internet
Author: User

Several methods of shell script execution
You can execute shell scripts in the following ways:

1. The relative path method must be cd to the script path first.

[Root @ banking tmp] # cd/tmp [root @ banking tmp] #./ceshi. sh script executed successfully

2. absolute path

[Root @ banking tmp] #/tmp/ceshi. sh script executed successfully
3. bash command call
[Root @ banking/] # bash/tmp/ceshi. sh script executed successfully
4. (Space) relative or absolute Mode
[Root @ banking/] #./tmp/ceshi. sh script executed successfully
Differences between the following methods

There is no difference between the first method and the second method. Both methods need to grant the script execution permission in advance.

The third is to use the script as a bash call. Therefore, the script can be executed without the execution permission.

The first three methods are to open a sub-shell in the current shell to execute the script content. When the script content ends, the sub-shell is closed and return to the parent shell.

The fourth method is to execute the script content in the current shell instead of opening a sub-shell separately.

The difference between opening a sub-shell and not opening a sub-shell is that the inheritance relationship of environment variables, such as the current variable set in the sub-shell, without special channel processing, the parent shell is invisible.

When executed in the current shell, all the set environment variables will take effect immediately.

Verify
[root@banking /]# cat /tmp/ceshi.sh top

1. pstree display in the first three execution modes

     ├─sshd─┬─sshd───bash───bash───top     │      └─sshd───bash───pstree
2. pstree display in the fourth Execution Mode
     ├─sshd─┬─sshd───bash───top     │      └─sshd───bash───pstree

3. Verify the inheritance and visibility relationships of environment variables.

Create two scripts: father. sh and subshell. sh. Father. sh calls subshell. sh.

[Root @ banking/] # cat/tmp/father. sh v_ceshi = 'father '# ------- define the variable echo in the parent shell "Call the script in the subshell mode"/tmp/subshell. shecho "the output v_ceshi value is $ {v_ceshi}" echo "" echo "run the script in the current shell ". /tmp/subshell. shecho "the output v_ceshi value is $ {v_ceshi}" [root @ banking/] # [root @ banking/] # cat/tmp/subshell. sh v_ceshi = son [root @ banking/] #
The execution result is
[Root @ banking/] #/tmp/father. sh: Use a sub-shell to call the script and output the v_ceshi value as father. Execute the script in the current shell and output the v_ceshi value as son.

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.