Skip to content

Commit 994ecdd

Browse files
committed
linux shell book
1 parent f7663c0 commit 994ecdd

File tree

157 files changed

+4526
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

157 files changed

+4526
-0
lines changed

chaper01/autofdisk.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
#本脚本会自动将vdb整个磁盘分成一个区,将将该分区格式化.
3+
#注意:所有数据均将丢失!!!
4+
#n(新建分区),p(新建主分区),1(主分区编号为1)
5+
#回车(从磁盘哪个位置开始分区,默认从第1个扇区)
6+
#回车(分区到哪个扇区结束,回车代表最后,将整个磁盘分1个区)
7+
#wq(保存退出),mkfs.xfs(格式化命令)
8+
fdisk /dev/vdb << EOF
9+
n
10+
p
11+
1
12+
13+
14+
wq
15+
EOF
16+
17+
mkfs.xfs /dev/vdb1
18+
19+
#这里文件或目录的属性测试知识点,可以参考2.4章节的内容
20+
[ ! -d /data ] && mkdir /data
21+
cat >> /etc/fstab << EOF
22+
/dev/vdb1 /data xfs defaults 0 0
23+
EOF
24+
mount -a

chaper01/automail.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
#语法格式:
3+
#命令 << 分隔符
4+
#内容
5+
#分隔符
6+
#系统会自动将两个分隔符之间的内容重定向传递给前面的命令,作为命令的输入。
7+
#注意:分隔符是什么都可以,但前后分隔符必须一致。推荐使用EOF(end of file)
8+
mail -s warning root@localhost << EOF
9+
This is content.
10+
This is a test mail for redirect.
11+
EOF

chaper01/calc.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
#计算1+2+3...+n的和,可以使用n*(n+1)/2公式快速计算结果
3+
read -p "请输入一个正整数:" num
4+
sum=$[num*(num+1)/2]
5+
echo -e "\033[32m$num以内整数的总和是:$sum\033[0m"
6+
7+
#使用三角形的底边和高计算面积:A=1/2bh
8+
read -p "请输入三角形底边长度:" bottom
9+
read -p "请输入三角形高度:" hight
10+
A=$(echo "scale=1;1/2*$bottom*$hight" | bc)
11+
echo -e "\033[32m三角形面积是:$A\033[0m"
12+
13+
#梯形面积:(上底+下底)*高/2
14+
read -p "请输入梯形上底:" a
15+
read -p "请输入梯形下底:" b
16+
read -p "请输入梯形高度:" h
17+
A=$(echo "scale=2;($a+$b)*$h/2" | bc)
18+
echo -e "\033[32m梯形面积是:$A\033[0m"
19+
20+
21+
#使用A=πr^2计算圆的面积,取2位小数π=3.14
22+
read -p "请输入圆的半径:" r
23+
A=$(echo "scale=2;3.14*$r^2" | bc)
24+
echo -e "\033[32m圆的面积是:$A\033[0m"
25+
26+
echo "3282820KiB等于多少GiB?"
27+
G=$(echo "32828920/1024/1024" | bc)
28+
echo -e "\003[32m答案${G}G\033[0m"
29+
#注意使用{}防止变量名歧义
30+
31+
#时间格式转化
32+
read -p "请输入秒数:" sec
33+
ms=$[sec*1000]
34+
echo -e "\033[32m$sec秒=$ms毫秒\033[0m"
35+
us=$[sec*1000000]
36+
echo -e "\033[32m$sec秒=$us微秒\033[0m"
37+
hour=$(echo "scale=2;$sec/60/60"|bc)
38+
echo -e "\033[32m$sec秒=$hour小时\033[0m"
39+

chaper01/echo_menu.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
#version:1.0
3+
#这个脚本仅演示菜单输出,没有具体的功能实现
4+
5+
echo "这是一个打印菜单的例子"
6+
7+
echo "1.查看网卡信息"
8+
echo "2.查看内存信息"
9+
echo "3.查看磁盘信息
10+
4.查看CPU信息
11+
5.查看账户信息"
12+
13+
#!/bin/bash
14+
#Version:2.0
15+
clear
16+
echo -e "\033[42m---------------------------------\033[0m"
17+
echo -e "\e[2;10H这里是菜单\t\t#"
18+
echo -e "#\e[32m 1.查看网卡信息\e[0m #"
19+
echo -e "#\e[33m 2.查看内存信息\e[0m #"
20+
echo -e "#\e[34m 3.查看磁盘信息\e[0m #"
21+
echo -e "#\e[35m 4.查看CPU信息\e[0m #"
22+
echo -e "#\e[36m 5.查看账户信息\e[0m #"
23+
echo -e "\033[42m---------------------------------\033[0m"
24+
echo

chaper01/exit.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
exit

chaper01/first.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
<<COMMENT
3+
Author:Jacob
4+
Date:2018-8-8
5+
Version:1.0
6+
Description:This is my first script
7+
COMMENT
8+
9+
#echo命令可以会先一行字符串,echo后给什么字串屏幕将回显什么字串
10+
echo "hello the world"

chaper01/heredoc_tab.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
#不能屏蔽Tab键,缩进将作为内容的一部分被输出
4+
#注意hello和world前面是tab键
5+
cat << EOF
6+
hello
7+
world
8+
EOF
9+
10+
#Tab键将被忽略,仅输出数据内容
11+
cat <<- EOF
12+
hello
13+
world
14+
EOF

chaper01/heredocument.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
cat > /tmp/test.txt << HERE
4+
该文件为测试文件。
5+
测试完后,记得将该文件删除。
6+
Welcome to Earth.
7+
HERE
8+

chaper01/printf_menu.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
#Version:1.0
3+
clear
4+
printf "\e[42m%s\n\e[0m" "---------------------------------"
5+
printf "\e[2;10H%s\t\t\n" "这里是菜单"
6+
printf "\e[32m%s\e[0m\n" "1.查看网卡信息"
7+
printf "\e[35m%s\e[0m\n" "2.查看内存信息"
8+
printf "\e[36m%s\e[0m\n" "3.查看磁盘信息"
9+
printf "\e[34m%s\e[0m\n" "4.查看CPU信息"
10+
printf "\e[33m%s\e[0m\n" "5.查看账户信息"
11+
printf "\e[42m%s\n\e[0m" "---------------------------------"
12+
echo
13+

chaper01/sleep.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
sleep 1000

0 commit comments

Comments
 (0)