Linux Grep

Linux 下面使用频率最高的命令之一,用于字符串搜索。支持正则表达式

普通匹配, 匹配普通字符串

$grep match_pattern filename

使用Grep进行多个文件搜索

$grep match_pattern file1 file2 file3

正则表达式匹配

$grep -E "[a-z][A-Z]" filename

正则表达式匹配使用egrep

$egrep "[a-z]" filename

打印除了匹配以外的结果

grep -v match_parrent filename

打印出统计行数

grep -c "[a-z]+" index.php

匹配电子邮件

grep -E "[a-z0-9_]+@[a-z0-9]+\.[a-z]+" ./ -R

匹配网址

grep -E "http://[a-zA-Z0-9.]+\.[a-zA-Z]{2,3}" ./ -R

Bash Array

#!/bin/bash

assarr=(1 2 3 4 5 5)
assarr[6]="hahah"


#输出指定的KEY
echo ${assarr[2]}

#输出数组所有
echo ${assarr[@]}
echo ${assarr[*]}

#关联数组
#定义一个关联数组
declare -A ass_array
ass_array=([apple]="1520" [orange]="1300")
echo ${ass_array[apple]}

#输出所有值
echo ${ass_array[*]}

#输出所有KEY
echo ${!ass_array[*]}

Nginx 创建虚拟主机BASH

#!/bin/bash

if [[ $# -lt 1 || $1 == "--help" || $1 == "-h" ]]
then
  echo "Usage:"
  echo "  sudo `basename $0` SITENAME"
  echo "Examples:"
  echo "  sudo `basename $0` drupal-8"
  exit
fi

WORKDIR="/home/zhouitpro/www"
APACHEDIR="/etc/nginx/sites-available"
APACHEENSITEDIR="/etc/nginx/sites-enabled"
HOSTSFILE="/etc/hosts"

echo "server {
    listen 80;
    root $WORKDIR/$1;
    server_name $1.localhost;
    location / {
        index index.php index.html index.htm;
    }
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }
    location ~ /\.ht {
        deny all;
    }
}" > $APACHEDIR/$1
sudo ln -s $APACHEDIR/$1 $APACHEENSITEDIR/$1
sudo /etc/init.d/nginx restart
grep -q "127.0.0.1  $1.localhost" $HOSTSFILE
if [ $? -ne 0 ]; then
  echo "127.0.0.1  $1.localhost" >> $HOSTSFILE
fi

VPN更新密码脚本


#!/bin/bash                                                                                                                                                  
printf '' > '/var/www/ssh110.com/data/vpnuser'
printf '' > '/etc/ppp/chap-secrets'
users=('vpn01' 'vpn02' 'vpn03' 'vpn04' 'vpn05' 'vpn06' 'vpn07' 'vpn08' 'vpn09' 'vpn10' 'vpn11' 'vpn12' 'vpn13' 'vpn14' 'vpn15' 'vpn16' 'vpn17' 'vpn18' 'vpn19' 'vpn20')
echo zhouitpro pptpd zhouitpro123 '*' >> '/etc/ppp/chap-secrets'
echo cm pptpd 602000 '*' >> '/etc/ppp/chap-secrets'
echo qcc pptpd qcc123456 '*' >> '/etc/ppp/chap-secrets'
for u in ${users[@]}; do
    pawd=''
    a=('0' '1' '2' '3' '4' '5' '6' '7' '8' '9' 'a' 'b' 'c' 'z' 'A' 'B' 'Z')
    for i in {1..10}
    do
        pawd=$pawd${a[$RANDOM % ${#a[*]}]}
    done
    echo $u : $pawd >> '/var/www/ssh110.com/data/vpnuser'
    echo $u 'pptpd' $pawd '*' >> '/etc/ppp/chap-secrets'
done;


SSH更新用户密码 脚本


#!/bin/bash
printf '' > '/var/www/ssh110.com/data/userpasswd'
users=('ssh01' 'ssh02' 'ssh03' 'ssh04' 'ssh05' 'ssh06' 'ssh07' 'ssh08' 'ssh09' 'ssh10' 'ssh11' 'ssh12' 'ssh13' 'ssh14' 'ssh15' 'ssh16' 'ssh17' 'ssh18' 'ssh19' 'ssh20')
for u in ${users[@]}; do
    pawd=''
    a=('0' '1' '2' '3' '4' '5' '6' '7' '8' '9' 'a' 'b' 'c' 'z' 'A' 'B' 'Z')
    for i in {1..10}
    do
        pawd=$pawd${a[$RANDOM % ${#a[*]}]}
    done
    echo $u : $pawd >> '/var/www/ssh110.com/data/userpasswd'
    echo $u:$pawd | chpasswd
done;


Apache 创建虚拟主机脚本

depoly-site.sh

$sudo depoly-site.sh drupal
#!/bin/bash

if [[ $# -lt 1 || $1 == "--help" || $1 == "-h" ]]
then
  echo "Usage:"
  echo "  sudo `basename $0` SITENAME"
  echo "Examples:"
  echo "  sudo `basename $0` drupal-8"
  exit
fi

WORKDIR="/home/zhouitpro/www"
APACHEDIR="/etc/apache2/sites-available"
HOSTSFILE="/etc/hosts"

echo "
	ServerAlias $1.localhost
	DocumentRoot $WORKDIR/$1
	
		Options FollowSymLinks
		AllowOverride All
        
" > $APACHEDIR/$1
a2ensite $1
service apache2 restart
grep -q "127.0.0.1  $1.localhost" $HOSTSFILE
if [ $? -ne 0 ]; then
  echo "127.0.0.1  $1.localhost" >> $HOSTSFILE
fi

Drupal 常用模块安装

#!/bin/bash
 
enable_modules=('admin_menu' 'module_filter' 'token' 'ctools' 'pathauto' 'ds' 'devel' 'views' 'context')
disable_modules=('firephp' 'overlay' 'toolbar' 'color' 'dashboard')
 
for dmodule in ${disable_modules[@]}; do
    sudo drush pm-disable $dmodule -y
done
 
for emodule in ${enable_modules[@]}; do
   sudo drush dl $emodule -y
done;
 
for emodule in ${enable_modules[@]}; do
   sudo drush pm-enable $emodule -y
done;