Linux——shell

shell

shell处于kernel的外层,用来负责接收使用者输入的命令,然后将命令解释成kernel能了解的方式,然后由kernel去执行,再将结果传回默认的输出周边。shell是一种命令解释器,同时也是一个程序语言。
脚本,方便批量处理

.sh 不必须加 为了加强标识

!/usr/bin/env bash

env声明 不管你在哪里,都能找到并使用 解决版本兼容性

脚本

mkdir /shell
cd /shell/

vim hello.sh

!/usr/bin/env bash

echo hello

chmod +x /shell/hello.sh ##执行权限
这里写图片描述

自动添加脚本

vim /etc/vimrc
map ms:call VAN()’s ##map映射 函数
function VAN() ##定义
call append(0,”################################”)
call append(1,”# Author :van #”)
call append(2,”# Email :van@yang.xrml#”)
call append(3,”# Version :1.0 #”)
call append(4,”# Create_Date :”.strftime(“%Y-%m-%d”).” #”)
call append(5,”# Description : #”)
call append(6,”################################”)
call append(7,” “)
call append(8,”#!/usr/bin/env bash”)
endfunction ##定义完成
这里写图片描述

vim /clear_logs.sh

这里写图片描述
Dir=/var/log/ ##名称只能由字母数字下划线组成,并且需首字母大写或山峰山谷

Dir/messages Dir/boot.log
$Dir/maillog
这里写图片描述

引用

echo ##
echo ## ##是转义字符,也叫做“逃逸字符”,目的在于使后接的字符回复原来作为单纯字符的用途
echo “##” ##“弱引用,其转译功能不能转译”!” “$” “\” “`”
echo ‘##’ ##‘强引用
这里写图片描述

echo ‘$5 !5 \5 5
echo “$5 !5 \5 5
这里写图片描述

echo “# my hostname is hostname #”
echo ‘# my hostname is hostname #’
这里写图片描述

my hostname is hostname # ##“先执行
变量的声明

a=1
ab= {a}b = 1b
这里写图片描述

diff

diff ##比较两个文件的不同
[num1,num2][a|c|d][num3,num4]
a ##添加
c ##更改
d ##删除
num1,num2 ##第一个文件的内容
num3,num4 ##第二个文件的内容
这里写图片描述
diff -u hello.sh hello.new >hello.path
这里写图片描述
yum install patch -y
patch hello.sh hello.path
这里写图片描述

grep

cp /etc/passwd .
vim passwd
test:ROOT:test
test:test:root
test:root:test
test:roottest:test
这里写图片描述
grep root /mnt/passwd ##过滤字符
root:x:0:0:roo/bash
test:test:root
test:root:test
test:roottest:test

grep -i root /mnt/passwd ##不区分大小写
root:x:0:0:roo/bash
test:ROOT:test
test:test:root
test:root:test
test:roottest:test

grep -i ^root /mnt/passwd ##行首
root:x:0:0:roo/bash

grep -i root$ /mnt/passwd ##行尾
test:test:root

grep -i root /mnt/passwd | grep -i -E "^root|root$" -v

反向过滤,行首行尾过滤掉
test:ROOT:test
test:root:test
test:roottest:test

grep -i "\<root\>" /mnt/passwd | grep -i -E "^root|root$" -v   

取消扩展
test:ROOT:test
test:root:test
这里写图片描述

grep -n student /etc/passwd ##显示行号
26:student:x:1000:1000:Student User:/home/student:/bin/bash

grep -n1 student /etc/passwd ##附近n行
25-chrony:x:998:996::/var/lib/chrony:/sbin/nologin
26:student:x:1000:1000:Student User:/home/student:/bin/bash
27-usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin

grep -B1 student /etc/passwd ##上面n行
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
student:x:1000:1000:Student User:/home/student:/bin/bash

grep -A1 student /etc/passwd ##下面n行
student:x:1000:1000:Student User:/home/student:/bin/bash
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
这里写图片描述

grep -r hello /shell/ ##目录中过滤
这里写图片描述

cut

cut -d : -f 1 passwd ##-d分隔符,-f第几列
root
bin
daemon

cut -c 1 passwd ##-c第几个字符
r
b
d
这里写图片描述

awk

awk -F 分隔符 -v TEST=$TEST  'BEGIN {print TEST} {print $2} END{print TEST}' /etc/passwd
awk -F : -v name=$name 'BEGIN {print name} {print $1} END{print "end"}' /etc/passwd

这里写图片描述
指定:为分隔符,打印第$字段

sed批量替换字符

sed ‘s/sbin/van/g’ passwd ##全文替换,输出
这里写图片描述
sed ‘s/sbin/van/g’ -i passwd ##-i input,替换原来的
cat passwd
这里写图片描述
sed ‘1,5s/van/westos/g’ passwd ##1到5行替换
这里写图片描述
sed ‘/lp/,/shutdown/s/westos/van/g’ passwd ##lp到shutdown段落所有
这里写图片描述
sed 5d num ##不显示第五行
sed 5p num ##重复显示第五行
sed -n 5p num ##只显示第五行
这里写图片描述
vim file
s/sbin/westos/g
s/nologin/lee/g
sed -f file passwd ##放入文件中执行
这里写图片描述

环境级

在当前环境生效,当前环境关闭,变量失效
export a=1 ##临时
sh test.sh
1
sh test.sh ##退出内存,则消失

这里写图片描述

用户级

只针对配置过的用户生效,其他用户无法使用
vim .bash_profile
export a=1
source .bash_profile
sh /mnt/test.sh
1
su - student
sh /mnt/test.sh

这里写图片描述

系统级

vim /etc/profile
export PATH=$PATH:/mnt

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/root/bin

vim .bash_profile
export PATH=$PATH:/mnt

source .bash_profile
sh /mnt/test.sh

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/root/bin:/root/bin:/mnt
这里写图片描述
bash的一些重要的内置变量:

$1 ~ $n  参数位置。当n超过9后,使用${n},例如${10}
$* 代表所有的参数位置,而且视为一个字符串
$@ 代表所有的参数位置,但是代表各位置参数组成的串行
$# 参数的个数
$? 上一个命令的返回值
$! 上一个后台进程的编号
$$ 目前shell的进程编号

命令别名

alias ##查看所有别名

当前用户

alias xie=’vim’
vim ~/.bashrc
alias xie=’vim’
这里写图片描述

所有用户

vim /etc/bashrc
alias xie=’vim’
source /etc/bashrc
这里写图片描述
unalias xie ##删除别名
这里写图片描述

变量定义方式

$1 ##第一串字符
$2 ##第二串字符
$3 ##第三串字符
$* ##所有字符“1 2 3”
$# ##有几个字符
$@ ##所有字符“1” “2” “3”

read -p “……” NUM
这里写图片描述

shell里面定义函数

函数:语句块
TEST()
{
echo hello world
}
TEST
这里写图片描述

变量对比

test = [ ]
-a  and
-o  or
-ne !=
-gt >
-ge >=
-le <=
-lt <
-z $a  函数值是不是空的
-n $a  是否不为空
-b $a  是否为块设备
-f $a  纯文本
-S $a  套结字
-L $a  链接
-nt $a 比较新
-ot $a 比较旧
-d $a  directory
-e $a  存在

四则运算

i++ —> i=i+1
i– —> i=i-1
j+=i —> j=j+i
j-=i —> j=j-1

+  #加
-  #减
*  #乘
/  #除
%  #余

运算明令
$[ 3 + 2 ]
let A=3+2
expr 3+2

脚本中的常用语句

1.
for

for 变量 in 列举
do
命令
done
这里写图片描述
这里写图片描述
2.
while

while 条件
do
命令
done
这里写图片描述
这里写图片描述
3.
if

if 条件1; then
动作1
elif 条件2; then
动作2
else
动作3
fi
这里写图片描述
这里写图片描述
break continue
break跳出当前循环,continue提前进入下一次循环

4.
case

case 条件
动作1)
;;
动作2)
;;
esac
这里写图片描述
这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值