In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the methods of executing shell scripts in Linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
There are many ways to write bash shell scripts, so let's make a summary now. Suppose that the file name of the shell script we wrote is hello.sh, the file location is in the / data/shell directory, and we already have execute permissions.
Method 1: change to the directory where the shell script is located (at this point, it is called the working directory) to execute the shell script:
The code is as follows:
Cd / data/shell
. / hello.sh
. / means to execute hello.sh in the current working directory. If you do not add. /, bash may respond to the error message that the hello.sh cannot be found. Because the current working directory (/ data/shell) may not be in the default search path of the executor, that is, it is not in the contents of the environment variable PASH. You can view the contents of PATH with the echo $PASH command. The current / data/shell is not in the environment variable PASH, so you must add. / to execute.
Method 2: execute the bash shell script in an absolute path:
The code is as follows:
/ data/shell/hello.sh
Method 3: use bash or sh directly to execute the bash shell script:
The code is as follows:
Cd / data/shell
Bash hello.sh
Or
The code is as follows:
Cd / data/shell
Sh hello.sh
Note that if you execute in method 3, you don't have to set the execution permissions for shell in advance, or even write to the first line in the shell file (specify the bash path). Because method 3 is executed by passing hello.sh as an argument to the sh (bash) command. At this time, hello.sh does not execute it by itself, but is called by others to execute, so do not execute permissions. Then it is easy to understand without specifying the bash path, hehe.
Method 4: execute the bash shell script in the current shell environment:
The code is as follows:
Cd / data/shell
. Hello.sh
Or
The code is as follows:
Cd / data/shell
Source hello.sh
The first three methods execute the shell script by opening a child shell environment in the current shell (called the parent shell) in which the shell script is executed. The child shell environment shuts down after the shell script is executed, and then goes back to the parent shell. Method 4 is executed in the current shell.
This is the end of the content of "what are the ways to execute shell scripts in Linux?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.