shell脚本基础

shell脚本基础

一. 变量

1. 变量的定义
  • 变量指的是系统中可改变的量
2. 变量名称注意事项
  • 只能包含字母、数字、下划线,并且不能以数字开头
  • 不应该跟系统中已有的环境变量重名,尽量不要全部使用大写,尽量不要用“_”下划线开头
  • 最好做到见名知义
  • 不能使用程序中的保留字,例如if、for等
3. 变量类型
  • 字符型,字母,下划线,特殊字符
  • 数值型
    • 整型,整数
    • 浮点型,包括小数
  • 布尔型,false(假)和true(真)。
4. 变量操作
  • 设置变量
  • 引用变量
  • 撤销变量
设置变量
[root@SYL4 ~]# a=10


$a 引用变量
[root@SYL4 ~]# echo $a
10


撤销变量
[root@SYL4 ~]# unset a
[root@SYL4 ~]# echo $a

[root@SYL4 ~]# 
4.1 双引号和单引号的区别:
  • 双引号,弱引用
  • 单引号,强引用
[root@SYL4 ~]# a=10
[root@SYL4 ~]# echo "$a"
10
[root@SYL4 ~]# echo $a
10
[root@SYL4 ~]# 

[root@SYL4 ~]# echo "\$a" \表示转义
$a
[root@SYL4 ~]# 
[root@SYL4 ~]# cp anaconda-ks.cfg abc
[root@SYL4 ~]# cp anaconda-ks.cfg abc
cp: overwrite 'abc'? yes
[root@SYL4 ~]# \cp anaconda-ks.cfg abc
[root@SYL4 ~]# /usr/bin/cp anaconda-ks.cfg abc 
[root@SYL4 ~]# 


[root@SYL4 ~]# echo '$a'
$a
[root@SYL4 ~]# 
5. bash变量类型
  • 环境变量,特点,全部大写
  • 本地变量,脚本本身使用 (局部变量),函数中使用
  • 位置变量
  • 特殊变量(bash内置的,用来保存某些特殊数据的变量,也称系统变量)
5.1 本地变量
VAR_NAME=VALUE      //本地变量,作用域为当前shell进程。对当前shell外的其它shell进程,包括当前shell的父shell、子shell进程均无效

local VAR_NAME=VALUE    //局部变量,作用域为当前代码段,常用于函数

变量=一个值
[root@SYL4 ~]# a=10
[root@SYL4 ~]# echo $a
10
[root@SYL4 ~]# bash
[root@SYL4 ~]# echo $a

[root@SYL4 ~]# a=20
[root@SYL4 ~]# echo $a
20
[root@SYL4 ~]# bash
[root@SYL4 ~]# echo $a

[root@SYL4 ~]# exit
exit
[root@SYL4 ~]# echo $a
20
[root@SYL4 ~]# 

[root@SYL4 ~]# pstree
systemd-+-NetworkManager---2*[{NetworkManager}]
        |-VGAuthService
        |-agetty
        |-auditd---{auditd}
        |-crond
        |-dbus-daemon---{dbus-daemon}
