作业题目:
1.命令使用:echo、eval、exec、export、read、shift、exit(要求退出码为222)
这些命令的功能,以及用例
2..使用read从标准输入读取两个变量: name1和name2, 要求name1包含空格
3.定义自定义环境变量:(使用export和declare)
针对root用户的所有连接:root_data=root
针对所有用户的变量: all_data=all
查看所有的环境变量, 查看所有变量
4.脚本执行的方式及特点:用样例验证
5.设置vim编辑sh文件的缩进为4个空格
6.$#,$*,$@,$n,$?的使用
作业过程:
1.命令使用:echo、eval、exec、export、read、shift、exit(要求退出码为222)
这些命令的功能,以及用例
echo、eval、exec、export、read、shift、exit这些都为常用的内部命令。
echo:在屏幕上输出信息
参数选项:
-n 不换行输出内容
-e 解析转义字符
示例:输出 i love shell
-n:
[root@haha ~]# echo -n "i love shell"
i love shell[root@haha ~]
-e:
[root@haha ~]# echo -e "i\tlove\tshell"
i love shell
eval:
格式: eval args
功能: 当shell程序执行到eval语句时,shell读入参数args,并将它们组合成一个新的命令,然后执 行。
示例:
[root@haha ~]# a='shuju;head -1 /etc/passwd'
[root@haha ~]# echo $a
shuju;head -1 /etc/passwd
[root@haha ~]# eval echo $a
shuju
root:x:0:0:root:/root:/bin/bash
exec:能够在不创建新的子进程的前提下,转去执行指定的命令,当指定的命令执行完毕后,该 进程就终止了。
示例:
[root@haha ~]# exec ls
执行后会退出当前进程,需要重新链接ssh
export设置或者显示环境变量
示例:
直接输入export会显示所有的环境变量
export datan=1 把datan=1加入到环境变量中
read:可从标准输入读取字符串等信息,传给shell程序内部定义的变量。
-p prompt:设置提示信息
-t timeout:设置输入等待时间,单位默认为秒
示例:
-p: 设置提示信息比如please input a number
[root@haha ~]# read -p "please input a number:" number
please input a number:50
[root@haha ~]# echo $number
50
-t : 设置输入的等待时间,超过等待时间程序将会自动停止
shift:在程序中每使用一次shift语句,都会使所有的位置参数依次向左移动一个位置,并使位置参 数$#减1,直到减到0为止。
示例:
exit:退出shell程序。在exit之后可以有选择地指定一个数作为返回状态
示例:
在文件中输出this is a shell 并写出返回的值
[root@haha ~]# bash -x test_s.sh
+ echo 'this is a shell'
this is a shell
+ exit 222
bash -n为脚本单步执行
2..使用read从标准输入读取两个变量: name1和name2, 要求name1包含空格
[root@haha ~]# echo -a "input name1 and name2:";read -a name1;read -a name2
-a input name1 and name2:
stenf curry james
example
[root@haha ~]# echo "${name1[0]} ${name1[1]} ${name1[2]}"
stenf curry james
3.定义自定义环境变量:(使用export和declare)
针对root用户的所有连接:root_data=root
针对所有用户的变量: all_data=all
查看所有的环境变量, 查看所有变量
定义自定义环境变量:
export name=1
export 查看
unset name 删除环境变量
同理declare一样,以下为declare的参数选项
-p:显示所有变量的值
-i:将变量定义为整数,在之后就可以直接对表达式求值,结果只能是整数。如果求值失败或者不是整 数,就设置为0。
-r:将变量声明为只读变量。只读变量不允许修改,也不允许删除。(也可使用readonly定义只读变量)
-a:变量声明为数组变量。但这没有必要,所有变量都不必显示定义就可以用作数组。事实上,在某 种意义上,似乎所有变量都是数组,而且赋值给没有下标的变量与赋值给下标为0的数组元素相同。
-f:显示所有自定义函数,包括名称和函数体。
-x:将变量设置成环境变量。可使用+x将变量变成非环境变量
env也可以查看环境变量
#针对root用户的所有连接:root_data=root
[root@servera bash]# vim /etc/profile
ROOT_DATA="ROOT"
export ROOT_DATA
#针对所有用户的变量: all_data=all
[root@servera bash]# vim ~/.bash_profile
ALL_DATA=="ALL"
export ALL_DATA
#重新加载环境变量
[root@servera bash]# source ~/.bash_profile
[root@servera bash]# source /etc/profile
查看所有的环境变量, 查看所有变量
[root@servera bash]# set | grep ROOT_DATA
ROOT_DATA=ROOT
[root@servera bash]# set | grep ALL_DATA
ALL_DATA==ALL
4.脚本执行的方式及特点:用样例验证
1)bash ./filename.sh(产生子进程,再运行,使用当前指定的bash shell去运行) 2)./filename.sh(产生子进程,再运行,使用脚本里面指定的shell去运行。使用该种方式执行需要x 权限)
3)source ./filename.sh(source命令是一个shell内部命令,其功能是读取指定的shell程序文件,并 且依次执行其中的所有的语句,并没有创建新的子shell进程,所以脚本里面所有创建的变量都会保存到 当前的shell里面)
4). filename.sh(和source一样,也是使用当前进程执行)
5.设置vim编辑sh文件的缩进为4个空格
[root@servera bash]# vim ~/.vimrc
autocmd FileType sh setlocal ai ts=2 sw=2 et
6.$#,$*,$@,$n,$?的使用
[root@servera bash]# vim p_test.sh
echo "------------------"
echo $#
echo "------------------"
echo $2
echo "------------------"
echo $?
[root@servera bash]# bash p_test.sh 1 2 3 4 5
------------------
5 # 参数总数
------------------
2 # 第2个位置参数对应的值
------------------
0 # 执行状态码,0表示执行成功
其中$*和$@需要特别注意:
$*:以"参数1 参数2 参数3…"的形式返回所有参数的值
$@:以"参数1""参数2""参数3"…的形式返回所有的值
示例
[root@servera bash]# vim return_all_vars_test.sh
for i in "name1 name2 name3 name4"
do
echo $i
done
for i in "name1" "name2" "name3" "name4"
do
echo $i
done
[root@servera bash]# bash return_all_vars_test.sh
name1 name2 name3 name4
name1
name2
name3
name4