Bash 计算文件和目录

F_CNT=0
D_CNT=0
for FILE in *; do
  test -f "$FILE" && F_CNT=`expr $F_CNT + 1`
  test -d "$FILE" && D_CNT=`expr $D_CNT + 1`
done
echo "$F_CNT files & $D_CNT dirs in current directory."

Bash 使用Launchd和电子邮件管理员禁用心跳

#!/bin/bash

###
# Stop heartbeatd so that failoverd initiates IPFailover
# leaving network interfaces intact.
###
# Setup mail
###
C_NAME=`/usr/sbin/scutil --get ComputerName`
NOTIFY="admin@admin.com"
WARNING="Heartbeatd has been halted - Failover process initiated"
###
# End Setup mail
###
HEARTD=`/bin/ps -axcu | /usr/bin/grep -i heartbeatd > /dev/null; echo $?`

if [ ! ${HEARTD} -eq 0 ];
then
        echo "heartbeatd is not running - uh oh..."
        /usr/bin/logger "FAILOVER: heartbeatd is not running - uh oh..."

        echo From: ${C_NAME} - ${WARNING} > /tmp/heartbeat_warning.txt | mail -s \
        "FAILOVER PROCESS: Heartbeatd was not running" ${NOTIFY} < /tmp/heartbeat_warning.txt
else
        launchctl unload /Library/LaunchDaemons/uk.ac.rave.heartbeatd.plist
        while [ $(/bin/ps -axcu | /usr/bin/grep -i heartbeatd > /dev/null; echo $?) -eq 0 ];
        do
        echo "Halting heartbeatd"
        /usr/bin/logger "FAILOVER: Halting heartbeatd"
        /bin/sleep 5
        done

        echo "heartbeatd has been halted"
        /usr/bin/logger "FAILOVER: heartbeatd has been halted"

        rm -f /var/run/heartbeatd.pid

        echo From: ${C_NAME} - ${WARNING} > /tmp/heartbeat_warning.txt | mail -s \
        "FAILOVER PROCESS: Heartbeat halted - failover in process" ${NOTIFY} < /tmp/heartbeat_warning.txt
fi
exit 0

Bash 在shell上递归搜索所有文件

find /path/ -name "*.ext"  -print -exec grep "search for"  '{}' \; | less

Bash 如何在shell中设置Ubuntu中的静态IP

### Edit /etc/network/interfaces (in this example setup I will use the IP address 192.168.178.50 for eth0):


# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# This is a list of hotpluggable network interfaces.
# They will be activated automatically by the hotplug subsystem.
mapping hotplug
        script grep
        map eth0

# The primary network interface
auto eth0
iface eth0 inet static
        address 192.168.178.50
        netmask 255.255.255.0
        network 192.168.178.0
        broadcast 192.168.178.255
        gateway 192.168.178.1
        #optional
        #dns-nameserver 102.168.178.1



### You can also edit /etc/resolv.conf to configure a dns-server to use:

nameserver 192.168.178.1

Bash 一些Ubuntu配置和更新命令

### reconfigure an installed program
dpkg-reconfigure <name>

### reconfigure console setting
dpkg-reconfigure console-data

### reconfigure the whole system
dpkg-reconfigure -a

### setting up timezone
tzconfig

### update apt-get source list
apt-get update

### install a package (one can also use aptitude)
apt-get install <name>

### upgrade the whole system
apt-get dist-upgrade

Bash 在多个日志文件中搜索今天的帖子请求

grep `date '+%d/%b/%Y'.*POST` $(find /var/www/vhosts -name access_log) | less

Bash 计算子目录中的文件

find . -name '.svn' -prune -or -maxdepth 2 -type d -print -exec sh -c 'find {} -name ".svn" -prune -or -print | wc -l' \;

Bash 什么版本的Apache?

httpd -V

Bash 查看MacBook上是否已连接电源

ioreg -l | grep ExternalConnected | cut -d'=' -f 2

Bash 查找大文件

find . -size +10240000c -exec du -h {} \;