shell programming concepts and specifications

shell programming specification

I. Introduction

Programming specifications and shell variables will explain in this article and verify instantiated, suitable for beginners to learn basic shell. In the learning process, we need to do it yourself, practice a few times, learning by doing experience its meaning, then learn self summarized, and gradually upgrade their skills. You will be able to believe in yourself!

First, we introduce strong language and simple language to explain the weak type (currently not understand it does not matter):

  • Strong language type - need to declare the variable type
  • Weak type of language - no need to declare a variable type
  • HTML markup language tags <head> </ head>

The shell script programming, typical of the weak type of language, where the variables defined directly, without having to declare, and the shell language is not object-oriented. Commands only need to be performed in order to save a text file, give the file executable permissions, you can run slowly when depth study shell programming, can be combined with various control Shell statement to perform more complex operations, combined with shell scripts programming can be a variety of services such as a large number of operations performed directly by a script, improve work efficiency.

Of course, if you want to learn shell, necessarily familiar with shell commands, especially commonly used commands and some important commands such as: grep, egrep, sed, awk and so on.

Second, the concept of shell

2.1 What is a shell?

shell-- In computer science, commonly known as Shell shell (to distinguish core), it refers to the software (command parser) "provide the user with user interface". It is similar to command.com and cmd.exe later under DOS. It receives user commands, and then calls the appropriate application.

2.2shell application scenarios

  • Repetitive operations
  • Batch Transactions
  • Automated operation and maintenance
  • Service health monitoring
  • Timing task execution

2.3shell role - command interpreter

Interposed between the kernel and the user, is responsible for interpreting the command line, execution line using a plurality of unrelated commands ";" spaced apart performed

Three, shell programming specifications

Linux system shell script is a special application that follows the standard script structure, and can output a friendly message, easier to read. The code corresponding to the more complex structure of the script, it should be necessary to add a comment text. Examples are as follows:

[root@localhost shell]# cat demo.sh 
#!/bin/bash               //特殊的脚本声明,表示此行以后的语句通过/bin/bash程序解释
#this is a demo shell script   //除了上方的#号键其余的#号键都表示改行是注释信息
cd /boot
echo "当前位于:"
pwd
echo "其中以vml开头的文件包括:"
ls -lh vml*

Then the program how to implement it? First, we can use the source directly or sh commands executed directly (note the difference !!!), or need to add execute permissions, and then executed.

[root@localhost shell]# ls -l
总用量 16
-rw-r--r--. 1 root root 134 11月 25 19:59 demo.sh   //没有可执行权限
-rwxr-xr-x. 1 root root 274 11月 25 18:58 state.sh
-rwxr-xr-x. 1 root root 208 11月 25 19:17 sujiaju.sh
-rwxr-xr-x. 1 root root 345 11月 25 19:11 welcome.sh
[root@localhost shell]# source demo.sh 
当前位于:
/boot
其中以vml开头的文件包括:
-rwxr-xr-x. 1 root root 5.7M 11月 14 11:41 vmlinuz-0-rescue-a33ccb7775134b0e83e04555d56fedb7
-rwxr-xr-x. 1 root root 5.7M 8月  23 2017 vmlinuz-3.10.0-693.el7.x86_64
[root@localhost boot]# cd -
/root/shell
[root@localhost shell]# sh demo.sh 
当前位于:
/boot
其中以vml开头的文件包括:
-rwxr-xr-x. 1 root root 5.7M 11月 14 11:41 vmlinuz-0-rescue-a33ccb7775134b0e83e04555d56fedb7
-rwxr-xr-x. 1 root root 5.7M 8月  23 2017 vmlinuz-3.10.0-693.el7.x86_64
[root@localhost shell]# 

After practice operation, you can make the following summary: no shell script file execute permissions can be performed by source or sh commands, but there is a path switching operation will switch to this source path after the command is executed, and the sh command does not, and files directly perform there is no sufficient authority, you need to change the permissions before implementation.

Fourth, pipes and redirection

During operation the majority of the shell are in the background, without user intervention, so learn extraction, filtration execution information is important, the following description of two shell environment I / O operations: a conduit redirection.

4.1 Pipeline Operation

As a result the front or rear input command processing target, in the same command line may be used a plurality of conduits symbols.

