#!/bin/bash 主要是为了申明,我所写的均为bash语言(我用的是bash解释器)
执行shell的几种方式
相对路径执行
绝对路劲执行
用sh工具执行 sh test.sh
是source 工具执行 source test.sh
用.加空格执行 . test.sh
[root@localhost ~]# a=2 b=3
[root@localhost ~]# echo $a
2
[root@localhost ~]# echo $b
3
[root@localhost ~]#
当变量名和字符符合使用时需要用{}起来
[root@localhost ~]# echo ${b}text
3text
当赋值左边出现空格是需要用到“”
[root@localhost ~]# b=linux 6.5
-bash: 6.5: command not found
[root@localhost ~]# b="linux 6.5"
[root@localhost ~]# echo $b
linux 6.5
单当遇到特殊字符是单引号是不够的,需要用到转义字符
[root@localhost ~]# kernerl='3.10 $b'
[root@localhost ~]# echo $kernerl
3.10 $b
当一堆反撇好难以将一推命令嵌套是,可以用$()命令
[root@localhost ~]# rpm -q `rpm -qf `which pwd``
rpm: no arguments given for query
which-2.20-7.el7.x86_64
package pwd is not installed
[root@localhost ~]# rpm -q $( rpm -qf $(which pwd))
coreutils-8.22-15.el7.x86_64
read命令的使用
[root@localhost ~]# read a b
1 3
[root@localhost ~]# echo $a $b
1 3
[root@localhost ~]# echo $a
1
加-p提示交互信息[root@localhost ~]# read -p "input your passwd :" passwd
input your passwd :798897
[root@localhost ~]# echo $passwd
798897
shell 中进行简单的运算操作
运算符和变量之间必须要有空格隔开,用其内部命令expr进行运算
格式: expr 变量1 运算符 变量2
[root@localhost ~]# a=2 b=5
[root@localhost ~]# echo `expr $a + $b `
7
[root@localhost ~]# ab=$( expr $a + $b )
[root@localhost ~]# echo $ab
7
修改path的环境变量,让他永久生效
[root@localhost mwb]# PATH="$PATH:/mwb"
[root@localhost mwb]# echo $PAHT
[root@localhost mwb]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/:/mwb
[root@localhost ~]# chmod 777 /mwb/first.sh
[root@localhost ~]# first.sh
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.4 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::20c:29ff:fe1c:3a9e prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1c:3a:9e txqueuelen 1000 (Ethernet)
RX packets 15341 bytes 1466586 (1.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 11424 bytes 3119362 (2.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 68 bytes 6498 (6.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 68 bytes 6498 (6.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]#
编写一个加法运算的脚本来说明位置变量
cat weizhi.sh
#!/bin/bash
SUM=$( expr $1 + $2 )
echo "$1 + $2 = $SUM"
[root@localhost ~]# weizhi.sh 3 5
3 + 5 = 8
预定义变量
$#:命令行中位置变量的个数
$*:所有变量位置的内容(如/root /etc就是)
$?:上一条命令执行后的返回状态,返回0表示正常,放回1表示执行异常或错误
$0:当前执行的进程或程序名
例子:
[root@localhost mwb]# cat back.sh
#!/bin/bash
file=beifen-$(date +%s).tar.gz
tar zcvf $file $* $> /mwb/tar.log
echo "yi zhixing $0 ge jiaoben"
echo "gong wancheng $# beifen duixiang"
echo "juti neirong baokuo $*"
[root@localhost mwb]# back.sh /etc/ /root/
tar: Removing leading `/' from member names
tar: $: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
yi zhixing /mwb/back.sh ge jiaoben
gong wancheng 2 beifen duixiang
juti neirong baokuo /etc/ /root/
.