[root@SYL4 ~]# pstree
systemd-+-NetworkManager---2*[{NetworkManager}]
        |-VGAuthService
        |-agetty
        |-auditd---{auditd}
        |-crond
        |-dbus-daemon---{dbus-daemon}
        |-firewalld---{firewalld}
        |-irqbalance---{irqbalance}
        |-polkitd---7*[{polkitd}]
        |-rhsmcertd
        |-rsyslogd---2*[{rsyslogd}]
        |-sshd-+-sshd---sshd-+-bash---pstree
        |      |             |-bash---top
        |      |             `-bash---sleep
        |      |-sshd---sshd-+-bash---bash---bash---bash
        |      |             |-bash---top
        |      |             `-bash---sleep
        |      `-sshd---sshd---6*[sftp-server]
        |-sssd-+-sssd_be
        |      `-sssd_nss
        |-systemd---(sd-pam)
        |-systemd-journal
        |-systemd-logind
        |-systemd-udevd
        |-tuned---4*[{tuned}]
        `-vmtoolsd---{vmtoolsd}
[root@SYL4 ~]# 
5.2 环境变量
  • 当前进程和子进程
[root@SYL4 ~]# echo $SHELL
/bin/bash
[root@SYL4 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@SYL4 ~]# echo $HISTSIZE
1000
[root@SYL4 ~]# 

export VAR_NAME=VALUE   //作用域为当前shell进程及其子进程
[root@SYL4 ~]# export aaa=20
[root@SYL4 ~]# echo $aaa
20
[root@SYL4 ~]# bash
[root@SYL4 ~]# echo $aaa
20
[root@SYL4 ~]# bash
[root@SYL4 ~]# echo $aaa
20
[root@SYL4 ~]# 
5.3 bash内建环境变量
PATH
SHELL

UID
[root@SYL4 ~]# echo $UID
0
[root@SYL4 ~]# 
$UID表示当前用户的ID
HISTSIZE

HOME
[root@SYL4 ~]# echo $HOME
/root
[root@SYL4 ~]# useradd tom
[root@SYL4 ~]# su - tom
[tom@SYL4 ~]$ echo $HOME
/home/tom
[tom@SYL4 ~]$ 

PWD
[root@SYL4 ~]# echo $PWD
/root
[root@SYL4 ~]# pwd
/root
[root@SYL4 ~]# 

HISTFILE
[root@SYL4 ~]# echo $HISTFILE
/root/.bash_history //历史所记得位置
[root@SYL4 ~]# 

PS1
[root@SYL4 ~]# echo $PS1
[\u@\h \W]\$
[root@SYL4 ~]# PS1=':\W:\$:'
:~:#:cd /etc/
:etc:#:
:etc:#:bash

[root@SYL4 ~]# 
\u root
@ @
\h SYL4
\W 当前位置
\$ #
5.4 位置变量
$1,$2,$3,....       //用来引用脚本的参数
    shift [num]         //位置变量使用完以后退出,后面的参数向前推进
[root@SYL4 ~]# vim test.sh
[root@SYL4 ~]# chmod +x test.sh 
[root@SYL4 ~]# cat test.sh 
#!/bin/bash

echo $1 $4 $5 $2
[root@SYL4 ~]# ./test.sh 1 2 3 4
1 4 2
[root@SYL4 ~]# ./test.sh 1 2 3 4 5
1 4 5 2
[root@SYL4 ~]# cat test.sh 
#!/bin/bash

echo $1 $2 $4 $5 $2
[root@SYL4 ~]# ./test.sh 1 2 3 4 5
1 2 4 5 2
[root@SYL4 ~]# 
5.5 特殊变量
$#      //是传给脚本的参数个数

[root@SYL4 ~]# vim test.sh 
[root@SYL4 ~]# cat test.sh 
#!/bin/bash

echo $#
[root@SYL4 ~]#
[root@SYL4 ~]# ./test.sh 1 2 3 4 5
5
 


$0      //是脚本本身的名字

[root@SYL4 ~]# vim test.sh 
[root@SYL4 ~]# cat test.sh 
#!/bin/bash  //shebang告诉我们用哪个程序执行

echo $0
[root@SYL4 ~]# ./test.sh
./test.sh
[root@SYL4 ~]# /bin/bash test.sh 
test.sh
[root@SYL4 ~]# 
执行脚本方式:
    test.sh 一个脚本
1. ./test.sh 必须要执行权限
2. /bin/bash test.sh




$!      //是shell最后运行的后台Process的PID

$@      //是传给脚本的所有参数的列表,参数是独立的
$*      //是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个,是一个整体
[root@SYL4 ~]# vim test.sh
[root@SYL4 ~]# cat test.sh 
#!/bin/bash

echo $*
[root@SYL4 ~]# ./test.sh 1 2 4 5 7 8
1 2 4 5 7 8
[root@SYL4 ~]# vim test.sh 
[root@SYL4 ~]# cat test.sh 
#!/bin/bash

echo $@
[root@SYL4 ~]# ./test.sh 1 2 4 5 7 8
1 2 4 5 7 8
[root@SYL4 ~]# 




$$      //是脚本运行的当前进程ID号

$?      //是显示上条命令的退出状态,0表示没有错误,其他表示有错误
[root@SYL4 ~]# ./test.sh 1 2 4 5 7 8
1 2 4 5 7 8
[root@SYL4 ~]# echo $?
0
[root@SYL4 ~]# 
5.6 只读变量(常量)
readonly VAR_NAME=VALUE     //不能修改值,不能销毁,只能等shell进程终止时随之消亡

[root@SYL4 ~]# readonly a=10
[root@SYL4 ~]# echo $a
10
[root@SYL4 ~]# a=20
bash: a: readonly variable
[root@SYL4 ~]# unset a
bash: unset: a: cannot unset: readonly variable
[root@SYL4 ~]# 

二. 脚本基础

1. 脚本定义
  • 按实际需要,结合命令流程控制机制实现的源程序。说白点就是命令的堆砌。

  • 手动执行的命令放在文件中,配合流程控制机制,形成一个文件

  • windows中的批处理以.bat结尾,.bat重头执行到结尾

2. 程序返回值
2.1 程序执行以后有两类返回值:
  • 程序执行的结果
  • 程序状态返回代码(0-255)
    • 0:正确执行
    • 1-255:错误执行,1、2、127系统预留,有特殊意义
echo $?
1 条件不成立
2 文件不存在
127 命令不存在

[root@SYL4 ~]# ls
abc  anaconda-ks.cfg  scripts  test.sh
[root@SYL4 ~]# echo $?
0
[root@SYL4 ~]# [ 2 -ne 2 ]
[root@SYL4 ~]# echo $?
1
[root@SYL4 ~]# ls adhbjgh test.sh 
ls: cannot access 'adhbjgh': No such file or directory
test.sh
[root@SYL4 ~]# echo $?
2
[root@SYL4 ~]# llls test.sh 
-bash: llls: command not found
[root@SYL4 ~]# echo $?
127
[root@SYL4 ~]# 
3. 脚本测试
  • 脚本具备通用性
bash -n scriptname      //检查脚本是否有语法错误
bash -x scriptname      //单步执行,检查脚本错在哪里
[root@SYL4 ~]# bash -n test.sh 

[root@SYL4 ~]# bash -x test.sh 
+ echo

[root@SYL4 ~]# 
4. 写脚本注意事项
  • 禁止将未成功执行过的代码直接写进脚本
  • 脚本中的命令一定要用绝对路径
5. shell算术运算
A=3
B=6

let C=$A+$B         //let 算术运算表达式

C=$[$A+$B]          //$[算术运算表达式]

C=$(($A+$B))         //$((算术运算表达式))

C=`expr $A + $B`    //expr 算术运算表达式,表达式中各操作数及运算符之间要有空隔,而且要使用命令引用

1.
[root@SYL4 ~]# vim test.sh 
[root@SYL4 ~]# cat test.sh 
#!/bin/bash

a=10
b=5

let c=$a+$b
echo $a + $b = $c
[root@SYL4 ~]# ./test.sh 
10 + 5 = 15
[root@SYL4 ~]# 


2.
[root@SYL4 ~]# vim test.sh 
[root@SYL4 ~]# cat test.sh 
#!/bin/bash

a=10
b=5

c=$[$a+$b]
echo $a + $b = $c
[root@SYL4 ~]# ./test.sh 
10 + 5 = 15
[root@SYL4 ~]# 


3.
[root@SYL4 ~]# vim test.sh 
[root@SYL4 ~]# cat test.sh 
#!/bin/bash

a=10
b=5

c=$(($a+$b))
echo $a + $b = $c
[root@SYL4 ~]# ./test.sh 
10 + 5 = 15
[root@SYL4 ~]# 
[root@SYL4 ~]# b=40
[root@SYL4 ~]# c=20
[root@SYL4 ~]# echo $(($b-$c))
20
[root@SYL4 ~]# 

4.
[root@SYL4 ~]# vim test.sh 
[root@SYL4 ~]# cat test.sh 
#!/bin/bash

a=10
b=5

c=`expr $a + $b`
echo $a + $b = $c
[root@SYL4 ~]# ./test.sh 
10 + 5 = 15
[root@SYL4 ~]# 

5. 除
[root@SYL4 ~]# vim test.sh 
[root@SYL4 ~]# cat test.sh 
#!/bin/bash

a=10
b=5

c=`expr $a / $b`
echo $a / $b = $c
[root@SYL4 ~]# ./test.sh 
10 / 5 = 2
[root@SYL4 ~]# 


6. 乘
[root@SYL4 ~]# vim test.sh 
[root@SYL4 ~]# cat test.sh 
#!/bin/bash

a=10
b=5

let c=$a*$b
echo $a*$b=$c
[root@SYL4 ~]# ./test.sh 
10*5=50
[root@SYL4 ~]# 

7. 平方
[root@SYL4 ~]# vim test.sh 
[root@SYL4 ~]# cat test.sh 
#!/bin/bash

a=10
b=5

let c=$a**2
echo $c
[root@SYL4 ~]# ./test.sh 
100
[root@SYL4 ~]# vim test.sh 
[root@SYL4 ~]# cat test.sh 
#!/bin/bash

a=10
b=5

let c=$a**3
echo $c
[root@SYL4 ~]# ./test.sh 
1000
[root@SYL4 ~]# 
6. 命令间的逻辑关系
逻辑与:&&
    第一个条件为假时,第二个条件不用再判断,最终结果已经有
    第一个条件为真时,第二个条件必须得判断
逻辑或:||
    前一个命令的结果为真时,第二个命令就不执行
    前一个命令的结果为假时,第二个命令必须执行

&& 前面的一条命令成功,才能执行;失败了,退出

[root@SYL4 ~]# ls sss && cat test.sh 
ls: cannot access 'sss': No such file or directory
[root@SYL4 ~]# ls && cat test.sh 
anaconda-ks.cfg  scripts  test.sh
#!/bin/bash

a=10
b=5

let c=$a**3
echo $c
[root@SYL4 ~]# ls && cat test
anaconda-ks.cfg  scripts  test.sh
cat: test: No such file or directory
[root@SYL4 ~]# 


|| 前面的一条命令成功,不执行;失败了,执行 

[root@SYL4 ~]# ls sss || cat test.sh 
ls: cannot access 'sss': No such file or directory
#!/bin/bash

a=10
b=5

let c=$a**3
echo $c
[root@SYL4 ~]# 


[root@SYL4 ~]# vim test.sh 
[root@SYL4 ~]# cat test.sh 
#!/bin/bash

ls hkjh && echo hehe
[root@SYL4 ~]# ./test.sh 
ls: cannot access 'hkjh': No such file or directory


[root@SYL4 ~]# vim test.sh 
[root@SYL4 ~]# cat test.sh 
#!/bin/bash

ls hkjh || echo hehe
[root@SYL4 ~]# ./test.sh 
ls: cannot access 'hkjh': No such file or directory
hehe
[root@SYL4 ~]# 

三. 作业

1. 写一个脚本,传递两个整数给此脚本,让脚本分别计算并显示这两个整数的和,差,积,商
[root@SYL4 scripts]# cat shenyunlong_1.sh 
#!/bin/bash

a=60
b=30

#计算并显示这两个整数的和
let c=$a+$b
echo $c
#计算并显示这两个整数的差
let c=$a-$b
echo $c
#计算并显示这两个整数的积
let c=$a*$b
echo $c
#计算并显示这两个整数的商
let c=$a/$b
echo $c
[root@SYL4 scripts]# 
2. 写一个脚本,要求如下:
  • 创建目录/tmp/scripts
  • 切换至此目录中
  • 复制/etc/pam.d目录至当前目录,并重命名为test
  • 将当前目录的test及其里面的文件和子目录的属主改为redhat
  • 将test及其子目录中的文件的其它用户的权限改为没有任何权限
[root@SYL4 scripts]# cat shenyunlong_2.sh 
#!/bin/bash

#创建目录/tmp/scripts
mkdir -p /tmp/scripts
#切换到/tmp/scripts
cd /tmp/scripts
#复制/etc/pam.d目录至当前目录,并重命名为test
cp -r /etc/pam.d /tmp/scripts
mv pam.d test
#将当前目录的test及其里面的文件和子目录的属主改为redhat
useradd redhat
chown -R redhat /tmp/scripts/test
#将test及其子目录中的文件的其它用户的权限改为没有任何权限
chmod -R o=--- /tmp/scripts/test
[root@SYL4 scripts]# 
3. 写一个脚本,要求如下:
  • 显示当前系统日期和时间,而后创建目录/tmp/lstest
  • 切换工作目录至/tmp/lstest
  • 创建目录a1d,b56e,6test
  • 创建空文件xy,x2y,732
  • 列出当前目录下以a,x或者6开头的文件或目录
  • 列出当前目录下以字母开头,后跟一个任意数字,而后跟任意长度字符的文件或目录
[root@SYL4 scripts]# cat shenyunlong_3.sh 
#!/bin/bash

#显示当前系统日期和时间
date
#创建目录/tmp/lstest
mkdir /tmp/lstest
#切换工作目录至/tmp/lstest
cd /tmp/lstest
#创建目录a1d,b56e,6test
mkdir a1d b56e 6test
#创建空文件xy,x2y,732
touch xy x2y 732
#列出当前目录下以a,x或者6开头的文件或目录
ls [ax6]*
#列出当前目录下以字母开头,后跟一个任意数字,而后跟任意长度字符的文件或目录
ls | grep "^[a-z][0-9]"
[root@SYL4 scripts]# 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值