Shell programming in the 2nd Chapter of Shell script Getting Started

Source: Internet
Author: User

Shell programming in the 2nd Chapter of Shell script Getting Started
1. What is the shell?
The shell is a command interpreter that interprets commands and programs that execute user input, is responsible for direct conversations with the user, interprets the user's input to the operating system, and processes the output of a wide variety of operating systems, and then outputs the output to the screen back to the user. To put it simply, the shell translates the user's words to the kernel, the kernel listens to control the hardware to work, after working the kernel will tell Shell,shell the result to print to the user to see. The shell is a bridge between the user and the kernel and even the hardware.
User knocks command->shell translation-Kernel control--hardware----kernel result escalation->shell printing--Users Get answers
Shell in English means "shell", Linux is said to be like a shell to wrap the system kernel and hardware.
2. What is a shell script
When a command or program statement is not executed at the command line, but is executed in the shell in the form of a program file, this program is called a shell script. The shell script is for the shell, it writes some shell syntax and commands inside, with regular expressions, pipelines, redirects, and so on, to achieve the function we want.
Example 1, write a command script to clear/var/log/messages.
#!/bin/bash
#version: v1.0
echo >/var/log/messages && echo "log cleaned up."
This is basically OK. But think about it, there are some problems with this script, as follows:
(1) The non-root user does not have permission to execute this command.
(2) Just do the sequential operation, there is no process Control statement.
Example 2, write a script, the same function but with process control. Each step should be prompted for success or failure.
#!/bin/bash
#version: v2.0
#首先判断是不是root, then determine if the log directory exists, and then determine whether the log exists, and finally empty the log.
#判断是不是root (whether the $UID is 0)
If [$UID-ne 0]
Then echo "Must is root!"
Exit 1 #返回1表示脚本执行失败了
Fi
echo "You are root."
#判断日志目录是否存在
Cd/var/log | | {
echo "Cannot change to the directory."
Exit 1
}
#清空日志
echo > Messages && {
echo "Log cleaned up."
Exit 0
}
echo "Log cannot clean up."
Exit 1
Important: The position of curly braces {} must not be wrong!! I hit the back of "Exit 1", "Exit 0":
Cd/var/log | | {
echo "Cannot change to the directory."
Exit 1}
It then prompts Logclean4.sh:line 29:syntax error:unexpected end of file. I've been looking for one hours to find out what's wrong! The advice is the same as the old boy knocked.
3. The Shell's status
Shell scripts are suitable for handling plain text types of data, whereas configuration and log files for almost all services in Linux are of plain text type. Therefore, learning shell scripts well can play a huge role in Linux systems!
4. Shell type
(1) Bourne shell, including sh, ksh, bash three kinds.
(2) C shell, including CSH, tcsh two kinds.
View Linux-supported Shell:cat/etc/shells
5. Comparison of other scripting languages
(1) PHP, focus on web development
(2) Perl, more complex syntax
(3) Python, you can learn in depth after mastering shell programming
In contrast, the Shell's advantage lies in dealing with the underlying business of the biased operating system. Some common system scripts, using shell development will be much simpler and faster.
6. Check the default shell of the operating system
There are two methods in the old boy's book, but the second method doesn't seem to suit me. A third method is given in the video, with the fourth method on the Web:
(1) [[email protected] ~]# echo $SHELL
/bin/bash
(2) [[email protected] ~]# grep root/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
This method is improved by adding a ^ in front of root to indicate a line rooted in root:
[[email protected] ~]# grep ^root/etc/passwd
Root:x:0:0:root:/root:/bin/bash
(3) [[email protected] ~]# head-1/etc/passwd
Root:x:0:0:root:/root:/bin/bash
(4) [[email protected] ~]# grep shell/etc/default/useradd
Shell=/bin/bash
This method can also be equivalent to
[Email protected] ~]# Useradd test
[Email protected] ~]# tail-1/etc/passwd
Test:x:501:501::/home/test:/bin/bash

Appendix:
1. Four ways to clear the log
(1) echo "" > Test.log
(2) echo > Test.log
(3) > Test.log
(4) Cat/dev/null > Test.log
2. View the bash version
Bash--version

Shell programming in the 2nd Chapter of Shell script Getting Started

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.