Linux常用经验操作

1. 文本查找替换

在linux文本处理中,尤其是在shell编程中,经常会用到查找替换,如关闭selinux等操作,根据经验可知,最有效的方法是sed的扩展正则表达式分组替换最快,如下举例,替换开头为root为python,此时用分组替换最为有效。

[root@localhost ~]#sed -i -r 's@^root(:.*)@python\1@' passwd
[root@localhost ~]#cat passwd 
python:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

2. 文本行插入数据

在初始化脚本中设定别名值的shell函数

set_alias (){
    sed -ri '/^#Source/i \alias yy="yum -y install"\n alias cdn="cd /etc/sysconfig/network-scripts/"' ~/.bashrc
    if [ $? -eq 0 ];then
        success
        echo -e "The new alias setting...\c"
    else
        failure
        echo -e "The new alias setting...\c"
    fi
bash ~/.bashrc

3. centos7设置开机启动

在centos7中由systemd接管开机启动,因此在开机启动时和centos6已经完全不同,因此在开机启动时,通常由俩种方式进行设置开机启动项

3.1继承centos6设置开机启动项

核心两步骤:

  • 修改/etc/rc.d/rc.local开机执行文件
    sed -ir "/^#.*boot./a \service gogs start" /etc/rc.d/rc.local

  • centos7张rc.local默认是没有执行权限,因此添加执行权限
    chmod +x /etc/rc.d/rc.local

具体操作如下:
[root@localhost ~]#sed -ir "/^#.*boot./a \service gogs start" /etc/rc.d/rc.local 
[root@localhost ~]#cat /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
service gogs start
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
service gogs start

3.2 采用systemd的方式进行开机启动

写一个systemd的开机服务启动脚本,通过systemctl enable gogs启动,这个有时间待续,暂时时间不够。

4. windows格式转Linux

4.1 windows写的脚本验证

在windows nodepad++写脚本时往往有时候会保存为windows格式,此时排查错误也无法排除,因此cat -A选项,进行验证,windows格式

[root@localhost init.d]#cat -A nginx 
#!/bin/bash^M$
#chkconfig: 2345 80 90^M$
#description:nginx run^M$
^M$
# nginxM-eM-^PM-/M-eM-^JM-(M-hM-^DM-^ZM-fM-^\M-,^M$
# @author^IDevil^M$
# @version^I0.0.1^M$
# @date^I^I2017-05-29^M$
^M$
PATH=/data/nginx/sbin^M$
DESC="nginx daemon"^M$
NAME=nginx^M$
DAEMON=/data/nginx/sbin/nginx^M$
CONFIGFILE=/data/nginx/conf/nginx.conf^M$
PIDFILE=/data/nginx/logs/nginx.pid^M$

4.2 vim : set fileformat=unix

将文本以vim打开,然后在命令模式下输入set fileformat=unix,最后在命令模式下保存退出。
再次打开

[root@localhost init.d]#cat -A nginx 
#!/bin/bash$
#chkconfig: 2345 80 90$
#description:nginx run$
$
# nginxM-eM-^PM-/M-eM-^JM-(M-hM-^DM-^ZM-fM-^\M-,$
# @author^IDevil$
# @version^I0.0.1$
# @date^I^I2017-05-29$
$
PATH=/data/nginx/sbin$
DESC="nginx daemon"$
NAME=nginx$
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值