Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basics of shell scripts that Linux must master?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Linux must master what is the basis of shell script, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

1. Basic syntax of shell

Shell:

Shell is a command interpreter, which is in the outermost layer of the operating system, which is responsible for talking to the user directly, interpreting the user's input to the operating system, and processing the output results of various operating systems, which are output to the screen and feedback to the user. What is a shell script:

Put Linux commands or statements in a file, through this program file to execute, this program is a shell script or shell program; you can enter system commands and related syntax statements in the script, such as variables, flow control statements, etc., combine them organically to form a powerful shell script. To create a shell program:

(1) create a file containing command and control structures, vim first.sh (2) modify the permissions of the file to make it executable, and chmod + x first.sh (3) detect syntax errors

(4) execute the script. How to execute the shell script:

(1) execute via absolute path, / root/first.sh (executable permissions need to be added x) (2) bash first.sh or sh first.sh (executable permissions may not be added) (3) source first.sh (executable permissions may not be added in the current shell) (4) sh

< cal.sh,cat cal.sh |sh,cat cal.sh |bash(可以不添加可执行权限) 初识 shell 脚本: #!/bin/bash # it is a shell! echo "hello world!" # 第一行:命令解释器类型 # 第二行:注释信息 2. shell 变量及运用 变量设置规则: (1)变量名称通常是大写字母,可以由数字、字母(大小写)和下划线组成。变量名区分大小写,不能以数字开头; (2)a=10,= 的作用是赋值,= 两边不能有空格; (3)变量存储的数据类型是整数型和字符串型; (4)字符串型建议加引号。如果字符串中有空格,要用单引号''或双引号""包含起来; (5)变量调用时,在变量前加 $,$a; (6)如果需要增加变量的值,可以进行变量值的叠加。变量需要用双引号包含"$变量名"或用${变量名}包含。 变量的分类: 按照变量的作用划分: (1)用户自定义变量(字母或下划线开头,不能以数字开头) (2)环境变量:保存和系统操作环境相关的数据 (3)位置参数变量:外汇跟单:www.gendan5.com向脚本中传递参数或数据的,变量名不能自定义,变量的作用是固定的 (4)预定义变量:是 bash 中已经定义好的变量,变量名不能自定义,变量的作用是固定的 按照变量的作用域划分: (1)全局变量:环境变量,值不随 shell 脚本的执行结束而消失,值传递给子 shell 而不传递给父 shell env 命令查看所有全局变量: export 声明全局变量: 添加环境变量: (2)局部变量:是 shell 程序内部定义的,其使用范围仅限于定义它的程序,对其它程序不可见。包括用户自定义变量、位置变量和预定义变量 命令的替换: 使用 $() 或反引号。echo $(pwd) 或 echo `pwd` 命令的嵌套使用: 使用 $($()) 单引号和双引号: 在单引号中,所有的字符(包括特殊字符)都将解释成字符本身成为普通字符; 在双引号中,除了 $、'' 和 \ 以外所有的字符都解释成字符本身。 删除变量: unset 变量名:删除变量 定义变量的配置文件: /etc/profile:系统全局环境和登陆系统的一些配置 /etc/bashrc:shell 全局自定义配置文件,用于自定义 shell /root/.bashrc:用于自定义某个用户的bash /root/.bash_profile:用户单独自定义某个用户的系统环境 插入永久变量:(1)vim /etc/profile;(2)在最后一行写入 export var8=100,保存并退出;(3)source /etc/profile 位置变量: 特殊变量: $* 以一个单字符串显示所有向脚本传递的参数 $# 传递到脚本的参数的个数 $$ 当前进程的进程号 PID $? 显示最后命令的退出状态;0表示没有错误,其他值都表示有错误 $! 后台运行的最后一个进程的进程号 PID 3. 数学运算 expr 命令: (1)对数字的基本计算;做比较时,输出结果为假时为0,1为真;特殊符号用转义符 (2)对字符串的处理 $(()): 运算符 意义 ++ -- 递增 递减 + - !~ 正 负 逻辑与取反 + - * / % 加 减 乘 除 余数 < >

= compare siz

=! = equal and unequal

> >

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report