git 、shell脚本

git

文件版本控制

 安装git

yum -y install git

创建仓库

将文件提交到暂存

git add .

#将暂存区域的文件提交仓库

git commit -m "说明"

#推送到远程仓库

git push

#获取远程仓库的更新

git pull

#克隆远程仓库

git clone

#分支,提高代码的灵活性

#检查分支

git branch

#*所在的行为当前分支

#创建新分支

git branch 新分支名称

#跳转分支

git checkout 分支名称

#在跳转分支的同时建立新分支

git checkout -b 新分支名称

#删除分支

git checkout -d|D 分支名称

#合并分支

#跳转到主合并分支

git checkout a

git merge  a

#合并冲突

手动解决

shell

概念

可以用过shell命令来操作和控制操作系统(是操作系统的外壳)

shell脚本是由shell命令组成的执行文件,将一些命令整合到一个文件中,进行处理

编写一个简单的脚本,并运行

[root@a1 ~]# vim helloworld.sh
[root@a1 ~]# bash helloworld.sh 
hello world!
执行方式

也可以通过以下方式来执行脚本:

安装nginx脚本的编写
vim  nginx.sh

在里面写入内容,复制相应的文件路径

判断是否安装的脚本
#!/bin/bash
rpm -qa|grep nginx
echo $?

文件/目录操作符

[root@shell ~]# touch abc

[root@shell ~]# ls -lh abc

-rw-r--r--. 1 root root 0 7月  26 11:26 abc

[root@shell ~]# [ -e "abc" ]

[root@shell ~]# echo $?

0

[root@shell ~]# [ -e "lllll" ]

[root@shell ~]# echo $?

1

[root@shell ~]# [ -w "lllll" ]

[root@shell ~]# echo $?

1

[root@shell ~]# [ -w "abc" ]

[root@shell ~]# echo $?

0

[root@shell ~]# [ -x "abc" ]

[root@shell ~]# echo $?

1

[root@shell ~]# ls -l abc

-rw-r--r--. 1 root root 0 7月  26 11:26 abc

[root@shell ~]# chmod +x abc

[root@shell ~]# [ -x "abc" ]

[root@shell ~]# echo $?

0

[root@shell ~]# [ -z "abc" ]

[root@shell ~]# echo $?

1

[root@shell ~]# 

变量

环境变量

由系统维护,用于设置工作环境

$PWD

$SHELL

$USER

脚本案例
[root@a1 ~]# vim test001.sh
[root@a1 ~]# bash test001.sh 1 2 3 4 5
1
2
3
4
5
[root@a1 ~]# bash test001.sh ni yao gan ma
ni
yao
gan
ma

[root@a1 ~]# cat test001.sh 
#!/bin/bash

echo $1
echo $2
echo $3
echo $4
echo $5

脚本案例
[root@a1 ~]# vim created.sh
[root@a1 ~]# bash created.sh xx yy
更改用户 xx 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@a1 ~]# cat created.sh 
#!/bin/bash
 useradd $1
echo $2|passwd --stdin $1

if条件 ,脚本
[root@a1 ~]# vim munu.sh
[root@a1 ~]# bash munu.sh 
1新增文件 2删除文件 3查找文件 4修改文件
请输入序号选择:3
其他功能正在开发
[root@a1 ~]# bash munu.sh 
1新增文件 2删除文件 3查找文件 4修改文件
请输入序号选择:1
[root@a1 ~]# cat munu.sh 
#!/bin/bash

echo "1新增文件 2删除文件 3查找文件 4修改文件"
read -p "请输入序号选择:"  m
if [ $m == 1 ];then
     touch aaaaa.txt
elif [ $m == 2 ];then
  rm -rf aaaaa.txt
else
    echo "其他功能正在开发"
fi

用户输入

read命令

read用来从标准输入中读取数据并赋值给变量

格式

read [-选项] [变量赋值]

[root@a1 ~]# read -p "输入一个数据" s
输入一个数据aabbcc
[root@a1 ~]# echo $s
aabbcc
[root@a1 ~]# read -p "输入一个数据" -s s
输入一个数据[root@a1 ~]# echo $s
weishenm

