一、查看su和su-切换用户时加载的文件都有哪些?
su - root,产生一个登录shell去执行后面的指令。登录 shell 执行的是shell 登录的流程,会执行 /etc/profile,/etc/profile.d/下定义的*.sh都会执行。
su root,产生一个非登录交互shell,非登录交互shell,只执行 用户家目录下 .profile等配置文件
二、第一章节例题
1.查看当前系统支持的shell
[root@wn2 ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
2.查看当前系统默认shell
[root@wn2 ~]# echo $SHELL
/bin/bash
3.不换行输出内容(参数为-n)
[root@wn2 ~]# echo i have a cat
i have a cat
[root@wn2 ~]# echo -n i have a cat
i have a cat[root@wn2 ~]#
4.解析转义字符(参数为-e)!!必须加双引号
[root@wn2 ~]# echo "i\thave\ta\tcat"
i\thave\ta\tcat
[root@wn2 ~]# echo -e "i\thave\ta\tcat"
i have a cat
转义字符:
\n 换行
\r 回车
\t 制表符
\b 退格
\v 纵向制表符
[root@wn2 ~]# echo -e "i\thave\ta\tcat"
i have a cat
[root@wn2 ~]# echo -e "i\vhave\va\vcat"
i
have
a
cat
[root@wn2 ~]# echo -e "i\nhave\na\ncat"
i
have
a
cat
[root@wn2 ~]# echo -e "i\bhave\ba\bcat"
havcat
[root@wn2 ~]# echo -e "i\rhave\ra\rcat"
cate
5.shell脚本编写时要多使用内部命令
查看一个指令是内部还是外部命令
[root@wn2 ~]# type echo
echo 是 shell 内嵌
[root@wn2 ~]# type export
export 是 shell 内嵌
[root@wn2 ~]# type eval
eval 是 shell 内嵌
[root@wn2 ~]# type tree
tree 是 /usr/bin/tree
[root@wn2 ~]# type mkdir
mkdir 是 /usr/bin/mkdir
[root@wn2 ~]# type cat
cat 已被哈希 (/usr/bin/cat)
(1)eval
[root@wn2 ~]# a='shuju;head -1 /etc/passwd'
[root@wn2 ~]# echo $a
shuju;head -1 /etc/passwd
[root@wn2 ~]# eval echo $a
shuju
root:x:0:0:root:/root:/bin/bash
[root@wn2 ~]# a='wangnan;tail -1 /etc/passwd'
[root@wn2 ~]# echo $a
wangnan;tail -1 /etc/passwd
[root@wn2 ~]# eval echo $a
wangnan
named:x:25:25:Named:/var/named:/sbin/nologin
(2)read命令—从标准输入读取字符串等信息,传给shell程序内部定义的变量
-p prompt 设置提示信息
-t timeout 设置输入等待时间(默认s)
[root@wn2 ~]# read -t 10 "please input your name:" name
wangnan
-bash: read: `please input your name:': 不是有效的标识符
[root@wn2 ~]# read -t 10 -p "please input your name:" name
please input your name:wangnan
[root@wn2 ~]# echo $name
wangnan
[root@wn2 ~]# read -t 10 -p "please input your name:" name
please input your name:[root@wn2 ~]#
[root@wn2 ~]# echo -n "please input your name:" ;read name1 name2 name3
please input your name:wangnan xiaoming xiaobai
[root@wn2 ~]# echo $name1
wangnan
[root@wn2 ~]# echo $name2
xiaoming
[root@wn2 ~]# echo $name3
xiaobai
(3)exit:退出shell程序,在exit之后可以有选择的指定一个数作为返回状态
查看:(echo $?)
[root@wn2 ~]# echo $?
0
6.编写shell脚本没必要使用cat,因为耗资源
[root@wn2 ~]# cat /etc/passwd |grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@wn2 ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
7.代码缩进
[root@wn2 ~]# cat creat_username.sh
#!/bin/bash
i=1
while [ $i -le 10 ]
do
if [ $i -le 9]
then
username=user0$i
else
username=user$i
fi
! id $username &>/dev/null &&{
useradd $username
echo $username | passwd --stdin $username &>/dev/null
}
let i++;
done
8.快速生成脚本开头的版本版权注释信息
[root@wn2 ~]# vim ~/.vimrc
[root@wn2 ~]# cat ~/.vimrc
autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()"
func SetTitle()
if expand("%:e") == 'sh'
call setline(1,"#!bin/bash")
call setline(2,"###################")
call setline(3,"#File name:".expand("%"))
call setline(4,"#Version:v1.0")
call setline(5,"#Email:admin@test.come")
call setline(6,"#Created time:".strftime("%F %T"))
call setline(7,"#Description:")
call setline(8,"###################")
call setline(9,"")
endif
endfunc
9.shell脚本的执行方式
(1)交互式执行
[root@wn2 ~]# for filename in `ls /etc`
> do
> if echo "$filename" | grep "passwd"
> then
> echo "$filename"
> fi
> done
passwd
passwd
passwd-
passwd-
(2)作为程序文件执行(常用)
[root@wn2 test]# vim test1.sh
[root@wn2 test]# cat test1.sh
#!/bin/bash
###################
#File name:test1.sh
#Version:v1.0
#Email:admin@test.come
#Created time:2021-01-16 12:27:20
#Description:
###################
for filename in `ls /etc`
do
if echo "$filename" | grep "passwd"
then
echo "$filename"
fi
done
10.执行脚本
(1)
(2)
[root@wn2 test]# vim test2.sh
[root@wn2 test]# cat test2.sh
#!/bin/bash
###################
#File name:test2.sh
#Version:v1.0
#Email:admin@test.come
#Created time:2021-01-16 12:45:18
#Description:
###################
cd /tmp
pwd
[root@wn2 test]# ls -l test2.sh
-rw-r--r--. 1 root root 168 1月 16 12:45 test2.sh
#方法一
[root@wn2 test]# bash test2.sh
/tmp
#方法二
[root@wn2 tmp]# ./test2.sh
-bash: ./test2.sh: 权限不够
[root@wn2 tmp]# chmod a+rx test2.sh
[root@wn2 tmp]# ./test2.sh
/tmp
#方法三
[root@wn2 test]# source test2.sh
/tmp
#方法四
[root@wn2 test]# . test2.sh
/tmp