本文转载自Shell小技巧
gzip -t a.tar.gz if [[ 0 == $? ]]; then echo "good zip" else echo "bad zip" fi
可以简化为:
gzip -t a.tar.gz && echo "good zip" || echo "bad zip"
while getopts ":a:b:c" OPT; do case $OPT in a) arg_a=$OPTARG";; b) arg_b=$OPTARG;; c) arg_c=true;; ?) ;; esac done shift $((OPTIND-1))
$ stat -c %s fw8ben.pdf
替换第一个:${string/pattern/replacement} 替换全部:${string//pattern/replacement}
$ a='a,b,c'
$ echo ${a/,/ } #替换第一个
$ echo ${a//,/ } #替换全部
$ a b c
string='My string' if [[ $string == *My* ]]; then echo "It's there!" fi
1>File 2>&1
rsync -r -t -v /source_folder /destination_folder rsync -r -t -v /source_folder [user@]host:/destination_folder
注:命令执行后destinationfolder内将包含一个名为sourcefolder的目录。
#为所有的txt文件加上.bak后缀: rename '.txt' '.txt.bak' *.txt #去掉所有的bak后缀: rename '.bak' '' *.bak
echo $LANG /etc/sysconfig/i18n
for ((i=0; i < 10; i++)); do echo $i; done for line in $(cat a.txt); do echo $line; done for f in *.txt; do echo $f; done while read line ; do echo $line; done < a.txt cat a.txt | while read line; do echo $line; done
pkill swiftfox #根据名称终止进程 kill -9 <pid> #根据pid终止进程
find ~/tmp -name "*abc*.txt" -mtime -5 #在~/tmp目录下查找名为*abc*.txt且修改时间为5天内的文件
cat a.txt | sed -e '/^$/d' $ (echo "abc "; echo ""; echo "ddd";) | awk '{ if(0!=NF) print $0;}'
[[ file1.txt -nt file2.txt ]] && echo true || echo false #-nt means "newer than"
# -t 10: warning与kill signal的间隔时间10s;+30: 分钟后定时关机 nohup shutdown -t 10 +30 &
$echo '2011-07-15 server_log_123.log hello world' | grep -o 'server_log_[0-9]\+\.log' server_log_123.log $echo '2011-07-15 server_log_123.log hello world' | sed 's/.*\(server_log_.*\.log\).*/\1/' server_log_123.log
$cat a.txt | tr -d '
' $dos2unix a.txt
hput() { eval "hkey_$1"="$2" } hget() { eval echo '${'"hkey_$1"'}' } $ hput k1 aaa $ hget k1 aaa
$ echo 'a b c d e f' | cut -d ' ' -f1,3- a c d e f
$ a=$( (echo 'out'; echo 'error' 1>&2) 2>&1 1>/dev/null) $ echo $a error
$cat a.txt | sed 1,3d
$ echo $foo | tr '[:lower:]' '[:upper:]' $ tr ‘[A-Z]’ ‘[a-z']’ < foo.txt
$ read a b c <<< “xxx yyy zzz” $ echo $b yyy
array=( one two three ) for i in ${array[@]} do echo $i done
$ df –h
$ du –sh ~/apps
$ cat /proc/cpuinfo
$ date +%Y-%m-%d 2012-12-24 $ date +%Y-%m-%d –date ‘-1 day’ 2012-12-23 $ date +%Y-m-%d –date ‘Dec 25’ 2011-12-25 $ date +%Y-m-%d –date ‘Dec 25 – 10 days’ 2011-12-15
1) 启动svn daemon
$ svnserve –d –r <svn_home>
$ vim /etc/sudoers
$ dirname ‘/home/todd/a.txt’ /home/todd $ basename ‘/home/todd/a.txt’ a.txt
$ finger weidagang [weidagang@localhost ~]$ finger weidagang Login: weidagang Name: weidagang Directory: /home/weidagang Shell: /bin/bash On since Sat Jan 7 14:15 (CST) on pts/2 from 10.69.22.141
[网卡] 配置文件:/etc/sysconfig/network-scripts/ifcfg-<i/f name> <静态IP分配实例> DEVICE=eth0 ONBOOT=yes BOOTPROTO=static IPADDR=192.168.168.11 NETMASK=255.255.255.0 GATEWAY=192.168.168.252 <DHCP实例> ONBOOT=yes TYPE=Ethernet DHCP_HOSTNAME=sleipnir.cullen.lesbell.com.au DEVICE=eth0 HWADDR=00:0c:6e:0a:3d:26 BOOTPROTO=dhcp USERCTL=no PEERDNS=yes 查看网卡状态:ifconfig 改变网络状态: <CentOS> service network {start|stop|restart|reload|status} 或 /etc/init.d/network {start|stop|restart|reload|status} <Ubuntu> /etc/init.d/networking restart [DNS] 配置文件:/etc/resolv.conf search cullen.lesbell.com.au lesbell.com.au nameserver 192.168.168.1 nameserver 192.168.168.252 查看域名解析结果:nslookup [Host] 配置文件:/etc/hosts 192.168.0.15 todd [ARP] 查看arp cache命令:arp –a [路由] 查看路由:netstat -nr 查看和修改路由表命令:route 跟踪路由:traceroute
$ netstat --tcp -l -n -p | grep 3306 tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 27238/mysqld
$ echo $HOSTNAME $ echo /proc/sys/kernel/hostname $ vim /etc/sysconfig/network
$ cat file1.txt aaa bbb ccc $cat file2.txt bbb ccc ddd #搜索仅出现在file1.txt中的行 $ comm –2 –3 file1.txt file2.txt aaa #搜索file1.txt和file2.txt都出现的行 $ comm –1 –2 file1.txt file2.txt bbb ccc
# 多字符作分隔符 $ echo "a||b||c||d" | awk -F '[|][|]' '{print $3}' c # 多种分隔符1 $echo "a||b,#c d" | awk -F '[| ,#]+' '{print $4}' d # 多种分隔符2 $echo "a||b##c|#d" | awk -F '([|][|])|([#][#])' '{print $NF}' c|#d
# 加密文件 $ openssl aes-256-cbc -a -salt -in secrets.txt -out secrets.txt.enc # 解密文件 $ openssl aes-256-cbc -d -a -in secrets.txt.enc -out secrets.txt.new
删除下面a.txt中最后一行的逗号:
a.txt ======================== pmNoOfSwDownHsCong, pmUlUpswitchAttemptHigh, pmUlUpswitchAttemptLow, pmUlUpswitchSuccessHigh, pmUlUpswitchSuccessLow, pmUpswitchFachHsAttempt, pmUpswitchFachHsSuccess, ======================== $ cat a.txt | sed '$s/,$//'
文本进制转换
$ echo "ibase=16;1F" | bc 31 $ echo "obase=16;31" | bc 1F $ printf "%x" 4095 fff $ printf "%.8x" 4095 00000fff $ printf "%d" 0xfff 4095 $ printf "%.6d" 0x000fff 004095
文本转二进制数
# big endianness $ printf "0: %.8x" "65534" | xxd -r -g0 >a.bin $ xxd a.bin 0000000: 0000 fffe # little endianness $ printf "0: %.8x" 65534 | sed -e 's/0\: \(..\)\(..\)\(..\)\(..\)/0\: \4\3\2\1/' | xxd -r -g0 > a.bin $ xxd a.bin 0000000: feff 0000