可以给多个变量赋值
[root@a1 ~]# read -p "3个变量" a b c
3个变量12 18 20
[root@a1 ~]# echo $a
12
[root@a1 ~]# echo $b
18
[root@a1 ~]# echo $c
20

输入密码
[root@a1 ~]# vim register.sh
[root@a1 ~]# bash register.sh 
username:abc
password:abc
更改用户 abc 的密码 。
passwd:所有的身份验证令牌已经成功更新。
账户abc注册成功
[root@a1 ~]# su abc
[abc@a1 root]$ su
密码:
[root@a1 ~]# cat register.sh 
#!/bin/bash
 
read -p "username:" username
read -p "password:" password
useradd $username
echo $password|passwd --stdin $username
if [ $? -eq 0 ];then
  echo "账户$username注册成功"
fi

循环语法

[root@a1 ~]# mkdir /abc
[root@a1 ~]# nohup inotifywait -mr /abc/
nohup: 忽略输入并把输出追加到"nohup.out"

脚本案例
[root@a1 ~]# vim city.sh
[root@a1 ~]# bash city.sh 
青岛这是个好地方
上海这是个好地方
泉州这是个好地方
[root@a1 ~]# cat city.sh 
#!/bin/bash
for city in 青岛 上海 泉州
do  
  echo "$city这是个好地方"
done
脚本案例
[root@a1 ~]# vim ping.sh
[root@a1 ~]# cat ping.sh 
#!/bin/bash
for IP in $(echo 192.168.1.{10..254})
do
        ping -c 2 -i 0.1 $ip &> /dev/null
   if [ $? -eq 0 ];then
      echo "$ipisup"
fi
done
脚本案例
vim  gushi.sh
床前明月光
疑是地上霜
举头望明月
低头思故乡

awk的使用

[root@shell ~]# awk -F ":" '{print $1}' /etc/passwd

root

bin

daemon

adm

lp

sync

shutdown

halt

mail

operator

games

ftp

nobody

systemd-network

dbus

polkitd

sshd

postfix

chrony

user3

user4

mysql

hahaha

abc

sed

概念

一次处理一行,处理时把待处理的行放到缓冲区,成为”模式空间“。接着sed命令处理缓冲区的

数据,处理完之后把缓冲区的内容送往屏幕展示,这样不断重复直到文件末尾,文件内容并没有

被改变,除非使用重定向存储文件,文件内容会被修改。

语法格式

sed  选项  (定位符) 指令  文件名  (定位符) 指令 --想对文件的哪一步进行操作

选项

-n   屏蔽默认输出

-r   支持扩展正则

练习

   配置一个自动设置静态IP以及关闭seliux服务 关闭防火墙服务 关闭networkmanager,

修改主机名称的脚本,ip和主机名称使用read输入,这个操作只能在root下执行

脚本如下:

[root@shell ~]#vim net.sh

# !/bin/bash

# 备份

cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33.bak

read -p "请输入指定ip地址:" ip

# 替换dhcp 为 none

sed -i '/dhcp/s/dhcp/none/g' /etc/sysconfig/network-scripts/ifcfg-ens33

# 在文档最后添加5行

# IPADDR

sed -i '$aIPADDR='"$ip"'' /etc/sysconfig/network-scripts/ifcfg-ens33

# NETMAST

sed -i '$aNETMASK=255.255.255.0' /etc/sysconfig/network-scripts/ifcfg-ens33

# GATEWAY

sed -i '$aGATEWAY=10.1.1.2' /etc/sysconfig/network-scripts/ifcfg-ens33

# DNS1

sed -i '$aDNS1=8.8.8.8' /etc/sysconfig/network-scripts/ifcfg-ens33

# DNS2

sed -i '$aDNS2=114.114.114.114' /etc/sysconfig/network-scripts/ifcfg-ens33

# 修改uuid



# 修该主机名称

read -p "请输入主机名称" hn
hostnamectl set-hostname $hn

# 停用selinux

setenforce 0
sed -i '/SELINUX=enforcing/cSELINUX=disabled'  /etc/selinux/config

# 停用防火墙

systemctl stop firewalld
systemctl disable firewalld

# 停用NetworkManage

systemctl stop NetworkManager
systemctl disable NetworkManager

# 重启网络服务

systemctl restart netword

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值