1. 数组
#定义数组
array=("163" "21cn" "sina" "qq")
#获取数组长度
echo ${#array[*]}
#遍历数组
for arr in ${array[*]}; do
echo $arr
done
2. 转换大小写
# 使用typeset后必须重新赋值才起作用
typeset -u content #upper
content="Show Me The Money"
echo $content
typeset -l content #lower
content="Show Me The Money"
echo $content
# 使用tr
content="Show Me The Money"
new_content=`tr '[a-z]' '[A-Z]' <<< "$content"`
echo $new_content
new_content=`tr '[A-Z]' '[a-z]' <<< "$content"`
echo $new_content
content="Show Me The Money"
new_content=$(echo "$content" | tr '[a-z]' '[A-Z]')
echo $new_content
new_content=$(echo "$content" | tr '[A-Z]' '[a-z]')
echo $new_content
3. 变量替换
${variable:-value}
当variable为空时,替换为value
${variable:+value}
当variable有值时,替换为value
${variable:=value}
当variable为空时,替换并赋值为value
${variable:?message}
当variable为空时,退出脚本,并标准错误输出message
${param#word}
从param头部开始删除到word匹配的最小部分,然后返回剩余的部分
${param##word}
从param头部开始删除到word匹配的最大部分,然后返回剩余的部分
${param%word}
从param尾部开始删除到word匹配的最小部分,然后返回剩余的部分
${param%%word}
从param尾部开始删除到word匹配的最大部分,然后返回剩余的部分
file="/home/fdipzone/shell/command.class.sh"
echo ${file#*/} # home/fdipzone/shell/command.class.sh
echo ${file##*/} # command.class.sh
echo ${file%/*} # /home/fdipzone/shell
echo ${file%%/*} # 空
echo ${file#*.} # class.sh
echo ${file##*.} # sh
echo ${file%.*} # /home/fdipzone/shell/command.class
echo ${file%%.*} # /home/fdipzone/shell/command
echo ${file/sh/php} # /home/fdipzone/phpell/command.class.sh
echo ${file//sh/php} # /home/fdipzone/phpell/command.class.php 替换多个
4.调试脚本程序
set -n #不执行脚本,只检查语法错误
set -v #在执行命令之前回显
set -x #在执行命令之后回显
set -u #如果使用了未定义变量,给出错误提示。
5.here文档
在shell脚本中向命令输入的方法。
cat <<!fdipzone!
Hello Wolrd
Shell Ok
!fdipzone!
6.参数变量
$$ Shell本身的PID(ProcessID)
$! Shell最后运行的后台Process的PID
$? 最后运行的命令的结束代码(返回值)
$- 使用Set命令设定的Flag一览
$* 所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
$@ 所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$# 添加到Shell的参数个数
$0 Shell本身的文件名
$1~$n 添加到Shell的各参数值。$1是第1参数、$2是第2参数
7.文件路径与文件名
#!/bin/bash
echo "dirname:$(dirname $0)"
echo "basename:$(basename $0)"
exit 0
8.读取输入数据
#!/bin/bash
# read命令 -p(提示语句) -n(字符最多个数) -t(等待时间) -s(不回显)
TIMEOUT=5
printf "Please input a number: "
read -t "$TIMEOUT" number
if [ "$number" ]; then
printf "number is: $number\n"
else
printf "\nnot input number\n"
fi
read -s -n10 -p "Please input the password:" password
printf "\nPassword is: $password\n"
exit 0
9.DEBUG
#!/bin/bash
DEBUG=true
function debug(){
"$DEBUG" && echo $(tput setaf 1;)"DEBUG >>>"$(tput sgr0)" $*"
}
debug 'file not exists'
exit 0
10.打印使用说明
#!/bin/bash
function print_info(){
cat <<!INFO!
Usage: myscript <command>
Version: $1
Available Commands
start: start worker
stop: stop worker
restart: restart worker
!INFO!
}
print_info 1.0
exit 0
11.使打印字符串有颜色
#!/bin/bash
NORMAL=$(tput sgr0)
GREEN=$(tput setaf 2; tput bold; tput setab 5)
YELLOW=$(tput setaf 3; tput setab 4)
RED=$(tput setaf 1)
function red(){
echo "$RED$*$NORMAL"
}
function green(){
echo "$GREEN$*$NORMAL"
}
function yellow(){
echo "$YELLOW$*$NORMAL"
}
green "Task has been completed"
red "The configuration file does not exist"
yellow "You have to use higher version."
exit 0
12. 生成指定位数的随机密码
# 生成8位随机密码
cat /dev/urandom | head -n 1 | md5sum | head -c 8
date +%N | md5sum | head -c 8
13. 截取字符串
var='1234567890abcdefg'
echo ${var:3:10} # 从字符串第3位开始截取,截取10个字符,输出4567890abc