In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge about "how to run a Shell script in Linux". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
There are two ways to run shell scripts in Linux. You can use:
bash script.sh
Alternatively, you can execute a shell script like this:
./ script.sh
It may be simple, but it doesn't explain much. Don't worry, I'll use examples to explain what's necessary so that you can understand why you should use a given syntax format when running a shell script.
I'll use this line of shell script to make things as simple as possible:
abhishek@itsfoss:~/Scripts$ cat hello.sh echo "Hello World! "Method 1: Run shell scripts by passing files as arguments to the shell
The first involves passing the name of the script file as an argument to the shell.
Given that bash is the default shell, you can run a script like this:
bash hello.sh
Do you know the advantage of this method? Your script does not require permission to execute. Very convenient and fast for simple tasks.
Running a Shell Script in Linux
If you're not familiar with it, I suggest you read my detailed guide to file permissions for Linux.
Remember, what you need to pass as a parameter is a shell script. A shell script consists of commands. If you use an ordinary text file, it will complain about the wrong command.
Run a text file as a script
In this approach, you specify explicitly that you want to use bash as the interpreter for the script.
Shell is just a program, and bash is just an implementation of Shell. There are other shell programs, such as ksh, zsh, etc. If you have other shells installed, you can use them instead of bash.
For example, I have installed zsh and used it to run the same script:
Using Zsh to execute Shell scripts
Method 2: Execute the shell script by specifying the path to the script
Another way to run a shell script is by providing its path. But before you can do that, your files must be executable. Otherwise, when you try to execute the script, you will get a "permission denied" error.
Therefore, you first need to make sure that your script has executable permissions. You can use chmod to grant this permission to your own script, like this:
chmod u+x script.sh
After making your script executable, you simply enter the name of the file and its absolute or relative path. Most of the time, you're in the same directory, so you can use it like this:
./ script.sh
If you are not in the same directory as your script, you can specify the absolute or relative path of the script:
Run Shell scripts in other directories
This one before the script./ Is very important (when you are in the same directory as the script).
Why can't you use script names when you're in the same directory? This is because your Linux system looks for executable files to run in several directories specified in the PATH environment variable.
Here is the value of my system PATH environment variable:
abhishek@itsfoss:~$ echo $PATH/home/abhishek/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
This means that any file with executable permissions in the following directories can run anywhere on the system:
/home/abhishek/.local/bin
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games
/usr/local/games
/snap/bin
Binaries or executables of Linux commands (like ls, cat, etc.) are located in one of these directories. This is why you can run commands anywhere in your system by using their names. See, the ls command is located in the/usr/bin directory.
When you use a script without specifying its absolute or relative path, the system will not find the script mentioned in the PATH environment variable.
Why do most shell scripts include #! in their headers? /bin/bash ?
Remember I mentioned that shell is just one program, and there are different implementations of shell programs.
When you use #! /bin/bash, you specifically specify bash as the interpreter to run scripts. If you don't do it, and with... script.sh runs a script that usually runs in the shell you are running.
Is there a problem? There might be. See, most shell syntax is common to most kinds of shells, but some syntax may vary.
For example, arrays behave differently in bash and zsh. In zsh, array indexes start at 1, not 0.
Bash Vs Zsh
Use #! /bin/bash to identify that the script is a bash script and should be run using bash as the script's interpreter, regardless of the shell being used on the system. If you use zsh's special syntax, you can do this by adding #! to the first line of the script. /bin/zsh to identify it as a zsh script.
In #! The space between/bin/bash has no effect. You can also use #!/ bin/bash 。
"How to run a Shell script in Linux" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.