一、命令使用:echo、eval、exec、export、read、shift、exit(要求退出码为222)这些命令的功能,以及用例。
echo:可在屏幕上输出信息。
[root@localhost ~]# echo do my homework
do my homework
eval:当shell程序执行到eval语句时,shell读入参数args,并将它们组合成一个新的命令,然后执 行。
[root@localhost ~]# a='homework;echo do'
[root@localhost ~]# echo $a
homework;echo do
[root@localhost ~]# eval echo $a
homework
do
exec:能够在不创建新的子进程的前提下,转去执行指定的命令,当指定的命令执行完毕后,该 进程就终止了。
[root@localhost ~]# echo do;exec echo my;echo homework
do
my
Connection closed.
Disconnected from remote host(192.168.238.128:22) at 12:23:40.
Type `help' to learn how to use Xshell prompt.
export:设置或者显示环境变量。
[root@localhost ~]# export NAME="bianliang"
[root@localhost ~]# echo $NAME
bianliang
read:可从标准输入读取字符串等信息,传给shell程序内部定义的变量。
[root@localhost ~]# read -t 5 -p "do my homework" #等待5秒
do my homework[root@localhost ~]#
shift:在程序中每使用一次shift语句,都会使所有的位置参数依次向左移动一个位置,并使位置参 数$#减1,直到减到0为止。
[root@localhost ~]# vim shift.sh
while [ $# != 0 ]; do
echo "第一个参数为:$1,还剩下参数数:$(($#-1))"
shift
done
[root@localhost ~]# chmod +x shift.sh
[root@localhost ~]# ./shift.sh a b c d e
第一个参数为:a,还剩下参数数:4
第一个参数为:b,还剩下参数数:3
第一个参数为:c,还剩下参数数:2
第一个参数为:d,还剩下参数数:1
第一个参数为:e,还剩下参数数:0
exit:退出shell程序。在exit之后可以有选择地指定一个数作为返回状态。
[root@localhost ~]# vim exit.sh
echo begin
exit 222
echo end
[root@localhost ~]# chmod +x exit.sh
[root@localhost ~]# ./exit.sh
begin
[root@localhost ~]# echo $?
222
二、使用read从标准输入读取两个变量: name1和name2, 要求name1包含空格。
[root@localhost ~]# vim homework.sh
#!/bin/bash
IFS=";"
read name1 name2
echo $name1
echo $name1 $name2
[root@localhost ~]# chmod a+rx homework.sh
[root@localhost ~]# ./homework.sh
1 2;3
1 2
1 2 3
三、定义自定义环境变量:(使用export和declare)。
1.用户的环境变量配置(non-login shell) ~/.bash_profile或~/.bashrc
2.全局环境变量的配置(login shell) /etc/bashrc、/etc/profile文件或者/etc/profile.d目录中定义。
引用变量:$变量名 或者${变量名}
查看变量:echo $变量名,set(可查看所有变量:包括自定义变量和环境变量),env显示全局变量, declare输出所有的变量、函数、整数和已经导出的变量。
取消变量:unset 变量名
作用范围:仅在当前shell中有效
四、脚本执行的方式及特点:用样例验证。
1.bash ./filename.sh(产生子进程,再运行,使用当前指定的bash shell去运行)
2../filename.sh(产生子进程,再运行,使用脚本里面指定的shell去运行。使用该种方式执行需要x权限)
3.source ./filename.sh(source命令是一个shell内部命令,其功能是读取指定的shell程序文件,并 且依次执行其中的所有的语句,并没有创建新的子shell进程,所以脚本里面所有创建的变量都会保存到 当前的shell里面)
4.. filename.sh(和source一样,也是使用当前进程执行)
[root@localhost test]# vim test2.sh
#!/bin/bash
cd /tmp
pwd
[root@localhost test]# ls -l test2.sh
-rw-r--r--. 1 root root 24 Apr 30 20:09 test2.sh
(1)[root@localhost test]# bash test2.sh
/tmp
(2)[root@localhost test]# ./test2.sh
-bash: ./test2.sh: Permission denied
[root@localhost test]# chmod a+rx test2.sh
[root@localhost test]# ./test2.sh
/tmp
(3)[root@localhost test]# source test2.sh
/tmp
[root@localhost tmp]#
(4)[root@localhost test]# . test2.sh
/tmp
[root@localhost tmp]#
[root@localhost test]# echo 'userdir=`pwd`' > test3.sh
[root@localhost test]# cat test3.sh
userdir=`pwd`
(1)[root@localhost test]# bash test3.sh
[root@localhost test]# echo $userdir
[root@localhost test]#
(2)[root@localhost test]# chmod a+rx test3.sh
[root@localhost test]# ./test3.sh
[root@localhost test]# echo $userdir
[root@localhost test]#
(3)[root@localhost test]# source test3.sh
[root@localhost test]# echo $userdir
/test
(4)[root@localhost test]# . test3.sh
[root@localhost test]# echo $userdir
/test
五、设置vim编辑sh文件的缩进为4个空格。
[root@localhost ~]# vim ~/.vimrc
autocmd FileType sh setlocal ai ts=2 sw=2 et
六、$#,$*,$@,$n,$?的使用。
变量 | 说明 |
---|---|
$# | 命令行参数的个数 |
$n | 表示传递给脚本的第n个参数,例如$1表示第一个参数,$2表示第二个参数,$3表示第三 个参数 |
$0 | 当前脚本的名称 |
$* | 以"参数1 参数2 参数3…"的形式返回所有参数的值 |
$@ | 以“参数1”“参数2”“参数3”…的形式返回所有的值 |
$? | 前一个命令或者函数的返回状态码。$?的返回值用法:(1)判断命令、脚本或函数等程序 是否执行成功(2)若在脚本中调用执行“exit 数字”,则会返回这个数字给“$?”变量(3)如 果是在函数里,则通过“return 数字”把这个数字以函数值的形式传给“$?” |
$$ | 返回本程序的进程ID(PID),不常用 |
$! | 获取上一个在后台工作的进程的进程号,不常用 |
$_ | 保存之前执行的命令的最后一个参数,不常用 |