shell script basis

What is shell

  The most complete history Tutorial: http://c.biancheng.net/shell/base/

  A shell is a command interpreter, he is the outermost layer of the operating system, is responsible for a user interacts with the system kernel, the command entered by the user to turn a system, then the result of the input, output feedback to the users of the system

Popular terms shell is put Unix/Linuxunder the command into a file set to perform, it is not only a connected user and the Linux kernel programs, but also a management scripting language Linux command interpreter, it is the use of shell functions written a program, statements in the shell document and the result is fed back to the user, the shell provides arrays, loops, and logic condition judgment important functions.

We entered the command computer is not set up anything else, it requires a program to translate into binary own recognition, while the results back to us

  shell species: https://www.cnblogs.com/leilong/p/9206487.html

Classification of programming languages

  • Low-level language (machine-oriented language):

   Machine language: a binary language

   Assembly: symbolic language, the mnemonic opcodes instead, i.e. instead of binary symbol languages

  • High-level language (for human-readable language):

     Static language: compiled languages ​​c, c ++, java

     Dynamic languages: interpreted language php, shell, python, Perl

         gcc compiler :( interpreter) speaks of human understanding of language into machine language understood

Programming language

Interpreted language

Language Category

What is a shell script

通俗来讲shell脚本就是把Unix/Linux下的命令放入一个文件内集中执行,它不仅是一个连接用户和Linux内核的程序,又是一个管理Linux的脚本语言的命令解释器/bin/bash和/bin/sh,它是利用了shell功能写成的一个程序,将shell语句放入文件然后将结果反馈给用户,shell提供了数组、循环、条件以及逻辑判断等重要功能。

1. 书写脚本

cat /server/scripts/shell.sh
#!/bin/bash   ----> 指定脚本默认使用的命令解释器 第一行
echo "hello word"


#执行结果
[root@m01 /server/scripts]# sh shell.sh 
hello word

标注:

  • 语法正确,文件有+x执行权限chmod +x shell.sh
  • 以 # 开头,整行注释不执行
  • 文件以 .sh 结尾
  • shell 脚本的执行方式

2. shell 脚本执行方式

  • /root/shell.sh 或者 ./shell.sh (要有+x执行权限)usr/bin/bash
  • bash shell.sh 或 sh shell.sh (可以不对脚本文件添加权限)
  • sh < shell.sh (重定向)
  • source 或 . 替你在当前环境中 执行1次脚本 ,source 一般用来实现include 功能 或配置环境变量 别名后生效. /etc/init.d/functions

注:在/etc/shells文件系统中给出系统已知的 shell :/bin/sh、/bin/bash、/usr/bin/sh、/usr/bin/bash

shell 变量及运用

shell 变量

  变量是shell 传递数据的一种方法。变量是用来代表每个值的符号名。就好比我们小时候数学里的 变量和变量值,比如y=x+1 那么 相当与x+1的结果赋值给了y ,当然我么也可以不断给y赋值,之后赋值的y会把之前的值给替换掉。

变量的命名规则:

  1. 命令只能使用英文字母,数学和下划线,首个字母不能以数字开头。

  2. 中间不能有空格,可以使用下划线( _ )

  3. 不能使用标点符号。

  4. 不能使用bash里的关键字( 可用help帮助 )

  5. 等号 = 用于为变量分配值,等号两边不能有空格

  6. 变量存储的数据类型是整数类型和字符串类型

  7. 在赋值时建议用引号括起来。因为如果字符串中存在空格符号,需要使用单引号或双引号。

  8. 要对变量进行调用,在变量前加 $ 符号

变量的分类:

  1. 用户自定义变量

  2. 环境变量:主要保存和系统操作环境相关的数据。

  3. 位置参数变量:主要是用来向脚本当中传递参数或数据,变量名不能自定义,变量作用是固定的。

  4. 预定义变量:是Bash 中已经自定义好的变量,变量名不

  5. 能自定义,变量作用也是自定义好的。

按照变量作用域可以分成 2 类:全局变量和局部变量

局部变量 是 shell 程序内部定义的,其作用仅限于定义它的程序,对其他程序不可见。包括:用户自定义变量、位置变量和预定义变量。

全局变量 是环境变量,其值不随 shell 脚本的执行结束而消失。

内置环境变量

 

环境变量 含义
PATH 指定命令的搜索路径
HOME 指定用户的主工作目录(即用户登录系统时,默认路径)
HISTSIZE 指保存历史命令记录的条数
LOGNAME 指当前用户的登录名
HOSTNAME 指主机的名称,程序如果要用到主机名,通常从这个环境取得
SHELL 指当前用户时那种Shell
LANG/LAGUGE 语言相关的环境变量,使用多种语言的用户可以修改此环境变量。/etc/sysconfig/i8n
MAIL 指当前用户的邮件存放目录
PS1 命令基本提示符,root用户是#,普通用户是$
PS2 附属提示符,默认是 ">"
UID 记录用户的UID信息
HISTCONTROL 控制history命令是否记录以空格开头的命令

Guess you like

Origin www.cnblogs.com/Mercury-linux/p/12153548.html