1. 定义一个对所有用户都生效的命令别名,例如: lftps= ‘lftp 172.168.0.1/pub’
- 编辑/etc/bashrc文件,在末行添加alias lftps=‘lftp 172.16.0.1/pub’
alias lftps='lftp 172.16.0.1/pub'
2. 显示/etc/passwd
文件中不以/bin/bash结尾的行
[root@etcd03 ~]# grep -v "/bin/bash$" /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/in
ntp:x:38:38::/etc/ntp:/sbin/nologin
nginx:x:1002:1002::/home/nginx:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
3. 找出/etc/passwd
文件中,包含二位数字或者三位的行。
[root@etcd03 ~]# egrep "<[0-9]{2,3}>" /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
4. 显示/proc/meminfo
文件中以大写或小写S开头的行;用三种方式实现。
[root@etcd03 ~]# grep -i ^s /proc/meminfo
SwapCached: 0 kB
SwapTotal: 4079612 kB
SwapFree: 4079612 kB
Shmem: 738760 kB
Slab: 185800 kB
SReclaimable: 117884 kB
SUnreclaim: 67916 kB
[root@etcd03 ~]# grep ^[sS] /proc/meminfo
SwapCached: 0 kB
SwapTotal: 4079612 kB
SwapFree: 4079612 kB
Shmem: 738760 kB
Slab: 185812 kB
SReclaimable: 117884 kB
SUnreclaim: 67928 kB
[root@etcd03 ~]# egrep "^(s|S)" /proc/meminfo
SwapCached: 0 kB
SwapTotal: 4079612 kB
SwapFree: 4079612 kB
Shmem: 738760 kB
Slab: 185816 kB
SReclaimable: 117888 kB
SUnreclaim: 67928 kB
5. 使用echo输出一个绝对路径,使用egrep
取出路径名,类型执行dirname /etc/passwd
的结果。
[root@etcd03 ~]# echo /var/log/messages | grep -o "^/.*/"
/var/log/
6. 找出ifconfig
中的ip地址。要求结果只显示IP地址。
[root@etcd03 ~]# ifconfig | grep "inet" |grep -v "inet6"|awk -F ' ' '{print $2}'
172.17.92.1
172.17.92.0
127.0.0.1
7. vim定制自动缩进四个字符。
- 编辑
/etc/vimrc
文件,添加set tabstop=4
,保存退出
set shiftwidth=4
set tabstop=4
set ai
8. 编写脚本,实现自动添加三个用户,并计算这三个用户的uid
之和。
#!/bin/bash
read -p "Enter three username:" UserName1 UserName2 UserName3
id $UserName1 &> /dev/null || useradd $UserName1
id $UserName2 &> /dev/null || useradd $UserName2
id $UserName3 &> /dev/null || useradd $UserName3
Uid1=$(id $UserName1 | cut -c 5-8)
Uid2=$(id $UserName2 | cut -c 5-8)
Uid3=$(id $UserName3 | cut -c 5-8)
UidSum=$(($Uid1 + $Uid2 + $Uid3))
echo "The sum of newuser's uid is:$UidSum"
9. find
用法以及常用用法的实例演示。
-
find
:语法:
find [OPTIONS] [查找起始路径] [查找条件] [处理动作]
实例:
find . -type f -name "*.log" -print0 | xargs -0 rm -f
在当前目录下将文件名中含有.log的后缀名文件强制删除