查看目录
tree -i -f | awk '{if(!system("test -d "$1))print $1}'
批量快速创建user
for i in user{0..10}; do useradd $i echo 123456 | passwd --stdin $i done
使用if语句判断
#!/bin/bash #文件判断 if [ -e $1 ]; then echo "file exists" fi if [ -r $1 ]; then echo "readable" fi if [ -w $1 ]; then echo "writable" fi if [ -x $1 ]; then echo "executeable" fi if [ -s $1 ]; then echo "have contents" fi if [ -d $1 ]; then echo "directory" fi if [ -f $1 ]; then echo "file" fi if [ -c $1 ]; then echo "charactor device" fi if [ -b $1 ]; then echo "block device" fi #字符串判断 if [ $1 = "admin" ]; then echo "you are admin" fi if [ $1 != "demon" ]; then echo "you are not demon" fi if [ -z $1 ]; then echo "zero string" fi if [ -n $1 ]; then echo "not empty" fi #数字判断 if test $1 -eq 4; then echo "$1=4" fi if test $1 -ne 4; then echo "$1!=4" fi if test $1 -gt 4; then echo "$1>4" fi if test $1 -ge 4; then echo "$1>=4" fi if test $1 -lt 4; then echo "$1<4" fi if test $1 -le 4; then echo "$1<=4" fi #C语言语法 if (( $1 != 'demon' )); then echo "[C]not demon" elif (( $1 > 5 )); then echo "[C]$1 > 5" fi #更兼容的用法 if [[ $str == "hello" ]]; then echo $str fi if [[ -d $dir ]]; then echo $dir is directory fi
循环 与 选择
#POSIX用法
for ((i=0;i<10;i++)); do if [ $i -eq 5 ]; then continue elif [ $i -eq 9 ]; then break; fi echo $i done read -p "Enter a char: " char case $char in [a-z]|[A-Z]) echo "A letter" ;; [0-9]) echo "A digut" ;; *) echo "function key" ;; esac echo '$#: ' $# echo '$@: ' $@ echo '$*: ' $*
#shift用法 while [ $# -gt 0 ]; do if [[ $str == "" ]]; then str=$1 else str="$str-$1" fi shift done echo $str
数组
#!/bin/bash a="hello" arr=($a 1 2 "c" d) length=${#arr} # 5 arr[5]=$arr echo length is $length # 6 for i in ${arr[@]}; do echo $i done for i in `seq 0 $length`; do # 0 ~ 5 arr[$i]=$i done i=0 while [ $i -lt ${#arr[*]} ]; do echo ${arr[$i]} let "++i" done
函数
function add(){ tot=1 for (( i=1;i<=$1;i++ )); do tot=$(($tot*$i)) done echo $tot } add 6 #720 function sum(){ return $(($1+$2)) } sum 30 40 echo "30 + 40 = $?" echo $$ #pid echo $- #set echo $! #the lastest pid
cut 提取用户名
cut -d: -f1 /etc/passwd
cat /etcpasswd | awk -F: '{print "username: "$1"("$2")"}'
awk 一些常见用法
for i in `awk -F: '{print $1}' /etc/passwd |head` ; dosleep 1 echo $i done for i in `df -Th | awk '{if(NR==2)print int($6)}'`; do echo usage: $i% done df -Th | awk 'END{print "row: "NR " col: "NF}'
测试文件[access.log]
192.168.10.1 root.php 192.168.10.2 bin.php 192.168.10.4 daemon.php 192.168.10.2 adm.php 192.168.10.1 lp.php 192.168.10.1 sync.php 192.168.10.2 shutdown.php 192.168.10.6 halt.php 192.168.10.2 mail.php 192.168.10.2 uucp.php 192.168.10.2 operator.php 192.168.10.3 games.php 192.168.10.4 gopher.php 192.168.10.1 ftp.php 192.168.10.7 nobody.php 192.168.10.2 vcsa.php 192.168.10.4 abrt.php 192.168.10.4 ntp.php 192.168.10.3 saslauth.php 192.168.10.2 postfix.php 192.168.10.3 sshd.php 192.168.10.2 tcpdump.php 192.168.10.3 dbus.php 192.168.10.1 apache.php 192.168.10.8 mysql.php
awk 过滤 192.168.10.1 访问的记录
cat access.log | awk '$1 ~ /192.168.10.1/ {print $0}'
awk 过滤非 192.168.10.1 访问的记录
cat access.log | awk '$1 !~ /192.168.10.1/ {print $0}'
sed 只打印第5行数据
cat passwd |sed -n '5'p
sed 打印第5到8行数据
cat passwd |sed -n '5,8'p
sed 屏蔽第3到20行数据
cat passwd |sed '3,20'd
sed 打印登录用户
cat passwd | sed -n '/bash/'p
sed 显示第一行到包含sync的行
cat passwd | sed -n 1,/sync/p
sed 显示从sshd到最后一行
cat /etc/passwd | sed -n '/sshd/,$'p
uniq 相同合并并统计
cat access.log | awk '{print $1}' | sort | uniq -c
uniq 打印出现超过一次的行
cat access.log | awk '{print $1}' | sort | uniq -d
sort 按字母升序
cat access.log | awk '{print $1}' | sort
sort -r 按字母降序
cat access.log | awk '{print $1}' | sort -r
sort 也可以自己分割文件排序,不用awk, -t指定分隔符默认空格, -k指定按哪一列排序
cat access.log | sort -t: -k1
split 将文件进行分割成多个小文件
split -5 passwd splitname
#ls
#passwd splitnameaa splitnameab splitnameac splitnamead splitnameae
颜色库
#!/bin/bash #name color.sh
#auth demonxian3
da=` echo -e "