目录
1.变量值的定义
name=abc
名称如何定义:以下划线 数字和字母的组合,须以字母或下划线开头,见名知意
等号两边不允许有空格
abin_age 第一种书写方式
ABIN_AGE 系统的书写方式
Abin_Age 大驼峰语法
abin_Age 小驼峰语法
1)数值的定义
name_age=1231231 --必须连续的数字
2)字符串的定义
name="abc" --字符串必须加双引号,不知道加什么就加 双引号 解析变量
'' 单引号 所见即所得 不解析变量
3)命令的定义
time=`date` --反引号解析命令
time=$() $()解析命令
PS:注意命令的定义是把结果复制给了变量 如果想改变结果 再次赋值
[root@abin ~]# date
Sat Jun 13 12:10:08 CST 2020
[root@abin ~]# time=`date`
[root@abin ~]# echo $time
Sat Jun 13 12:10:15 CST 2020
[root@abin ~]#
2.Shell特殊位置变量
1)$0 代表了脚本的名称 如果全路径执行 则脚本名称带全路径
使用方法 给用户提示
[root@abin /server/scripts]# cat test.sh
#!/bin/bash
##create abin 2020-6-12 QQ:1781668
echo $0
[root@abin /server/scripts]# sh test.sh
test.sh
[root@abin /server/scripts]#
2)$n 脚本的第n个参数 0被脚本名称占用 从1开始 $9以后要加{}视为一个整体
[root@abin /server/scripts]# cat test.sh
#!/bin/bash
##create abin 2020-6-12 QQ:1781668
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11}
[root@abin /server/scripts]# sh test.sh {a..z}
a b c d e f g h i j k
[root@abin /server/scripts]#
3)$# 获取脚本传参的总个数
控制脚本传参的个数 [ $# -ne 2 ] && echo "请输入两个参数" && exit
[root@abin /server/scripts]# cat test.sh
#!/bin/bash
##create abin 2020-6-12 QQ:1781668
[ $# -ne 2 ] && echo "请输入两个参数" && exit
name=$1
age=$2
echo $name $age
[root@abin /server/scripts]# sh test.sh abin
请输入两个参数
[root@abin /server/scripts]# sh test.sh abin 18
abin 18
[root@abin /server/scripts]#
4)$* 获取脚本所有的参数 不加引号和$@相同 加上双引号 则把参数视为一个参数$1$2$2
[root@abin /server/scripts]# cat test.sh
#!/bin/bash
##create abin 2020-6-12 QQ:1781668
name=$1
age=$2
echo $name $age
echo $*
echo $@
[root@abin /server/scripts]# sh test.sh abin 18 boy
abin 18
abin 18 boy
abin 18 boy
[root@abin /server/scripts]#
5)$@ 获取脚本所有的参数 不加双引号和$*相同,加上双引号则把参数视为独立的参数 $1 $2 $3
$*和$@在脚本中相同 在循环体内不同
[root@abin /server/scripts]# set -- "I am" abin 18 boy set为模拟传参
[root@abin /server/scripts]# echo $*
I am abin 18 boy
[root@abin /server/scripts]# echo $@
I am abin 18 boy
[root@abin /server/scripts]# for i in $*;do echo $i;done
I
am
abin
18
boy
[root@abin /server/scripts]# for i in $@;do echo $i;done
I
am
abin
18
boy
[root@abin /server/scripts]# for i in "$*";do echo $i;done
I am abin 18 boy
[root@abin /server/scripts]# for i in "$@";do echo $i;done
I am
abin
18
boy
[root@abin /server/scripts]#
6)$? 获取上一条命令的返回结果 0为成功 非0为失败 *****常用
[root@abin /server/scripts]# cat test.sh
#!/bin/bash
##create abin 2020-6-12 QQ:1781668
[ $# -ne 2 ] && echo "请输入两个参数" && exit 50 --$?自定义输出
name=$1
age=$2
echo $name $age
echo $*
echo $@
[root@abin /server/scripts]# sh test.sh
请输入两个参数
[root@abin /server/scripts]# echo $? --自定义输出
50
[root@abin /server/scripts]#
7)$$ 获取脚本的PID
echo $$ > /tmp/nginx_log.pid
[root@abin /server/scripts]# cat test.sh
#!/bin/bash
##create abin 2020-6-12 QQ:1781668
echo $$ > /tmp/nginx_log.pid
sleep 60
[root@abin /server/scripts]# sh test.sh & --&表示加入后台运行
[1] 3145
[root@abin /server/scripts]# ps aux|grep test.sh
root 3145 0.0 0.1 113176 1400 pts/2 S 14:58 0:00 sh test.sh
root 3150 0.0 0.0 112704 976 pts/2 R+ 14:59 0:00 grep --color=auto test.sh
[root@abin /server/scripts]# kill -9 `cat /tmp/nginx_log.pid` --通过获取进程PID号杀掉进程
-bash: kill: (3145) - No such process
[1]+ Done sh test.sh
[root@abin /server/scripts]# ps aux|grep test.sh
root 3162 0.0 0.0 112704 980 pts/2 R+ 14:59 0:00 grep --color=auto test.sh
[root@abin /server/scripts]#
8)$! 获取上一个在后台运行脚本的PID 调试使用
9)$_ 获取命令行最后一个参数 相当于ESC. 错误值也返回
[root@abin /server/scripts]# ll /etc/passwd /etc/hosts /etc/test
ls: cannot access /etc/test: No such file or directory
-rw-r--r--. 1 root root 371 May 9 17:10 /etc/hosts
-rw-r--r-- 1 root root 1201 Jun 2 08:31 /etc/passwd
[root@abin /server/scripts]# echo $_
/etc/test
[root@abin /server/scripts]#
3.脚本传参的三种方式
第一种传参方式 赋值
name=$1
age=$2
echo $name $age
第二种传参方式 直接传参
echo $1 $2
第三种传参方式 read
read -p "please input name:" name
echo $name
题:使用read传参的方式 修改 主机名称 abin 和 IP地址 10.0.0.100 sed
1.read -p "请输入要修改的主机名称" 变量名称
2.如何修改主机名称
3.执行查看是否生效
第一步:
read -p "请输入要修改的主机名称:" name
第二步:
hostnamectl set-hostname $name
第三步:
测试 排错
[root@abins /server/scripts]# cat hostname.sh
#!/bin/bash
##create abin 2020-6-13 QQ:1781668
read -p "请输入要修改的主机名称:" name
hostnamectl set-hostname $name
[root@abins /server/scripts]# sh hostname.sh
请输入要修改的主机名称:abin
[root@abins /server/scripts]#
[root@abins /server/scripts]# cat test.sh
#!/bin/bash
##create abin 2020-6-12 QQ:1781668
dir="/etc/hosts"
read -p "请输入一个文件名称:" dir1
echo $dir
echo $dir1
[root@abins /server/scripts]# sh test.sh
请输入一个文件名称:/etc/passwd
/etc/hosts
/etc/passwd
[root@abins /server/scripts]#
二次赋值:(脚本执行由上到下,上面的会被覆盖)
[root@abins /server/scripts]# cat test.sh
#!/bin/bash
##create abin 2020-6-12 QQ:1781668
dir="/etc/hosts"
read -p "请输入一个文件名称:" dir
echo $dir
echo $dir
[root@abins /server/scripts]# sh test.sh
请输入一个文件名称:/etc/passwd
/etc/passwd
/etc/passwd
[root@abins /server/scripts]#
4.编写第一个shell脚本
三种执行脚本的方式
1)使用命令解释器执行 bash sh
2)使用全路径执行脚本 注意:需要加执行权限
3)使用source或者. 执行脚本
5.环境变量
1)什么是环境变量
x=1 y=x+1 x y===变量名称 等号右边==变量值 = 赋值
右边一堆内容,使用一个名字来代替称为环境变量
2)环境变量分类
1.环境变量(全局变量) 2.普通变量(局部变量)
按照生命周期划分 两类
永久的 需要在环境变量文件中配置 /etc/porfile 永久生效
临时的 使用export声明即可 临时生效 重启失效
test=abin 只对当前的bash生效
export test=abin 对(当前)所有的bash生效
写入/etc/profile针对所有用户和bash生效
补充:如何查看变量
env
set
变量生效顺序
/etc/profile 开机所有用户加载此文件
. ~/.bashrc
.bash_profile
.bashrc
6.环境变量字符串
字符串切片和长度统计
[root@abin ~]# test="I am abin boy"
[root@abin ~]# echo $test
I am abin boy
[root@abin ~]# echo ${test:0:2}
I
[root@abin ~]# echo ${test:0:3}
I a
[root@abin ~]# echo ${test:2:2}
am
[root@abin ~]# echo ${test:1:3}
am
[root@abin ~]# echo ${test:6:10}
bin boy
[root@abin ~]# echo ${test:5:10}
abin boy
[root@abin ~]#
笔试题:
1)统计以下字符串 输出小于3的字符
如何统计字符串长度
[root@abin ~]# test="I am abin boy"
[root@abin ~]# echo ${#test}
13
[root@abin ~]# echo $test|wc -L
13
[root@abin ~]# expr length $test
13
[root@abin ~]# echo $test|awk '{print length}'
13
[root@abin ~]#第一种方式
[root@abin /server/scripts]# cat for.sh
#!/bin/sh
for i in I am abin boy I am 18
do
[ ${#i} -lt 3 ] && echo $i
done
第二种方式
[root@abin /server/scripts]# echo "I am abin boy I am 18"|xargs -n1|awk '{if(length<3)print}'
第三种方式
[root@abin /server/scripts]# echo "I am abin boy I am 18"|awk '{for(i=1;i<=NF;i++)if(length($i)<3)print $i}'
2)字符串的删除和替换
# 从前往后匹配删除 ##贪婪匹配
[root@abin ~]# url=www.sina.com.cn
[root@abin ~]# echo $url
www.sina.com.cn
[root@abin ~]# echo $url|sed 's#www.##g' --第一种方式
sina.com.cn
[root@abin ~]# echo ${url#*.} --第二种方式
sina.com.cn
[root@abin ~]# echo ${url#*.*.} --贪婪匹配
com.cn
[root@abin ~]# echo ${url##*.} --贪婪匹配
cn
[root@abin ~]#
% 从后往前匹配删除 %%贪婪匹配
[root@abin ~]# echo ${url%.*}
www.sina.com
[root@abin ~]# echo ${url%.*.*}
www.sina
[root@abin ~]# echo ${url%%.*}
www
[root@abin ~]#
替换
[root@abin ~]# echo $url|sed 's#www#bbs#g'
bbs.sina.com.cn
[root@abin ~]# echo ${url/www/bbs}
bbs.sina.com.cn
[root@abin ~]# echo ${url/w/b}
bww.sina.com.cn
[root@abin ~]# echo ${url//w/b}
bbb.sina.com.cn
[root@abin ~]#
7.数值运算
1)expr 整数运算
[root@abins /server/scripts]# expr 1 + 1
2
[root@abins /server/scripts]# expr 1 - 1
0
[root@abins /server/scripts]# expr 1 \* 1
1
[root@abins /server/scripts]# expr 1 / 1
1
2)echo $(()) 整数运算
[root@abins ~]# echo $((10+10))
20
[root@abins ~]# echo $((10-10))
0
[root@abins ~]# echo $((10*10))
100
[root@abins ~]# echo $((10/10))
1
3)$[] 整数运算
[root@abins ~]# echo $[10+10]
20
[root@abins ~]# echo $[10-10]
0
[root@abins ~]# echo $[10*10]
100
[root@abins ~]# echo $[10/10]
1
4)bc 整数运算
[root@abins ~]# echo 10+10.5|bc
20.5
[root@abins ~]# echo 10-10.5|bc
-.5
[root@abins ~]# echo 10*10.5|bc
105.0
[root@abins ~]# echo 10/10.5|bc
0
5)awk python 整数运算
[root@abins ~]# awk 'BEGIN{print 1+1}'
2
[root@abins ~]# awk 'BEGIN{print 1-1}'
0
[root@abins ~]# awk 'BEGIN{print 1*1}'
1
[root@abins ~]# awk 'BEGIN{print 1/1}'
1
[root@abins ~]# echo 10 20|awk '{print $1+$2}'
30
[root@abins ~]# echo 10 20|awk '{print $1-$2}'
-10
[root@abins ~]# echo 10 20|awk '{print $1*$2}'
200
[root@abins ~]# echo 10 20|awk '{print $1/$2}'
0.5
[root@abins ~]#
[root@abins ~]# python
Python 2.7.5 (default, Apr 11 2018, 07:36:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1+1
2
>>> 1+1.5
2.5
>>>
PS:ctrl+z退出
8.判断输入的是否是整数
[root@abins /server/scripts]# cat test.sh
#!/bin/bash
##create abin 2020-6-12 QQ:1781668
read -p "请输入第一个数:" num
expr 1 + $num >/dev/null 2>&1
[ $? -ne 0 ] && echo "请输入整数" && exit 1
read -p "请输入第二个数:" num1
expr 1 + $num1 >/dev/null 2>&1
[ $? -ne 0 ] && echo "请输入整数" && exit 2
echo "$num+$num1=$[$num+$num1]"
[root@abins /server/scripts]# sh test.sh
请输入第一个数:10.5
请输入整数
[root@abins /server/scripts]# sh test.sh
请输入第一个数:10
请输入第二个数:20
10+20=30
[root@abins /server/scripts]#
题:做一个计算器算加减乘除 用三种传参的方式完成
[root@abins /server/scripts]# cat test.sh
#!/bin/bash
##create abin 2020-6-12 QQ:1781668
echo "第一种方法"
echo "$1+$2=$[$1+$2]"
echo "$1-$2=$[$1-$2]"
echo "$1*$2=$[$1*$2]"
echo "$1/$2=$[$1/$2]"
echo "第二种方法"
num1=$1
num2=$2
echo "$num1+$num2=$[$num1+$num2]"
echo "$num1-$num2=$[$num1-$num2]"
echo "$num1*$num2=$[$num1*$num2]"
echo "$num1/$num2=$[$num1/$num2]"
echo "第三种方法"
read -p "请输入两个数字:" num1 num2
echo "$num1+$num2=$[$num1+$num2]"
echo "$num1-$num2=$[$num1-$num2]"
echo "$num1*$num2=$[$num1*$num2]"
echo "$num1/$num2=$[$num1/$num2]"
[root@abins /server/scripts]# sh test.sh 10 20
第一种方法
10+20=30
10-20=-10
10*20=200
10/20=0
第二种方法
10+20=30
10-20=-10
10*20=200
10/20=0
第三种方法
请输入两个数字:10 20
10+20=30
10-20=-10
10*20=200
10/20=0
[root@abins /server/scripts]#