用户工具

站点工具


linux:shell
  • bash
#! /bin/bash  //首行提示解释器
var=value  //变量命名不能有空格,value含空格时使用引号
printf "value of %s is %s\n" var $var //格式化输出
if grep pattern file > /dev/null; then //忽略所有输出,检查程序退出状态,找到模式时执行
read pass < /dev/tty  //提示输入密码,通常会stty -echo关闭回显,然后stty echo打开回显
&>file  //等价于 >file 2>&1,&>>append等价于 >>file 2>&1
  • tips
//命令提示符:echo $PS1 #[\u@\h \W]\$
vi ~/.bash_profile  #export PS1="xlongwei# "
//ll -i,列出inode,处理难以输入文件名的情形
find . -maxdepth 1 -inum 12345 -exec rm {} \;
  • case:
usage(){
    echo "Usage: start.sh ( commands ... )"
    echo "commands: "
}
status(){}
start(){}
stop(){}
if [ $# -eq 0 ]; then 
    usage
else
	case $1 in
	status) status ;;
	start) start ;;
	stop) stop ;;
	restart) stop && start ;;
	*) usage ;;
	esac
fi
  • array
namespaceIps=(
    dev=24
    dat=29
)
for ((i=0; i<${#namespaceIps[@]}; ++i)); do
namespaceIp=${namespaceIps[i]}
namespace=`echo ${namespaceIp}|cut -d '=' -f 1`
ip=10.7.128.`echo ${namespaceIp}|cut -d '=' -f 2`
echo "deploy ${service} ${namespace} ${ip}"
done
//分割字符串为数组
string="mi,shi,yu,feng,zhong"
array=($(echo ${string} | sed "s/,/\n/g"))
for item in ${array[*]}; do
echo ${item}
done
  • string
for file in `ls /home/tomcat/code/cmp_*.jar`; do
echo $str | cut -c1-8  #截取1-8位,expr substr "$a" 1 8,${str:from:to}
${#str} #长度
  • map映射
declare -A namespaces # 初始化=(["my01"]="01" ["my02"]="02")
if [ -z "${namespaces[${namespace}]}" ]; then
namespaces["${namespace}"]="true" #所有key=${!myMap[@]},所有value=${myMap[@]},长度=${#myMap[@]}
fi
for key in ${!myMap[@]};do
echo ${myMap[$key]}  # value
done
  • tar:分卷压缩,
//分卷文件名:files.tgz.0,-a 1默认两位数字
tar zcvf - dir | split -b 2m -d -a 1 - files.tgz.
解压:-j=bz2 -J=xz(xz -d),unzip -O GB2312 file.zip文件名乱码问题
tar.gz,tar zcvf file.tgz dir,tar zxvf file.tgz,gzip -d file.gz dir,gunzip file.gz
tar.bz,tar jcvf file.tar.bz2 dir,tar jxvf file.tar.bz2
cat filestgz.* | tar zxv
shasum file,校验和,sha*支持多种算法
  • ip
ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:" # 多网卡时可能返回多个ip
ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}'
https://stedolan.github.io/jq/download/
https://github.com/stedolan/jq/releases/download/jq-1.6/jq-1.6.tar.gz,或git clone
https://github.com/stedolan/jq/releases/download/jq-1.3/jq-linux-x86_64,这是可执行文件
linux/shell.txt · 最后更改: 2021/12/24 09:35 由 admin