shell是用户与linux操作系统沟通的桥梁。
1.shell脚本的基本概念
2.第一个shell脚本
hello.sh
#!/bin/bash
#我的第一个shell脚本
echo hello,world!
执行脚本:
bash hello.sh
sh hello.sh
#给hello.sh赋权限
[root@master examples]# chmod u+x hello.sh
[root@master examples]# ./hello.sh
[root@master examples]# hello.sh
-bash: hello.sh: 未找到命令
#把当前目录添加到环境变量
[root@master examples]# vim /ect/profile
export PATH=.:$PATH
#再次执行即可
[root@master examples]# source /etc/profile
[root@master examples]# hello.sh
3.shell中声明变量
[root@master examples]# username=zhangsan
[root@master examples]# echo username
username
[root@master examples]# echo $username
zhangsan
[root@master examples]# echo ${username}
zhangsan
获取参数:
#!/bin/bash
#我的第一个shell脚本
echo hello,world!
echo 'args num is:' $#
echo $0
echo $1
echo $2
echo $3
[root@master examples]# hello.sh orange apple banana
hello,world!
args num is: 3
./hello.sh
orange
apple
banana
[root@master examples]# echo $?
0
[root@master examples]# haha
-bash: haha: 未找到命令
[root@master examples]# echo $?
127