Shell是一个命令行解释器,它能够通过接收 应用程序/用户 的命令,去调用操作系统内核,从而完成程序指令。Shell是用户操作Linux系统的指令,同时,Shell还是一个功能相当强大的编程语言,具有易编写,易调试,灵活性强的特点。
[tang@aliyun module]$ echo $HOME
/home/tang
[tang@aliyun module]$ echo $PWD
/opt/module
[tang@aliyun module]$ echo $SHELL
/bin/bash
[tang@aliyun module]$ echo $USER
tang
[tang@aliyun bin]$ $PATH
bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/module/jdk1.8.0_291/bin:/opt/module/hadoop-3.1.3/bin
[root@aliyun bin]$ A=1
[root@aliyun bin]$ echo $A
1
[tang@aliyun bin]$ readonly B=2
[tang@aliyun bin]$ echo $B
2
[tang@aliyun bin]$ unset B
bash: unset: B: cannot unset: readonly variable
[tang@aliyun myshell]$ vim params.txt
[tang@aliyun myshell]$ cat params.txt
#!/bin/bash
echo $#
echo $*
echo $@
echo $?
echo $1
[tang@aliyun myshell]$ bash params.txt han tang zhonghua
3
han tang zhonghua
han tang zhonghua
0
han
[tang@aliyun myshell]$ [ ]
[tang@aliyun myshell]$ echo $?
1
[tang@aliyun myshell]$ [ tang ]
[tang@aliyun myshell]$ echo $?
0
[tang@aliyun data]$ [ tang = tang ]
[tang@aliyun data]$ echo $?
0
[tang@aliyun data]$ [ -z tang ]
[tang@aliyun data]$ echo $?
0
[tang@aliyun data]$ [ 1 -lt 2 ]
[tang@aliyun data]$ echo $?
0
[tang@aliyun data]$ [ 1 -gt 2 ]
[tang@aliyun data]$ echo $?
1
[tang@aliyun data]$ ll
total 8
-rw-r--r-- 1 root root 4 Jul 7 12:16 han.txt
-rw-r--r-- 1 tang tang 5 Jul 7 12:17 tang.txt
[tang@aliyun data]$ [ -r tang.txt ]
[tang@aliyun data]$ echo $?
0
[tang@aliyun data]$ [ -w han.txt ]
[tang@aliyun data]$ echo $?
1
[tang@aliyun data]$ [ -e zhonghua.txt ]
[tang@aliyun data]$ echo $?
1
[tang@aliyun data]$ [ -f tang.txt ]
[tang@aliyun data]$ echo $?
0
[tang@aliyun data]$
# 输入一个字符串,如果是han,则输出 "强汉",如果是tang,
# 则输出 "盛唐",如果是其它,输出 "中华复兴"。
[tang@aliyun myshell]$ cat if.sh
#!/bin/bash
if [ $1 = "han" ]
then
echo "强汉"
elif [ $1 = "tang" ]
then
echo "盛唐"
else
echo "中华复兴"
fi
[tang@aliyun myshell]$ bash if.sh han
强汉
[tang@aliyun myshell]$ bash if.sh tang
盛唐
[tang@aliyun myshell]$ bash if.sh now
中华复兴
# 输入一个字符串,如果是han,则输出 "强汉",如果是tang,
# 则输出 "盛唐",如果是其它,输出 "中华复兴"。
[tang@iZwz945acgf8n138r7qd7pZ myshell]$ cat case.sh
#!/bin/bash
case $1 in
"han")
echo "强汉"
;;
"tang")
echo "盛唐"
;;
*)
echo "中华复兴"
;;
esac
[tang@aliyun myshell]$ bash case.sh han
强汉
[tang@aliyun myshell]$ bash case.sh tang
盛唐
[tang@aliyun myshell]$ bash case.sh now
中华复兴
# 从1加到100
[tang@aliyun myshell]$ cat for1.sh
#!/bin/bash
sum=0
for ((i=i; i<=100; i=i+1))
do
sum=$[$sum+$i]
done
echo $sum
[tang@aliyun myshell]$ bash for1.sh
5050
# 打印所有输入参数
[tang@aliyun myshell]$ cat for2.sh
#!/bin/bash
for i in $*
do
echo $i
done
[tang@aliyun myshell]$ bash for2.sh han tang zhonghua
han
tang
zhonghua
# 从1加到100
[tang@aliyun myshell]$ cat while.sh
#!/bin/bash
i=1
sum=0
while [ $i -le 100 ]
do
sum=$[$sum+$i]
i=$[$i+1]
done
echo $sum
[tang@aliyun myshell]$ bash while.sh
5050
[tang@aliyun myshell]$ basename basename.sh
basename.sh
[tang@aliyun myshell]$ basename basename.sh .sh
basename
[tang@aliyun myshell]$ dirname /home/tang/myshell/test.txt
/home/tang/myshell
# 计算两个输入参数的和
[tang@aliyun myshell]$ cat function.sh
#!/bin/bash
function sum()
{
sum=$[ $1 + $2 ]
echo $sum
}
sum $1 $2
[tang@aliyun myshell]$ bash function.sh 1 2
3
grep 是一个文本过滤工具,通常和管道联合使用快速查询关键字。
egrp 支持扩展正则
options部分
# 准备数据
[tang@aliyun myshell]$ cat sed.txt # 准备数据
shell linux java
is is is
yyds nb dl
[tang@aliyun myshell]$ sed '3a my my my' sed.txt # 每一列is下面添加my
shell linux java
is is is
my my my
yyds nb dl
[tang@aliyun myshell]$ sed '/is/d' sed.txt # 删除所有包含is的列
shell linux java
yyds nb dl
[tang@aliyun myshell]$ sed 's/is/always/g' sed.txt # 将所有is替换为always
shell linux java
always always always
yyds nb dl
[tang@aliyun myshell]$ sed -e '2d' -e 's/java/hadoop/' sed.txt # 将第2行删除并替换java为hadoop
shell linux hadoop
is is is
yyds nb dl
[tang@aliyun myshell]$ cat passwd # 准备数据
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
root:x:3:4:adm:/var/adm:/sbin/nologin
[tang@aliyun myshell]$ awk -F : '/^root/ {print $7}' passwd # 索passwd文件以root关键字开头的所有行,并输出该行的第7列。
/bin/bash
/sbin/nologin
[tang@aliyun myshell]$ awk -F : '/^root/ {print $1","$7}' passwd # 搜索passwd文件以root关键字开头的所有行,并输出该行的第1列和第7列,中间以“,”号分割。
root,/bin/bash
root,/sbin/nologin
[tang@aliyun myshell]$ awk -F : 'BEGIN{print "learn"} {print $1","$7} END{print "shell"}' passwd # 只显示passwd的第一列和第七列,以逗号分割,且在所有行前面添加列名"learn",在最后一行添加"shell"。
learn
root,/bin/bash
bin,/sbin/nologin
daemon,/sbin/nologin
root,/sbin/nologin
shell
[tang@aliyun myshell]$ awk -F : -v i=1 '{print $3+i}' passwd # 将passwd文件中的用户id增加数值1并输出(id为第三列)
1
2
3
4
[tang@aliyun myshell]$ awk -F : '{print FILENAME","NR","NF}' passwd # 统计passwd文件名,每行的行号,每行的列数
passwd,1,7
passwd,2,7
passwd,3,7
passwd,4,7
[tang@aliyun myshell]$ ifconfig eth0 | grep inet | awk -F " " '{print $2}' # 切割ip
172.19.99.251
[tang@aliyun myshell]$ cat sed.txt
shell linux java
is is is
yyds nb dl
[tang@aliyun myshell]$ awk '/^$/ {print NR}' sed.txt # 查询sed.txt中空行所在的行号
2
[tang@aliyun myshell]$ cat cut.txt # 准备数据
shell linux java
is is is
yyds nb dl
[tang@aliyun myshell]$ cut -d " " -f 1 cut.txt # 切割第一列
shell
is
yyds
[tang@aliyun myshell]$ cut -d " " -f 2- cut.txt # 切割2,3列
linux java
is is
nb dl
[tang@aliyun myshell]$ grep shell cut.txt | cut -d " " -f 1 #cut.txt中切割出shell
shell
[tang@aliyun myshell]$ echo $PATH | cut -d : -f 2-
/usr/local/bin:/usr/sbin:/usr/bin:/opt/module/jdk1.8.0_291/bin:/opt/module/hadoop-3.1.3/bin
[tang@aliyun myshell]$ ifconfig eth0 | grep inet | cut -d " " -f 10 # 切割ifconfig中的ip
172.19.99.251
# 提示5秒内,读取控制台输入的名称
[tang@aliyun myshell]$ cat read.sh
#!/bin/bash
read -t 5 -p "请输入您要说的话:" INPUT
echo $INPUT
[tang@aliyun myshell]$ bash read.sh
请输入您要说的话:强汉盛唐
强汉盛唐
[tang@iZwz945acgf8n138r7qd7pZ myshell]$
[tang@aliyun myshell]$ cat sort.txt # 准备数据
han:40:5.4
tang:20:8.2
zhonghua:50:2.3
[tang@aliyun myshell]$ sort -t ":" -nrk 3 sort.txt # 按照“:”分割后的第三列倒序排序。
tang:20:8.2
han:40:5.4
zhonghua:50:2.3
[tang@aliyun myshell]$ sort -t ":" -nrk 1 sort.txt # 字母也可以排序哦
tang:20:8.2
han:40:5.4
zhonghua:50:2.3
sshpass
使用 ssh 远程连接服务器的时候,总是需要输入密码,比较麻烦,使用 sshpass可以命令行或脚本上,显式输入密码,不需要额外啊输入密码
#1.安装/解压 略
#2.编译安装软件
tangshengcai@MacBook-Pro ~ % ./configure
tangshengcai@MacBook-Pro ~ % make && make install
#3.一步登录
tangshengcai@MacBook-Pro ~ % sshpass -p "用户密码" ssh -v -o 'ProxyCommand=ncat --proxy-type socks5 --proxy 跳板机ip:端口 --proxy-auth "跳板机用户名:密码" %h %p' 用户名@远程机器ip
下载链接: https://sourceforge.net/projects/sshpass/files/latest/download
Shell易理解易上手,而经过多年的发展,shell命令已经丰富多样,博主会将常用到的Shell命令或者脚本信息,陆陆续续的更新到文件博文上面,记录下来,方便速记查阅。