当前位置:网站首页>Shell脚本系列篇(1) 入门
Shell脚本系列篇(1) 入门
2022-06-27 01:28:00 【liyuanjie8521】
Shell脚本入门
1. Shell概念
Shell是一个命令解释器, 作用是解释执行用户输入的命令及程序等,用户每输入一条命令,Shell就解释执行一条。这种从键盘一输入命令,就可以立即得到回应的对话方式,称为交互的方式。
Shell脚本语言是弱类型语言(无须定义变量的类型即可使用).
查看系统的shell支持
- Centos7.8
$ cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
- ubuntu18.04
$ cat /etc/shells
/bin/sh
/bin/bash
/bin/rbash
/bin/dash
2 Shell脚本语言优势
- Shell脚本语言更适合处理偏操作系统底层的业务.
- Linux正则表达式及三剑客grep、awk、sed等命令.
- 使用Shell更符合Linux运维简单、易用、高效的三大基本原则.
3. 常用操作系统默认Shell
CentOS Linux系统默认的Shell是:
bash查看CentOS Linux系统默认的Shell
# echo $SHELL
# grep root /etc/passwd
1.4 Shell脚本格式
- 查看系统的shell版本
# bash --version
- 升级bash版本
# yum -y update bash
- 脚本开头(第一行) : 在第一行指出由哪个程序解释器来执行脚本内容
#! /bin/bash
脚本注释
- #后面的内容表示注释,用来对脚本进行注释说明
- 注释尽量不要用中文,在脚本中最好也不要有中文
- Shell脚本是从上至下、从左至右依次执行每一行的命令及语句的,即执行完了一个命令后再执行下一个.
- 在执行Shell脚本时,会向系统内核请求启动一个新的进程,以便在该进程中执行脚本的命令及子Shell脚本.
Shell脚本执行方式
1. bash xxx.sh
2. chmod +x xxx.sh 然后./xxx.sh
3. source xxx.sh 使用source或点号读入或加载指定的shell脚本文件。
结论:
通过source或.加载执行过的脚本,由于是在当前Shell中执行脚本,因此在脚本结束之后,脚本中的变量(包括函数)值在当前Shell中依然存在.
sh和bash执行脚本都会启动新的子Shell执行,执行完后退回到父Shell。因此,变量(包括函数)值等无法保留。
在进行Shell脚本开发时,如果脚本中有引用或执行其他脚本的内容或配置文件的需求时,最好用.或source先加载该脚本或配置文件,处理完之后,再将它们加载到脚本的下面,就可以调用source加载的脚本及配置文件中的变量及函数等内容了.
- 使用sh执行脚本会导致当前Shell无法获得变量值。
1.5 Shell脚本开发基本规范和习惯
shell脚本规范化格式如下
#!/bin/bash
set +o posix
#: <<'COMMENT'
#'''
#@File : xxxxx.sh
#@Time : 2021/12/20 14:03:14
#@Modify : 2021/12/20 16:13:14
#@Author : Li yuanjie
#@Version : 1.1
#@Contact : [email protected]
#@Desc : Project shell scripts
#'''
#COMMENT
# 调试开关
# set -x
#set -euxo pipefail
## -------------------------------------------------------------------------
export PATH=/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin
export TERM="xterm-256color"
export WORKDIR="$(cd $(dirname '$0') && pwd )"
cd "${WORKDIR}" || exit 1
## -------------------------------------------------------------------------
info() {
date +"%F %T $( tput bold ; tput setaf 2)Info: [email protected]$( tput sgr0)"
}
warn() {
date +"%F %T $( tput bold ; tput setaf 3)Warning: [email protected]$( tput sgr0)"
}
error() {
date +"%F %T $( tput bold ; tput setaf 1)Error: [email protected]$( tput sgr0)"
}
err_exit() {
date +"%F %T $( tput bold ; tput setaf 1)Error: [email protected]$( tput sgr0)"
exit 1
}
Shell脚本的第一行是指定脚本解释器
#!/bin/bashShell脚本的开头会加版本,版权等信息
在Shell脚本中尽量不用中文.
Shell脚本的命名应以
.sh为扩展名.Shell脚本应存放在固定的路径下.
代码书写习惯
- 成对的符号应尽量一次性写出来,然后退格在符号里增加内容,以防止遗漏.
- 中括号
([])两端至少要有1个空格. - 对于流程控制语句,应一次性将格式写完,再添加内容.
- 通过缩进让代码更易读
- 对于常规变量的字符串定义变量值应加双引号,并且等号前后不能有空格,需要强引用的(指所见即所得的字符引用),则用单引号
(' '),如果是命令的引用,则用反引号. - 脚本中的单引号、双引号及反引号必须为英文状态下的符号.
边栏推荐
- XSS notes (Part 2)
- 乔治·华盛顿大学 : Hanhan Zhou | PAC:多智能体强化学习中具有反事实预测的辅助价值因子分解
- Find the minimum value in the rotation sort array ii[classical Abstract dichotomy + how to break the game left, middle and right are equal]
- JSON parsing, esp32 easy access to time, temperature and weather
- Two days of beautiful butterfly animation
- Systematic analysis of social networks using Networkx: Facebook network analysis case
- Online text digit recognition list summation tool
- The most difficult 618 in history, TCL won the first place in both jd.com and tmall.com shares in the TV industry
- 3線spi屏幕驅動方式
- ML:机器学习工程化之团队十大角色背景、职责、产出物划分之详细攻略
猜你喜欢

持续交付-Blue Ocean 应用

Visual introduction to Matplotlib and plotnine

可视化介绍 Matplotlib 和 Plotnine

Meituan: data management and pit avoidance strategy summarized after stepping on Thunder for several years
![[graduation season] role conversion](/img/4e/aa763455da974d9576a31568fc6625.jpg)
[graduation season] role conversion

理想L9产品力分析:售价45.98万,采用四缸发动机,续航1315公里

Pointer compression for JVM

The listing of Fuyuan pharmaceutical is imminent: the net amount raised will reach 1.6 billion yuan, and hubaifan is the actual controller

Buuctf PWN write UPS (6)

Esp32 add multi directory custom component
随机推荐
Systematic analysis of social networks using Networkx: Facebook network analysis case
Bs-gx-016 implementation of textbook management system based on SSM
Memcached Foundation
leetcode 1143. Longest Commom Subsequence 最长公共子序列(中等)
Database interview questions +sql statement analysis
The most difficult 618 in history, TCL won the first place in both jd.com and tmall.com shares in the TV industry
getReader() has already been called for this request
XSS notes (Part 2)
疫情期间居家办公的总结体会 |社区征文
One click acceleration of Sony camera SD card file copy operation, file operation batch processing tutorial
Memcached foundation 5
Reporting Classes中uvm_report_server的get_severity_count和get_server用法
【毕业季】角色转换
get_ Usage Summary of sequencer
Kept to implement redis autofailover (redisha) 12
Structure the fifth operation of the actual camp module
Keepalived 实现 Redis AutoFailover (RedisHA)14
Topolvm: kubernetes local persistence scheme based on LVM, capacity aware, dynamically create PV, and easily use local disk
Markdown table (consolidated)
Tsinghua & Zhiyuan | cogview2: faster and better text image generation model