Linux 程序包管理及sed基础
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux 程序包管理及sed基础相关的知识,希望对你有一定的参考价值。
-
1、 简述rpm与yum命令的常见选项,并举例
rpm简称包管理器,即RedHat系列发行版的程序包管理工具,由于它遵循GPL规则且功能强大方便,故而广受欢迎; yum是rhel系列系统上rpm包管理器的前端工具,它可以自动执行系统更新,包括依赖性分析和基于“知识库”元数据的过时处理,还可以执行新的包的安装,删除旧的包,为其用户提供更方便快捷的安装程序。
rpm命令:rpm [OPTIONS] [PACKAGE_FILE]
安装:-i, --install
升级:-U, --update, -F, --freshen
卸载:-e, --erase
查询:-q, --query
校验:-V, --verify
数据库维护:--builddb, --initdb
安装:
rpm {-i|--install} [install-options] PACKAGE_FILE ...
rpm -ivh PACKAGE_FILE ...
GENERAL OPTIONS:
-v:verbose,详细信息
-vv:更详细的输出
[install-options]:
-h:hash marks输出进度条;每个#表示2%的进度;
--test:测试安装,检查并报告依赖关系及冲突消息等;
--nodeps:忽略依赖关系;不建议;
--replacepkgs:重新安装
注意:rpm可以自带脚本;
四类:--noscripts
preinstall:安装过程开始之前运行的脚本,%pre , --nopre
postinstall:安装过程完成之后运行的脚本,%post , --nopost
preuninstall:卸载过程真正开始执行之前运行的脚本,%preun, --nopreun
postuninstall:卸载过程完成之后运行的脚本,%postun , --nopostun
--nosignature:不检查包签名信息,不检查来源合法性;
--nodigest:不检查包完整性信息;
示例:
[[email protected] ~]# ls
anaconda-ks.cfg functions motd
CentOS7搭建网络yum源.sh java-1.7.0-openjdk-1.7.0.191-2.6.15.4.el7_5.x86_64.rpm yum.log
fstab java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.i686.rpm zsh-5.0.2-28.el7.x86_64.rpm
fstab.new java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64.rpm
fstab.patch ldirectord-3.9.6-0rc1.1.2.x86_64.rpm
[[email protected] ~]# rpm -ivh zsh-5.0.2-28.el7.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:zsh-5.0.2-28.el7 ################################# [100%]
[[email protected] ~]# rpm -qa | grep zsh
zsh-5.0.2-28.el7.x86_64
升级:
rpm {-U|--upgrade} [install-options] PACKAGE_FILE ...
rpm {-F|--freshen} [install-options] PACKAGE_FILE ...
-U:升级或安装;
-F:升级
rpm -Uvh PACKAGE_FILE ...
rpm -Fvh PACKAGE_FILE ...
--oldpackage:降级;
--force:强制升级;
注意:(1) 不要对内核做升级操作;Linux支持多内核版本并存,因此,直接安装新版本内核;
(2) 如果某原程序包的配置文件安装后曾被修改过,升级时,新版本的程序提供的同一个配置文件不会覆盖原有版本的配置文件,而是把新版本的配置文件重命名(FILENAME.rpmnew)后提供;
卸载:
rpm {-e|--erase} [--allmatches] [--nodeps] [--noscripts] [--test] PACKAGE_NAME ...
--allmatches:卸载所有匹配指定名称的程序包的各版本;
--nodeps:忽略依赖关系
--test:测试卸载,dry run模式
示例:
[[email protected] ~]# rpm -qa | grep zsh
zsh-5.0.2-28.el7.x86_64
[[email protected] ~]# rpm -evh zsh
Preparing... ################################# [100%]
Cleaning up / removing...
1:zsh-5.0.2-28.el7 ################################# [100%]
查询:
rpm {-q|--query} [select-options] [query-options]
[select-options]
PACKAGE_NAME:查询指定的程序包是否已经安装,及其版本;
-a, --all:查询所有已经安装过的包;
-f FILE:查询指定的文件由哪个程序包安装生成;
-p, --package PACKAGE_FILE:用于实现对未安装的程序包执行查询操作;
--whatprovides CAPABILITY:查询指定的CAPABILITY由哪个程序包提供;
--whatrequires CAPABILITY:查询指定的CAPABILITY被哪个包所依赖;
[query-options]
--changelog:查询rpm包的changlog;
-l, --list:程序安装生成的所有文件列表;
-i, --info:程序包相关的信息,版本号、大小、所属的包组,等;
-c, --configfiles:查询指定的程序包提供的配置文件;
-d, --docfiles:查询指定的程序包提供的文档;
--provides:列出指定的程序包提供的所有的CAPABILITY;
-R, --requires:查询指定的程序包的依赖关系;
--scripts:查看程序包自带的脚本片断;
用法:
-qi PACKAGE, -qf FILE, -qc PACKAGE, -ql PACKAGE, -qd PACKAGE
-qpi PACKAGE_FILE, -qpl PACKAGE_FILE, -qpc PACKAGE_FILE, ...
示例:
[[email protected] ~]# rpm -qf /bin/cd
bash-4.2.46-28.el7.x86_64
[[email protected] ~]# rpm -qf /bin/pwd
coreutils-8.22-18.el7.x86_64
[[email protected] ~]# rpm -ql zsh
/bin/zsh
/etc/skel/.zshrc
/etc/zlogin
/etc/zlogout
/etc/zprofile
/etc/zshenv
/etc/zshrc
[[email protected] ~]# rpm -qi zsh
Name : zsh
Version : 5.0.2
Release : 28.el7
Architecture: x86_64
Install Date: Sun 16 Sep 2018 12:32:31 AM EDT
Group : System Environment/Shells
Size : 5855982
License : MIT
Signature : RSA/SHA256, Thu 10 Aug 2017 04:28:17 PM EDT, Key ID 24c6a8a7f4a80eb5
Source RPM : zsh-5.0.2-28.el7.src.rpm
Build Date : Wed 02 Aug 2017 06:52:37 AM EDT
Build Host : c1bm.rdu2.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://zsh.sourceforge.net/
Summary : Powerful interactive shell
[[email protected] ~]# rpm -qc zsh
/etc/skel/.zshrc
/etc/zlogin
/etc/zlogout
/etc/zprofile
/etc/zshenv
/etc/zshrc
[[email protected] ~]# rpm -q --scripts zsh
postinstall scriptlet (using /bin/sh):
if [ ! -f /etc/shells ] ; then
echo "/bin/zsh" > /etc/shells
else
grep -q "^/bin/zsh$" /etc/shells || echo "/bin/zsh" >> /etc/shells
fi
if [ -f /usr/share/info/zsh.info.gz ]; then
# This is needed so that --excludedocs works.
/sbin/install-info /usr/share/info/zsh.info.gz /usr/share/info/dir --entry="* zsh: (zsh). An enhanced bourne shell."
fi
:
preuninstall scriptlet (using /bin/sh):
if [ "$1" = 0 ] ; then
if [ -f /usr/share/info/zsh.info.gz ]; then
# This is needed so that --excludedocs works.
/sbin/install-info --delete /usr/share/info/zsh.info.gz /usr/share/info/dir --entry="* zsh: (zsh). An enhanced bourne shell."
fi
fi
:
postuninstall scriptlet (using /bin/sh):
if [ "$1" = 0 ] ; then
if [ -f /etc/shells ] ; then
TmpFile=`/bin/mktemp /tmp/.zshrpmXXXXXX`
grep -v ‘^/bin/zsh$‘ /etc/shells > $TmpFile
cp -f $TmpFile /etc/shells
rm -f $TmpFile
fi
fi
校验:
rpm {-V|--verify} [select-options] [verify-options]
S file Size differs
M Mode differs (includes permissions and file type)
5 digest (formerly MD5 sum) differs
D Device major/minor number mismatch
L readLink(2) path mismatch
U User ownership differs
G Group ownership differs
T mTime differs
P caPabilities differ
包来源合法性验正和完整性验正:
来源合法性验正:
完整性验正:
获取并导入信任的包制作者的密钥:
对于CentOS发行版来说:rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
验正:
(1) 安装此组织签名的程序时,会自动执行验正;
(2) 手动验正:rpm -K PACKAGE_FILE
示例:
手动验证
[[email protected] ~]# rpm -K zsh-5.0.2-28.el7.x86_64.rpm
zsh-5.0.2-28.el7.x86_64.rpm: rsa sha1 (md5) pgp md5 OK
[[email protected] ~]# rpm -import /etc/pki/rpm-gpg/RPM-GPG-KEY-
RPM-GPG-KEY-CentOS-7 RPM-GPG-KEY-CentOS-Testing-7
RPM-GPG-KEY-CentOS-Debug-7 RPM-GPG-KEY-EPEL-7
[[email protected] ~]# rpm -import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[[email protected] ~]# rpm -K zsh-5.0.2-28.el7.x86_64.rpm
zsh-5.0.2-28.el7.x86_64.rpm: rsa sha1 (md5) pgp md5 OK
数据库重建:
rpm管理器数据库路径:/var/lib/rpm/
查询操作:通过此处的数据库进行;
获取帮助:
CentOS 6:man rpm
CentOS 7:man rpmdb
rpm {--initdb|--rebuilddb} [--dbpath DIRECTORY] [--root DIRECTORY]
--initdb:初始化数据库,当前无任何数据库可实始化创建一个新的;当前有时不执行任何操作;
--rebuilddb:重新构建,通过读取当前系统上所有已经安装过的程序包进行重新创建;
示例:
RPMDB(8) System Manager‘s Manual RPMDB(8)
NAME
rpmdb - RPM Database Tool
SYNOPSIS
rpm {--initdb|--rebuilddb}
DESCRIPTION
The general form of an rpm rebuild database command is
rpm {--initdb|--rebuilddb} [-v] [--dbpath DIRECTORY] [--root DIRECTORY]
Use --initdb to create a new database if one doesn‘t already exist (existing database is not overwrit‐
ten), use --rebuilddb to rebuild the database indices from the installed package headers.
SEE ALSO
popt(3),
rpm(8),
rpmkeys(8),
rpmsign(8),
rpm2cpio(8),
rpmbuild(8),
rpmspec(8),
rpm --help - as rpm supports customizing the options via popt aliases it‘s impossible to guarantee that
what‘s described in the manual matches what‘s available.
http://www.rpm.org/ URL:http://www.rpm.org/
yum命令的用法:
yum [options] [command] [package ...]
command is one of:
* install package1 [package2] [...]
* update [package1] [package2] [...]
* update-to [package1] [package2] [...]
* check-update
* upgrade [package1] [package2] [...]
* upgrade-to [package1] [package2] [...]
* distribution-synchronization [package1] [package2] [...]
* remove | erase package1 [package2] [...]
* list [...]
* info [...]
* provides | whatprovides feature1 [feature2] [...]
* clean [ packages | metadata | expire-cache | rpmdb | plugins | all ]
* makecache
* groupinstall group1 [group2] [...]
* groupupdate group1 [group2] [...]
* grouplist [hidden] [groupwildcard] [...]
* groupremove group1 [group2] [...]
* groupinfo group1 [...]
* search string1 [string2] [...]
* shell [filename]
* resolvedep dep1 [dep2] [...]
* localinstall rpmfile1 [rpmfile2] [...]
(maintained for legacy reasons only - use install)
* localupdate rpmfile1 [rpmfile2] [...]
(maintained for legacy reasons only - use update)
* reinstall package1 [package2] [...]
* downgrade package1 [package2] [...]
* deplist package1 [package2] [...]
* repolist [all|enabled|disabled]
* version [ all | installed | available | group-* | nogroups* | grouplist | groupinfo ]
* history [info|list|packages-list|packages-info|summary|addon-info|redo|undo|rollback|new|sync|stats]
* check
* help [command]
显示仓库列表:
repolist [all|enabled|disabled]
显示程序包:
list
# yum list [all | glob_exp1] [glob_exp2] [...]
# yum list {available|installed|updates} [glob_exp1] [...]
安装程序包:
install package1 [package2] [...]
reinstall package1 [package2] [...] (重新安装)
升级程序包:
update [package1] [package2] [...]
downgrade package1 [package2] [...] (降级)
检查可用升级:
check-update
卸载程序包:
remove | erase package1 [package2] [...]
查看程序包information:
info [...]
查看指定的特性(可以是某文件)是由哪个程序包所提供:
provides | whatprovides feature1 [feature2] [...]
清理本地缓存:
clean [ packages | metadata | expire-cache | rpmdb | plugins | all ]
构建缓存:
makecache
搜索:
search string1 [string2] [...]
以指定的关键字搜索程序包名及summary信息;
查看指定包所依赖的capabilities:
deplist package1 [package2] [...]
查看yum事务历史:
history [info|list|packages-list|packages-info|summary|addon-info|redo|undo|rollback|new|sync|stats]
安装及升级本地程序包:
* localinstall rpmfile1 [rpmfile2] [...]
(maintained for legacy reasons only - use install)
* localupdate rpmfile1 [rpmfile2] [...]
(maintained for legacy reasons only - use update)
包组管理的相关命令:
* groupinstall group1 [group2] [...]
* groupupdate group1 [group2] [...]
* grouplist [hidden] [groupwildcard] [...]
* groupremove group1 [group2] [...]
* groupinfo group1 [...]
示例:
[[email protected] ~]# yum -y install java
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: ftp.cuhk.edu.hk
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package java-1.8.0-openjdk.x86_64 1:1.8.0.181-3.b13.el7_5 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================================
Package Arch Version Repository Size
========================================================================================================================
Installing:
java-1.8.0-openjdk x86_64 1:1.8.0.181-3.b13.el7_5 updates 250 k
Transaction Summary
========================================================================================================================
Install 1 Package
Total download size: 250 k
Installed size: 501 k
Downloading packages:
java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64.rpm | 250 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64 1/1
Verifying : 1:java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64 1/1
Installed:
java-1.8.0-openjdk.x86_64 1:1.8.0.181-3.b13.el7_5
Complete!
[[email protected] ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: ftp.cuhk.edu.hk
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
repo id repo name status
base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 9,911
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 12,685
extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 402
updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 1,336
repolist: 24,334
[email protected] ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base epel extras updates
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors
-
2、自建yum仓库,分别为网络源和本地源
本地yum源
[[email protected] ~]# mount -r -t iso9660 /dev/cdrom /media/cdrom [[email protected] ~]# ls -l /media/cdrom/ total 1586 -rw-rw-r--. 3 root root 14 Sep 5 2017 CentOS_BuildTag drwxr-xr-x. 3 root root 2048 Sep 5 2017 EFI -rw-rw-r--. 3 root root 227 Aug 30 2017 EULA -rw-rw-r--. 3 root root 18009 Dec 9 2015 GPL drwxr-xr-x. 3 root root 2048 Sep 6 2017 images drwxr-xr-x. 2 root root 2048 Sep 5 2017 isolinux drwxr-xr-x. 2 root root 2048 Sep 5 2017 LiveOS drwxrwxr-x. 2 root root 1585152 Sep 5 2017 Packages drwxrwxr-x. 2 root root 4096 Sep 5 2017 repodata -rw-rw-r--. 3 root root 1690 Dec 9 2015 RPM-GPG-KEY-CentOS-7 -rw-rw-r--. 3 root root 1690 Dec 9 2015 RPM-GPG-KEY-CentOS-Testing-7 -r--r--r--. 1 root root 2883 Sep 6 2017 TRANS.TBL [[email protected] ~]# cd /media/cdrom/repodata/ [[email protected] repodata]# ls 0c34273ad0292747ee5e15c047d3e51c67ca59861a446972db45d71abacc7ad7-primary.sqlite.bz2 38b60f66d52704cffb8696750b2b6552438c1ace283bc2cf22408b0ba0e4cbfa-c7-x86_64-comps.xml 6cd606547d4f569538d4090e9accdc3c69964de1116b9ab1e0a7864bb1f3ec98-filelists.sqlite.bz2 9346184be1deb727caf4b1ecf4a7949155da5da74af9b92c172687b290a773df-c7-x86_64-comps.xml.gz a0af68e1057f6b03a36894d3a4f267bbe0590327423d0005d95566fb58cd7a29-other.xml.gz b686d3a0f337323e656d9387b9a76ce6808b26255fc3a138b1a87d3b1cb95ed5-primary.xml.gz c1561546c684bd06b3a499c2babc35c761b37b2fc331677eca12f0c769b1bb37-filelists.xml.gz cf0cc856d46b3095106da78256fb28f9d8defea4118d0e75eab07dc53b7d3f0d-other.sqlite.bz2 repomd.xml repomd.xml.asc TRANS.TBL [[email protected] repodata]# cat /etc/yum.repos.d/ CentOS-Base.repo CentOS-Debuginfo.repo.bak CentOS-Sources.repo.bak epel-testing.repo CentOS-Base.repo.bak CentOS-fasttrack.repo.bak CentOS-Vault.repo.bak CentOS-CR.repo.bak CentOS-Media.repo.bak epel.repo [[email protected] ~]# cd /etc/yum.repos.d/ [[email protected] yum.repos.d]# mkdir back [[email protected] yum.repos.d]# mv *.repo ./back [[email protected] yum.repos.d]# cd back/ [[email protected] back]# ls CentOS-Base.repo epel.repo epel-testing.repo [[email protected] back]# vim mybase.repo [[email protected] back]# cat /etc/yum.repos.d/back/mybase.repo [mybase] name=my base repo baseurl=file:///media/cdrom/repodata gpgcheck=0 enable=1
网络yum 源脚本
#!/bin/bash
#CentOS 7 configures the network yum source
#System environment:CentOS Linux release 7.5.1804 (Core)
#Exit values:0 is normal,13 has no network or network exception, 222 yum source failed to build
#Date:2018-8-25
#Modify time:2018-9-13
#v1.0
#by:ZDF
set -v
Yum() {
find ‘/etc/yum.repos.d/‘ -name *.repo -exec mv {} {}‘.bak‘ ;
$(which wget &> /dev/null)
if [ $? -eq 0 ];then
wget -q -t 5 -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
else
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
fi
if [ $? -eq 0 ];then
yum clean all && yum list all && yum repolist
fi
if [ $? -eq 0 ];then
echo -e ‘ 33[32mCentOS network yum source has been configured successfully and you can now use yum to install or uninsta programs. 33[0m‘
echo -e ‘ 33[1;34mWhether to install EPEL source? (Y|N) 33[m‘ ; read Epel
if [[ "$Epel" == Y || "$Epel" == y ]];then
yum -y install epel-release
[ "$?" -eq 0 ] && echo -e ‘ 33[32mCentOS EPEL source construction is completed 33[0m‘
fi
else
echo -e ‘ 33[47;31;1mCentOS network yum source configuration failed, please confirm! 33[0m‘ && exit 222
fi
exit "$?"
}
ping -c4 www.baidu.com
if [ $? -ne 0 ];then
echo -e ‘ 33[31;1mError, please check the network. 33[0m‘ ; exit 13
else
if [ -f ./yum.log ];then
echo >> ./yum.log
echo -e ‘ 33[1;31m===== Network YUM source installation log information ===== 33[m‘ | tee -a yum.log
Yum | tee -a yum.log
else
echo -e ‘ 33[1;31m===== New network yum source ===== 33[m‘ | tee yum.log
Yum | tee -a yum.log
fi
fi
#-_-#**END**-_-~
-
3、简述at和crontab命令,制定 每周三凌晨三、五点10分执行某个脚本,输出当前时间,时间格式为 2017-12-28 10:00:00
at和crontab都是计划任务执行命令,未来的某个时间点执行一次某任务用at,而周期性在某个时间点执行任务用crontab
at命令:
at [OPTION]... TIME
TIME:
HH:MM [YYYY-mm-dd]
noon,midnight, teatime
tomorrow
now+#
UNIT:minutes, hours, days, OR weeks
at的作业有队列,用单个字母表示,默认都使用a队列;
常用选项:
-l:查看作业队列,相当于atq
-f /PATH/FROM/SOMEFILE:从指定文件中读取作业任务,而不用再交互式输入;
-d:删除指定的作业,相当于atrm;
-c:查看指定作业的具体内容;
-q QUEUE:指明队列;
注意:作业执行结果是以邮件发送给提交作业的用户;
示例:
[[email protected] ~]# at now+2min
at> cat /etc/fstab
at> cat /etc/issue
at> <EOT>
job 3 at Sun Sep 16 01:46:00 2018
Can‘t open /var/run/atd.pid to signal atd. No atd running?
[[email protected] ~]# at -l
1 Sun Sep 16 01:45:00 2018 a root
3 Sun Sep 16 01:46:00 2018 a root
crontab命令:
crontab [-u user] [-l | -r | -e] [-i]
-e:编辑任务;
-l:列出所有任务;
-r:移除所有任务;即删除/var/spool/cron/USERNAME文件;
-i:在使用-r选项移除所有任务时提示用户确认;
-u user:root用户可为指定用户管理cron任务;
用户cron的配置格式:/var/spool/cron/USERNAME
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * command to be executed
注意:
(1) 每行定义一个cron任务,共6个字段;
(2) 此处的环境变量不同于用户登录后获得的环境,因此,建议命令使用绝对路径,或者自定义PATH环境变量;
(3) 邮件发送给当前用户;
注意:运行结果以邮件通知给当前用户;如果拒绝接收邮件:
(1) COMMAND > /dev/null
(2) COMMAND &> /dev/null
注意:定义COMMAND时,如果命令需要用到%,需要对其转义;但放置于单引号中的%不用转义亦可;
示例:每周三凌晨三、五点10分执行某个脚本,输出当前时间,时间格式为 2017-12-28 10:00:00
[[email protected] ~]# date +%F-%T
2018-09-16-03:50:36
[[email protected] ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[[email protected] ~]# vim date.sh
[[email protected] ~]# cat date.sh
#!/bin/bash
#
date +%F-%T
[[email protected] ~]# bash -x date.sh
+ date +%F-%T
2018-09-16-03:56:41
[[email protected] ~]# chmod +x date.sh
[[email protected] ~]# ls -l date.sh
-rwxr-xr-x. 1 root root 26 Sep 16 03:54 date.sh
[[email protected] ~]# crontab -l
10 3,5 * * 3 ./date.sh
-
4、简述sed常用操作命令,并举例
sed命令:
文本处理三剑客:
grep, egrep, fgrep:文本过滤器
sed:Stream EDitor,流编辑器,行
awk:文本格式化工具,报告生成器sed [OPTION]... ‘script‘ [input-file] ...
script:
地址定界编辑命令常用选项: -n:不输出模式空间中的内容至屏幕; -e script, --expression=script:多点编辑; -f /PATH/TO/SED_SCRIPT_FILE 每行一个编辑命令; -r, --regexp-extended:支持使用扩展正则表达式; -i[SUFFIX], --in-place[=SUFFIX]:直接编辑原文件 ; ~]# sed -e ‘[email protected]^#[[:space:]]*@@‘ -e ‘/^UUID/d‘ /etc/fstab 地址定界: (1) 空地址:对全文进行处理; (2) 单地址: #:指定行; /pattern/:被此模式所匹配到的每一行; (3) 地址范围 #,#: #,+#: #,/pat1/ /pat1/,/pat2/ $:最后一行; (4) 步进:~ 1~2:所有奇数行 2~2:所有偶数行 编辑命令: d:删除; p:显示模式空间中的内容; a ext:在行后面追加文本“text”,支持使用 实现多行追加; i ext:在行前面插入文本“text”,支持使用 实现多行插入; c ext:把匹配到的行替换为此处指定的文本“text”; w /PATH/TO/SOMEFILE:保存模式空间匹配到的行至指定的文件中; r /PATH/FROM/SOMEFILE:读取指定文件的内容至当前文件被模式匹配到的行后面;文件合并; =:为模式匹配到的行打印行号; !:条件取反; 地址定界!编辑命令; s///:查找替换,其分隔符可自行指定,常用的有[email protected]@@, s###等; 替换标记: g:全局替换; w /PATH/TO/SOMEFILE:将替换成功的结果保存至指定文件中; p:显示替换成功的行;
示例:
显示偶数行;[[email protected] ~]# sed -n ‘n;p‘ /etc/fstab # # Created by anaconda on Wed Sep 5 21:24:35 2018 # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # UUID=38a2a3be-952e-4541-814b-77553d4e9204 /boot xfs defaults 0 0
逆序显示文件的内容;
[[email protected] ~]# sed -n ‘n;p‘ /etc/fstab # # Created by anaconda on Wed Sep 5 21:24:35 2018 # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # UUID=38a2a3be-952e-4541-814b-77553d4e9204 /boot xfs defaults 0 0 [[email protected] ~]# sed ‘1!G;h;$!d‘ /etc/fstab /dev/mapper/centos-swap swap swap defaults 0 0 UUID=38a2a3be-952e-4541-814b-77553d4e9204 /boot xfs defaults 0 0 /dev/mapper/centos-root / xfs defaults 0 0 # # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # # Created by anaconda on Wed Sep 5 21:24:35 2018 # /etc/fstab #
取出文件后两行;
[[email protected] ~]# sed ‘$!N;$!D‘ /etc/fstab UUID=38a2a3be-952e-4541-814b-77553d4e9204 /boot xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0
删除原有的所有空白行,而后为所有的非空白行后添加一个空白行;
[[email protected] ~]# sed ‘/^$/d;G‘ /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Sep 5 21:24:35 2018
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=38a2a3be-952e-4541-814b-77553d4e9204 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
显示奇数行;
[[email protected] ~]# sed ‘n;d‘ /etc/fstab
# /etc/fstab
#
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
/dev/mapper/centos-root / xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
以上是关于Linux 程序包管理及sed基础的主要内容,如果未能解决你的问题,请参考以下文章
linux应用程序基础及安装方式(RPM,YUM和源代码编译)!