准备机器
shell 10.0.0.21 172.16.1.21
2.解释器类型与执行方式
bash是centos中的默认解释器
[root@shell ~]# mkdir -p /server/scripts #创建脚本指定目录
[root@shell ~]# cd /server/scripts
[root@shell scripts]# vim test_01.sh #编写一个简单脚本
#!/bin/bash
# author: lcx
# version: 1.0
# createtime: 2019-07-15
# 输出一段文字
echo hello Linux!
#执行的方法
[root@shell scripts]# sh test_01.sh #sh执行 最常用的使用方式
hello Linux!
#bash执行
[root@shell scripts]# bash test_01.sh
hello Linux!
[root@shell scripts]# cat test_01.sh |bash #适用于执行多个脚本
hello Linux!
#绝对路径执行
[root@shell scripts]# chmod +x test_01.sh #添加执行权限
[root@shell scripts]# /server/scripts/test_01.sh
hello Linux!
#输入重定向执行
[root@shell scripts]# sh < test_01.sh
hello Linux!
#用“.” 和source执行
[root@shell scripts]# . test_01.sh
hello Linux!
[root@shell scripts]# source test_01.sh
hello Linux!
[root@shell scripts]# cat *.sh
#!/bin/bash
# author: lcx
# version: 1.0
# createtime: 2019-07-15
# 输出一段文字
echo hello Linux!
#!/bin/bash
# author: lcx
# version: 1.0
# createtime: 2019-07-15
# 输出一段文字
echo hello word!
[root@shell scripts]# cat *.sh|bash
hello Linux!
hello word!
#创建软链接执行
[root@shell scripts]# ln -LfT test_01.sh /usr/local/bin/test.sh
[root@shell scripts]# /usr/local/bin/test.sh
hello Linux!
[root@shell scripts]# test.sh
hello Linux!
3. 父shell和子shell
父shell中的一些环境变量,在子shell可以以看到
而子shell中的变量,在父shell中看不到
bash
ps -auxf #查看进程
退出当前shell exit
4.Shell脚本书写注意格式
-
脚本放在指定目录
-
创建脚本,后缀是 “.sh”
-
在第一行顶格添加命令解释器的声明 "#!/bin/bash"
-
添加作者 时间及版权信息
-
养成良好习惯,添加注释
-
编写脚本
5.变量基础
5.1变量
值可变的量,称为变量
变量名=变量值 常说的变量,一般指变量名
字母数字下划线,不能以数字开头
#普通变量
[root@shell scripts]# name=linux #赋值
[root@shell scripts]# echo $name
linux
[root@shell scripts]# Name1=shell #大写可以
[root@shell scripts]# name_1=shell01 #字母开头可以
[root@shell scripts]# _1name=shell02 #下划线开头可以
[root@shell scripts]# 1_name=shell03 #数字开头不可以
-bash: 1_name=shell03: command not found
5.2 环境变量和普通变量
登录式shell
非登录式shell
[root@shell scripts]# declare -x name=lcx
[root@shell scripts]# echo $name
lcx
[root@shell scripts]# bash
[root@shell scripts]# echo $name
5.3临时变量和永久变量
如果按照变量的生存周期来划分的话,Linux变量可以分为两类:
- 永久变量:需要修改变量配置文件,使得变量永久生效
- 临时变量,使用export命令或者直接在当前shell中赋值的变量
在使用history的时候,加入脚本中,执行什么也不会输出。验证时候验证失败。暂时将其当做现象,课下进行深入研究
set -o history #添加此条命令即可
echo hello Linux!
history
5.4 变量练习
5.5 环境变量文件的加载顺序
/etc/profile ===> ~/.bash_profile ===> ~/.bashrc ===> /etc/bashrc
6. 课堂作业
7. shell特殊变量
[root@shell scripts]# cat test_03.sh
#!/bin/bash
# author: lcx
# date: 2019-07-15
# version: 1.0
echo $0
echo $[1-10]
echo $#
echo $*
echo $@
[root@shell scripts]# sh test_03.sh {1..10}
test_03.sh
-9
10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
8.修改/etc/vimrc文件
[root@shell scripts]# cat /etc/vimrc
set nocompatible
set nu
set history=100
filetype on
filetype plugin on
filetype indent on
set autoread
set mouse=c
syntax enable
set cursorline
hi cursorline guibg=#00ff00
hi CursorColumn guibg=#00ff00
set foldenable
set foldmethod=manual
set foldcolumn=0
setlocal foldlevel=3
set foldclose=all
nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4
set smarttab
set ai
set si
set wrap
set sw=4
set wildmenu
set ruler
set cmdheight=1
set lz
set backspace=eol,start,indent
set whichwrap+=<,>,h,l
set magic
set noerrorbells
set novisualbell
set showmatch
set mat=4
set hlsearch
set ignorecase
set encoding=utf-8
set fileencodings=utf-8
set termencoding=utf-8
set smartindent
set cin
set showmatch
set guioptions-=T
set guioptions-=m
set vb t_vb=
set laststatus=4
set pastetoggle=<F9>
set background=dark
highlight Search ctermbg=black ctermfg=white guifg=white guibg=black
autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()"
func SetTitle()
if expand("%:e") == 'sh'
call setline(1, "#!/bin/bash")
call setline(2, "##############################################################")
call setline(3, "# File Name: ".expand("%"))
call setline(4, "# Version: V1.0")
call setline(5, "# Author: lcx") #名字随意
call setline(6, "# Organization: www.oldboyedu.com")
call setline(7, "##############################################################")
endif
endfunc
效果
9.进程特殊变量 $? $$ $! $_
9.1 $? 的用法
显示最后命令的退出状态。0表示没有错误,非0表明有错误。此变量最常用
默认情况下,无论脚本内部命令是否执行成功,脚本均认为是正确执行,返回0如果想控制脚本在内部命令执行错误的情况下,脚本则被认为是执行失败,可以控制返回值为非0值
[root@shell scripts]# cat test_04.sh
#!/bin/bash
##############################################################
# File Name: test_04.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
echo "hello lcx"
echo $?
cat ~/access.txt
echo $?
exit 100
[root@shell scripts]# sh test_04.sh
hello lcx
0
#!/bin/bash
# author: lcx
# version: 1.0
# createtime: 2019-07-15
# 输出一段文字
set -o history
echo hello Linux!
history
0
[root@shell scripts]# echo $?
100
# if [ $? -ne 0 ]; then exit 100; fi
[root@shell scripts]# cat test_04.sh
1 #!/bin/bash
2 ##############################################################
3 # File Name: test_04.sh
4 # Version: V1.0
5 # Author: lcx
6 # Organization: www.oldboyedu.com
7 ##############################################################
8 echo "hello lcx"
9 echo $?
10 cat ~/access.txt
11 if [ $? -ne 0 ]; then exit 100; fi #如果返回值不等于0,则显示100
[root@shell scripts]# sh test_04.sh
hello lcx
0
#!/bin/bash
# author: lcx
# version: 1.0
# createtime: 2019-07-15
# 输出一段文字
set -o history
echo hello Linux!
history
0
[root@shell scripts]# echo $? #结果正确
0
9.2 $$ 的用法
显示脚本运行的当前进程ID号,此变量不常用,了解即可
[root@shell scripts]# cat test_05.sh
#!/bin/bash
##############################################################
# File Name: test_05.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
echo $$
echo $$ >/tmp/test_05.pid
sleep 20
[root@shell scripts]# sh test_05.sh &
[1] 14586
[root@shell scripts]# 14586
[root@shell scripts]# ps -ef|grep test_05.sh
root 14586 14268 0 15:55 pts/3 00:00:00 sh test_05.sh
root 14589 14268 0 15:56 pts/3 00:00:00 grep --color=auto test_05.sh
[root@shell scripts]# kill -9 `cat /tmp/test_05.pid`
[root@shell scripts]# ps -ef|grep test_05.sh
root 14592 14268 0 15:56 pts/3 00:00:00 grep --color=auto test_05.sh
[1]+ Killed sh test_05.sh
9.3 $! 的用法
之前运行脚本的最后一个参数,此变量最不常用,了解即可
[root@shell scripts]# sh test_05.sh &
[1] 14703
[root@shell scripts]# 14703
[root@shell scripts]# sh test_05.sh &
[2] 14705
[root@shell scripts]# 14705
[root@shell scripts]# sh test_05.sh &
[3] 14707
[root@shell scripts]# 14707
[root@shell scripts]# sh test_05.sh &
[4] 14709
[root@shell scripts]# 14709
[root@shell scripts]# ps -ef|grep test_05.sh
root 14703 14268 0 16:03 pts/3 00:00:00 sh test_05.sh
root 14705 14268 0 16:03 pts/3 00:00:00 sh test_05.sh
root 14707 14268 0 16:03 pts/3 00:00:00 sh test_05.sh
root 14709 14268 0 16:03 pts/3 00:00:00 sh test_05.sh
root 14716 14268 0 16:03 pts/3 00:00:00 grep --color=auto test_05.sh
[root@shell scripts]# echo $!
14709
9.4 $_ 的用法
之前运行脚本的最后一个参数,此变量最不常用,了解即可
[root@shell scripts]# cat test_03.sh
#!/bin/bash
# author: lcx
# date: 2019-07-15
# version: 1.0
#echo $0
#echo $[1-10]
#echo $#
#echo $*
#echo $@
for i in "$*";do echo $i;done
for i in "$@";do echo $i;done
[root@shell scripts]# sh test_03.sh a b c
a b c
a
b
c
[root@shell scripts]# echo $_
c
[root@shell scripts]# sh test_03.sh 123 456 789
123 456 789
123
456
789
[root@shell scripts]# echo $_
789
10.shell变量子串
[root@shell scripts]# url=www.linuxcx.cn
[root@shell scripts]# echo $url
www.linuxcx.cn
[root@shell scripts]# echo $urlx
[root@shell scripts]# echo ${url}x
www.linuxcx.cnx
[root@shell scripts]# echo "$url"x
www.linuxcx.cnx
[root@shell scripts]# echo ${#url}
14
[root@shell scripts]# echo ${url:5}
inuxcx.cn
[root@shell scripts]# echo ${url:4}
linuxcx.cn
[root@shell scripts]# echo ${url:0}
www.linuxcx.cn
[root@shell scripts]# echo ${url:4:7}
linuxcx
[root@shell scripts]# test=abcABCabcABC
[root@shell scripts]# echo ${test#abc}
ABCabcABC
[root@shell scripts]# echo ${test#*c}
ABCabcABC
[root@shell scripts]# echo ${test##*c}
ABC
[root@shell scripts]# echo ${test%c*}
abcABCab
[root@shell scripts]# echo ${test%%c*}
ab
[root@shell scripts]# echo ${test##c*}
abcABCabcABC
[root@shell scripts]# echo ${test#*c*}
ABCabcABC
[root@shell scripts]# echo ${test/abc/def}
defABCabcABC
[root@shell scripts]# echo ${test//abc/def}
defABCdefABC
获取变量字符串长度的五种方法:
[root@shell scripts]# echo ${url}
www.linuxcx.cn
[root@shell scripts]# echo ${#url}
14
[root@shell scripts]# expr length $url
14
[root@shell scripts]# echo ${url}|wc -L
14
[root@shell scripts]# echo $url|awk '{print length}'
14
[root@shell scripts]# echo $url|awk '{print length($0)}'
14
11.作业
1、 请使用shell脚本打印下面语句中字符数不小于6的单词
I am teacher oldchang and I like eating and sleeping
#第一种方法
[root@shell scripts]# cat ge.sh
#!/bin/bash
##############################################################
# File Name: ge6.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
for w in I am teacher oldchang and I like eating and sleeping
do
echo $w |xargs -n1|egrep "^[a-Z]{6,}"
done
[root@shell scripts]# sh ge6.sh
oldchang
teacher
eating
sleeping
#第二种方法
[root@shell scripts]# cat ge01.sh
#!/bin/bash
##############################################################
# File Name: ge01.sh
# Version: V2.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
for w in I am teacher oldchang and I like eating and sleeping
do
[ `echo $w|wc -L` -ge 6 ] && echo $w
done
[root@shell scripts]# sh ge01.sh
oldchang
teacher
eating
sleeping
2、写出shell脚本,通过传参的方式,传入以下内容,并打印下面语句中字符数不小于6的单词
I am teacher oldchang and I like eating and sleeping
#传参方法 $1 $* $@等等
[root@shell scripts]# cat ge.sh
#!/bin/bash
##############################################################
# File Name: ge6.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
for w in $*
do
echo $w |xargs -n1|egrep "^[a-Z]{6,}"
done
[root@shell scripts]# sh ge.sh "I am teacher oldchang and I like eating and sleeping"
teacher
oldchang
eating
sleeping
1.使用脚本传参的方式实现整数的加、减、乘、除、取余、幂运算
[root@shell scripts]# cat bc01.sh
#!/bin/bash
##############################################################
# File Name: bc01.sh
# Version: V2.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
x=$1
y=$2
echo $1+$2=`echo $1+$2|bc`
echo $1-$2=`echo $1-$2|bc`
echo $1*$2=`echo $1*$2|bc`
echo $1/$2=`awk -vx=$x -vy=$y 'BEGIN{print x / y}'`
[root@shell scripts]# sh bc01.sh 2 3
2+3=5
2-3=-1
2*3=6
2/3=0.666667
[root@shell scripts]# sh bc01.sh 55 20
55+20=75
55-20=35
55*20=1100
55/20=2.75
#简易计算器升级版
[root@shell scripts]# cat bc.sh
#!/bin/bash
##############################################################
# File Name: bc.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
x=$1
y=$2
if [ $# -ne 2 ];then
echo "Usage: sh $0 参数必须为2个数值 语法错误!"
exit
fi
if [ $1 -gt $2 ];then
echo "数值对比: [$1] > [$2]"
elif [ $1 -eq $2 ];then
echo "数值对比: [$1] = [$2]"
else [ $1 -le $2 ]
echo "数值对比: [$1] < [$2] "
fi
echo $1\+$2=`awk -vx=$x -vy=$y 'BEGIN{print x + y}'`
echo $1\-$2=`awk -vx=$x -vy=$y 'BEGIN{print x - y}'`
echo $1\*$2=`awk -vx=$x -vy=$y 'BEGIN{print x * y}'`
echo $1\/$2=`awk -vx=$x -vy=$y 'BEGIN{print x / y}'`
[root@shell scripts]# sh bc.sh 8 6
数值对比: [8] > [6]
8+6=14
8-6=2
8*6=48
8/6=1.33333
[root@shell scripts]# sh bc.sh 77 88
数值对比: [77] < [88]
77+88=165
77-88=-11
77*88=6776
77/88=0.875
[root@shell scripts]# sh bc.sh 5
Usage: sh bc.sh 参数必须为2个数值 语法错误!
[root@shell scripts]# sh bc.sh 5 6 7
Usage: sh bc.sh 参数必须为2个数值 语法错误!
[root@shell scripts]# sh bc.sh 7 10
数值对比: [7] < [10]
7+10=17
7-10=-3
7*10=70
7/10=0.7
2.使用脚本变量传参的三种方式,写一个脚本,通过传参方式修改主机名和ip地址
[root@lcx scripts]# cat ip.sh
#!/bin/bash
# author: lcx
# version: 1.0
# createtime: 2019-07-15
# hostname
hostnamectl set-hostname $1
# IP
sed -ri "s#^IPADDR=(.*)#IPADDR='$2'#g" /etc/sysconfig/network-scripts/ifcfg-eth0
sed -ri "s#^IPADDR=(.*)#IPADDR='$3'#g" /etc/sysconfig/network-scripts/ifcfg-eth1
# restart network
systemctl restart network
su -
[root@lcx scripts]# sh ip.sh shell 10.0.0.21 172.16.1.21
Last login: Mon Jul 15 20:14:20 CST 2019 on pts/0
/etc/profile
.bash_profile
.bashrc
/etc/bashrc
[root@shell ~]# hostname -I
10.0.0.21 172.16.1.21
几种显示的格式
[root@shell scripts]# cat test_06.sh
#!/bin/bash
##############################################################
# File Name: test_06.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
line="I am teacher lcx and I like eating and sleeping"
echo $line
echo $1
echo $*
echo $@
[root@shell scripts]# sh test_06.sh "I am teacher lcx and I like eating and sleeping"I am teacher lcx and I like eating and sleeping
I am teacher lcx and I like eating and sleeping
I am teacher lcx and I like eating and sleeping
I am teacher lcx and I like eating and sleeping
#传参——read方式
read -p 只可读的方式显示
[root@shell scripts]# cat test_07.sh
#!/bin/bash
##############################################################
# File Name: test_07.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
read -p "请输入字符串: " line
echo $line
[root@shell scripts]# sh test_07.sh
请输入字符串: hello word
hello word