Getting Started with Linux shell programming

Source: Internet
Author: User

From the programmer's point of view, the shell itself is a program written in C language, from the user's point of view, the shell is the user and the Linux operating system communication Bridge. The user can either enter command execution or use shell scripting to do more complicated operations. In the increasingly perfect Linux GUI today, in the field of system management, shell programming still plays a role that can not be ignored. An in-depth understanding and proficiency in shell programming is one of the required lessons for every Linux user.

There are many types of shell in Linux, common: 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 language syntax differs, so it cannot be exchanged for use. Each shell has its own characteristics, and basically, mastering either of these is enough. In this article, we focus on bash, the Bourne Again Shell, which is widely used in daily work due to ease of use and free, and bash is the default shell for most Linux systems. In general, people do not differentiate between the Bourne shell and the 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 a text editor such as VI is fixed, as follows:

#!/bin/sh

#comments

Your commands go here

The symbol #! in the first line tells the system that the program specified by the subsequent path is the shell program that interprets the script file. If there is no such sentence in the first line, an error will occur when executing the script file. The next part is the main program, shell scripts like high-level languages, there are variable assignments, there are control statements. In addition to the first line, the line that begins with # is the comment line until the end of the line. If a row is not complete, you can add "at the end of the line, which indicates that the next line is merged into the same row.

After editing, save the script to filename.sh, the file name suffix sh indicates that this is a bash script file. To execute the script, you first change the properties of the script file to executable:

chmod +x filename.sh

The way to execute the script is:

./filename.sh

Example one:

Let's start with the classic "Hello World" and look at the simplest shell scripts.

#!/bin/sh

#print Hello World in the console window

A = "Hello World"

Echo $a

Shell script is a weakly typed language that uses variables without first declaring their type. The new variable is allocated memory in the local data area, which is owned by the current shell, and no child processes can access the local variable. These variables are different from environment variables, and environment variables are stored in another memory area called the user environment area, and this memory variable can be accessed by the quilt process. Variables are assigned in the following way:

Variable_name = Variable_value

If you assign a value to a variable that already has a value, the new value supersedes the old value. When taking a value, add $ to the variable name, $variable _name can be used in quotation marks, which is significantly different from other high-level languages. If there is confusion, you can use curly braces to differentiate, for example:

echo "Hi, $as"

It will not output "Hi, Hello Worlds", but output "Hi,". This is because the shell treats $as as a variable, and $as is not assigned a value, and its value is empty. The correct method is:

echo "Hi, ${a}s"

Example two:

Tomcat logs are scheduled to be saved by date.

#!/bin/sh

Baktime= ' Date +%y-%m-%d_%h%m ' #定义变量, get of course year, month, day, time, minute

Cp/usr/local/tomcat/logs/catalina.out/usr/local/tomcat/logs/catalina_$baktime.log

Cp/usr/local/tomcat2/logs/catalina.out/usr/local/tomcat2/logs/catalina_$baktime.log

echo "1" >/usr/local/tomcat/logs/catalina.out #显示结果定向至文件

echo "2" >/usr/local/tomcat2/logs/catalina.out


Getting Started with Linux shell programming

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.