A look at the following example:

[root@localhost ~]# grep -v "/sbin/nologin$" /etc/passwd  //将系统中允许登录的用户检索出来
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
lokott:x:1000:1000:lokott:/home/lokott:/bin/bash
[root@localhost ~]# tail /etc/passwd | grep "/sbin/nologin$"  | wc -l //统计文件passwd中最后10行的不 9                                                                     允许登录的用户总数

4.2 Redirection

The user through the process of the operating system to process information in the following categories interactive device file

  • Standard input: keyboard, file number 0
  • Outputs: monitor, document number 1
  • Standard error: monitor, document number 2

In actual Linux system maintenance, may change the input, the output direction of the content, rather than the default standard input and output devices (keyboard and monitor), this operation is called "redirect."

1) Input redirection

Redirect input refers to the way in order to receive input from the keyboard instead of the default specified file, instead of waiting for keyboard input. Redirection inputs "<" operator.

As we use the interaction-free operation for a user to set an example login password:

[root@localhost shell]#  echo "111111" |passwd --stdin zhangsan  //一般方法
更改用户 zhangsan 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@localhost shell]# vim pass.txt 
[root@localhost shell]# cat pass.txt 
123456
[root@localhost shell]# passwd --stdin zhangsan < pass.txt   //使用重定向输入会失败
更改用户 zhangsan 的密码 。
passwd: 鉴定令牌操作错误

According to the above operation, we found that fails, it is not our order wrong, but we Linux system, SELinux at play:

[root@localhost shell]# tail /var/log/messages           //查看系统日志文件 
Nov 26 14:10:09 localhost python: SELinux is preventing /usr/bin/passwd from ioctl access on the file /root/shell/pass.txt.#012#012*****  Plugin catchall (100. confidence) suggests   **************************#012#012If you believe that passwd should be allowed ioctl access on the pass.txt file by default.#012Then you should report this as a bug.#012You can generate a local policy module to allow this access.#012Do#012allow this access for now by executing:#012# ausearch -c 'passwd' --raw | audit2allow -M my-passwd#012# semodule -i my-passwd.pp#012

And then view the tail /var/log/audit/audit.log content will find a password failed to modify report information. Therefore, we need to turn off SELinux, and then executed.

The following error message:

type=USER_CHAUTHTOK msg=audit(1574750006.593:666): pid=53587 uid=0 auid=0 ses=11 subj=unconfined_u:unconfined_r:passwd_t:s0-s0:c0.c1023 msg='op=PAM:chauthtok grantors=? acct="zhangsan" exe="/usr/bin/passwd" hostname=localhost.localdomain addr=? terminal=pts/2 res=failed'
type=USER_CHAUTHTOK msg=audit(1574750008.850:667): pid=53587 uid=0 auid=0 ses=11 subj=unconfined_u:unconfined_r:passwd_t:s0-s0:c0.c1023 msg='op=change password id=1001 exe="/usr/bin/passwd" hostname=localhost.localdomain addr=? terminal=pts/2 res=failed'

After closing the results:

[root@localhost shell]# setenforce 0
[root@localhost shell]# passwd --stdin zhangsan < pass.txt
更改用户 zhangsan 的密码 。
passwd:所有的身份验证令牌已经成功更新。

(2) redirect the output

">" - said cover

">>" - said the additional

Example:

[root@localhost shell]# uname -p > kernel.txt
[root@localhost shell]# cat kernel.txt 
x86_64
[root@localhost shell]# uname -p >> kernel.txt
[root@localhost shell]# cat kernel.txt 
x86_64
x86_64
[root@localhost shell]# uname -p > kernel.txt
[root@localhost shell]# cat kernel.txt 
x86_64

(3) error redirection

The error information stored in the specified file, rather than directly to the display screen. Error redirection "2>" operator.

Example:

[root@localhost shell]# tar jcf /nonedir/ex.bz2 /etc/ 2> error.log
[root@localhost shell]# cat error.log 
tar: 从成员名中删除开头的“/”
tar (child): /nonedir/ex.bz2:无法 open: 没有那个文件或目录
tar (child): Error is not recoverable: exiting now

Redirection and piping operation is very commonly used in shell environment, we need to master.

Guess you like

Origin blog.51cto.com/14557673/2453655