一、基本命令
1.脚本执行退出 exit 退出bash,停止执行脚本
可以进行指定返回值
返回值和输出值的区分:
exit 退出脚本并返回指定值
当满足退出条件时候,执行退出,此时echo $?输出的是返回值
当此命令上一条命令执行成功时候,echo $?输出0,反之输出1,此时是输出值。
例子:
#!/bin/bash
mkdir m -p
cd /lianxi/m
if (($? == 0 ));then
exit 8
fi
for i in {1..100}
do
cat /lianxi/luaowei2.txt >>luaowei4.txt
done
[root@mahaoliang lianxi]# echo $?
0
[root@mahaoliang lianxi]# bash creat_big_file.sh
[root@mahaoliang lianxi]# echo $?
8.运算符:(( 进行整数的运算和比较))
shell中双圆括号进行数值运算
例子:
[root@mahaoliang lianxi]# echo $?
0
[root@mahaoliang lianxi]# bash creat_big_file.sh
[root@mahaoliang lianxi]# echo $?
8
[root@mahaoliang lianxi]# a=10
[root@mahaoliang lianxi]# b=12
[root@mahaoliang lianxi]# echo $(($a + $b))
22
[root@mahaoliang lianxi]# echo ;(($a + $b))
[root@mahaoliang lianxi]# c=$((a + b))
[root@mahaoliang lianxi]# echo $c
22
[root@mahaoliang lianxi]# ((a > b))
[root@mahaoliang lianxi]# echo $?
1
[root@mahaoliang lianxi]# echo $((a > b))
0
[root@mahaoliang lianxi]# echo $(a > b)
-bash: a: 未找到命令
[root@mahaoliang lianxi]# echo $((a < b))
1
[root@mahaoliang lianxi]#
9.
shell中小数计算
bc bc - An arbitrary precision calculator language
|管道符号:实现进程和进程之间的通信
bc 在比较时,输入1表示结果对,输出0表示结果为错
[root@mahaoliang ~]# a=12.3
[root@mahaoliang ~]# b=1.6
[root@mahaoliang ~]# echo $a + $b | bc
13.9
[root@mahaoliang ~]#
2.
df -Th查看linux里已经挂载的分区使用情况
文件系统 类型 容量 已用 可用 已用% 挂载点
devtmpfs devtmpfs 898M 0 898M 0% /dev
tmpfs tmpfs 910M 0 910M 0% /dev/shm
tmpfs tmpfs 910M 9.7M 901M 2% /run
tmpfs tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 50G 6.7G 44G 14% /
/dev/sda1 xfs 1014M 151M 864M 15% /boot
/dev/mapper/centos-home xfs 47G 33M 47G 1% /home
tmpfs tmpfs 182M 0 182M 0% /run/user/0
3.
wc 统计命令
wc - print newline, word, and byte counts for each file
-l, --lines
print the newline counts
[root@mahaoliang ~]# wc creat_big_file.sh
5 10 88 creat_big_file.sh
[root@mahaoliang ~]#
4.
free -m 查看内存使用
cache 缓存
buffer 缓冲区
cache和buffer的区别?
buffer:
data memory--》disk --》write data
cache: 读过的命令
disk --》data--》cache--》app
data disk--》memory --》read data
cache和buffer都是消耗内存的空间
echo 3 >/proc/sys/vm/drop_caches 清除缓存里的数据
[root@mahaoliang ~]# free -m
total used free shared buff/cache available
Mem: 1819 247 1419 9 152 1423
Swap: 2047 0 2047
[root@mahaoliang ~]# echo 3 >/proc/sys/vm/drop_caches
[root@mahaoliang ~]# free -m
total used free shared buff/cache available
Mem: 1819 239 1524 9 54 1474
Swap: 2047 0 2047
[root@mahaoliang ~]#
5.
head 从前面截取文件
tail 从尾部截取文件
cat grade.th |tail -n +2
从文件第二行开始到行末
[root@mahaoliang lianxi]# cat grade.th|tail -n +2
1 cali 38 chinese 426
2 rose 20 chinese 450
3 tom 19 Math 98
4 JACK 21 Chinese 120
5 kobe 24 basketable 300
6 mack 26 chinese 560
grep 文本过滤,根据匹配字符串一行一行查找,匹配到了默认就整行输出
sed :文本替换
sed -i ‘s/english/chinese/’ grade.th
-i:直接在源文件里修改
s == substitute
english == oldword
chinese == newword
sed -n '3,6p'
-n 不显示不匹配的行
3,6p显示3到6行
p == print
cat -n grade.th | sed -n ‘3p;5p;7p’
显示第3、5、7行
awk:文本截取
截取文本第五列
[root@mahaoliang lianxi]# cat grade.th | awk '{print $5}'
grade
426
450
98
120
300
560
显示文件第1,2,5列
[root@mahaoliang lianxi]# cat grade.th |awk '{print $1,$2,$5}'
id name grade
1 cali 426
2 rose 450
3 tom 98
4 JACK 120
5 kobe 300
6 mack 560
例子:
[root@mahaoliang lianxi]# cat -n superman.txt | head -5
1 I am the only powerman in the world!
2 I am the only powerman in the world!
3 I am the only powerman in the world!
4 I am the only powerman in the world!
5 I am the only powerman in the world!
[root@mahaoliang lianxi]# head -n 5 superman.txt
I am the only powerman in the world!
I am the only powerman in the world!
I am the only powerman in the world!
I am the only powerman in the world!
I am the only powerman in the world!
[root@mahaoliang lianxi]# tail -5 superman.txt
I am the only powerman in the world!
I am the only powerman in the world!
I am the only powerman in the world!
I am the only powerman in the world!
I am the only powerman in the world!
[root@mahaoliang lianxi]# cat -n superman.txt | head -20 |tail -16
5 I am the only powerman in the world!
6 I am the only powerman in the world!
7 I am the only powerman in the world!
8 I am the only powerman in the world!
9 I am the only powerman in the world!
10 I am the only powerman in the world!
11 I am the only powerman in the world!
12 I am the only powerman in the world!
13 I am the only powerman in the world!
14 I am the only powerman in the world!
15 I am the only powerman in the world!
16 I am the only powerman in the world!
17 I am the only powerman in the world!
18 I am the only powerman in the world!
19 I am the only powerman in the world!
20 I am the only powerman in the world!
[root@mahaoliang lianxi]# sed -i 's/English/chinese/' grade.th
[root@mahaoliang lianxi]# cat grade.th
id name age subject grade
1 cali 38 chinese 426
2 rose 20 chinese 450
3 tom 19 Math 98
4 JACK 21 Chinese 120
5 kobe 24 basketable 300
6 mack 26 chinese 560
[root@mahaoliang lianxi]# cat -n grade.th | sed -n '3,6p'
3 2 rose 20 chinese 450
4 3 tom 19 Math 98
5 4 JACK 21 Chinese 120
6 5 kobe 24 basketable 300
[root@mahaoliang lianxi]#
二、基本概念
1.
日志文件: 记录某个程序发生哪些事情
动态监控日志文件,在未来的学习过程中,用来排查故障,解决问题
log
/var 存放可变化的文件的文件夹 variable 文件的大小可变化--》增加内容
/var/log
/
/root root的家目录
/home 普通用户的家目录
/usr -->存放linux系统资源--》默认情况下软件会安装到这个目录下,软件就是资源 unix system resource
/usr/bin
/var 存放可变化的文件的文件夹 variable
2.
[root@sc-cali lianxi2]# ps aux|cat -n |sed -n '2,9p'
命令 + 选项 + 参数
选项: 是可接可不接,但是接了有特殊作用
如果选项和别的命令雷同,纯属巧合
选项是给命令传递的一个参数,告诉命令如何工作,需要额外做什么?
源码:
#include <stdio.h>
#include<unistd.h>
int main(int argc, char *argv[]){
char ch;
opterr = 0;
// getopt()函数是用来接受用户输入的选项 option
while((ch = getopt(argc,argv,"a:bcde"))!= -1){
switch(ch){
case 'a':
printf("option a:’%s’\n",optarg);
break;
case 'b':
printf("option b :b\n");
break;
default:
printf("other option :%c\n",ch);
}
printf("optopt +%c\n",optopt);
}
return 0;
}
三、练习
练习6:
1.在根目录下新建/lianxi目录
[root@mahaoliang /]# mkdir /lianxi
2.进入/lianxi目录,复制/etc/hosts文件到当前目录下
[root@mahaoliang lianxi]# cp /etc/hosts /lianxi
3.复制/var/log/secure 文件到当前目录下
[root@mahaoliang lianxi]# cp /var/log/secure /lianxi
4.使用vim 编辑器新建一个文件叫cali.txt,内容如下
cali 695811769 linux
hunan linux nongda
vim cali.txt
5.显示secure文件的前6行,并且显示行号
[root@mahaoliang lianxi]# cat -n secure | sed -n '1,6p'
or
[root@mahaoliang lianxi]# cat -n secure | head -6
nl secure |head -6
6.将cali.txt里的内容追加到当前目录下的hosts文件里
[root@mahaoliang lianxi]# cat cali.txt >> hosts
7.显示当前目录下的hosts文件的尾2行
[root@mahaoliang lianxi]# cat -n hosts | tail -2
tail -2 hosts
8.显示secure文件的第7行到10行之间的内容,显示/etc/passwd文件里的第3,5,7,8,9,10行
nl secure |head |tail -4 or
sed -n '7,10p'
[root@mahaoliang lianxi]# cat -n /etc/passwd | sed -n '3p;5p;7p;8p;9p;10p'
9.动态监控/var/log/messages文件的内容
**tail -f /var/log/message 动态监控文件末尾的内容 or
tailf /var/log/message**
10.查看/var/log/messages文件的内容显示行号,并且分页显示
nl /var/log/message | less
11.新建用户yalin设置密码为123
useradd yalin
passwd yalin
or
echo 123 | passwd yalin --stdin(标准输入standard input默认键盘为标准输入;标准输出standard output默认屏幕为标准输出)
12.显示ps aux查看进程命令输出信息,从第2行到9行,并且显示行号。
ps aux | nl | sed -n ‘2,9p’
13.截取/etc/hosts文件的第2列输出到屏幕
cat /etc/hosts | awk ‘{print $2}’
14.替换cali.txt里linux为sanchuang
sed -i ‘s/linux/sanchuang/’ cali.txt