In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to program Linux Shell scripts". Many people will encounter such a dilemma in the operation of actual cases, 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!
From the programmer's point of view, Shell itself is a program written in C language. From the user's point of view, Shell is the bridge between users and Linux operating system. Users can not only enter commands to execute, but also use Shell script programming to complete more complex operations. Today, with the increasing improvement of Linux GUI, Shell programming still plays an important role in the field of system management. In-depth understanding and proficiency in Shell programming is one of the required lessons for every Linux user.
There are many kinds of Shell in Linux, such as Bourne Shell (/ usr/bin/sh or / bin/sh), Bourne Again Shell (/ bin/bash), C Shell (/ usr/bin/csh), K Shell (/ usr/bin/ksh), Shell for Root (/ sbin/sh), and so on. Different Shell languages have different syntax, so they cannot be used interchangeably. Each Shell has its own characteristics, and basically, mastering any one of them is enough. In this article, we focus on Bash, or Bourne Again Shell, which is widely used in daily work because it is easy to use and free; at the same time, Bash is the default Shell for most Linux systems. In general, people do not distinguish between Bourne Shell and Bourne Again Shell, so in the following text, we can see #! / bin/sh, which can also be changed to #! / bin/bash.
The format for writing Shell scripts using text editors such as vi is fixed, as follows:
#! / bin/sh#commentsYour commands go here
The symbol # in the first line! Tell the system that the program specified in the subsequent path is the Shell sequence that interprets the script file. If the first line does not have this sentence, an error will occur when executing the script file. The following part is the main program. Shell scripts, like high-level languages, also have variable assignments and control statements. Except for the first line, the line that begins with # is the comment line until the end of the line. If a line is unfinished, you can add "at the end of the line, which indicates that the next line and this line will be merged into the same line.
After editing, save the script to filename.sh, and the file name suffix sh indicates that this is a Bash script file. When executing a script, first change the properties of the script file to executable:
Chmod + x filename.sh
The way to execute the script is:
. / filename.sh
Let's start with the classic "hello world" and take a look at the simplest Shell script.
#! / bin/sh#print hello world in the console windowa = "hello world" echo $a
Shell Script is a weakly typed language, and you don't need to declare its type first when using variables. The new variable is stored in memory allocated in the local data area, which is owned by the current Shell, and no child process can access the local variable. These variables are different from environment variables, which are stored in another memory area, called the user environment area, where variables can be accessed by child processes. Variables are assigned by:
Variable_name = variable_value
If you assign a value to a variable that already has a value, the new value replaces the old value. When taking a value, put $in front of the variable name, and $variable_name can be used in quotation marks, which is obviously different from other high-level languages. If confusion occurs, you can use curly braces to distinguish, for example:
Echo "Hi, $as"
Instead of outputting "Hi, hello worlds", it outputs "Hi,". This is because Shell treats $as as a variable, while $as is not assigned and its value is empty. The correct way is:
Echo "Hi, ${a} s"
Variables in single quotation marks do not perform variable substitution operations.
With regard to variables, you also need to know several Linux commands related to them.
Env is used to display variables and their values in the user environment area; set is used to display variables and their values in the local data area and the user environment area; and unset is used to delete the current value of the specified variable, which will be specified as the NULL;export command to transfer variables in the local data area to the user environment area.
Let's look at a more complex example. Combined with this example, let's talk about the syntax of Shell Script.
#! / bin/bash # we have less than arguments. Print the help text: if [$#-lt]; then cat
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.