# 截取子数组 declare -a arr=(a b c d e f g) # c d e 从index2开始截取3个 echo${arr[@]:2:3} a=(${arr[@]:1:3})
# 从index4开始的全部 ${arr[@]:4}
# 连接两个数组 abc=(a b c) distros=("${distros[@]}""${abc[@]}")
# 打印数组列表 竖列 arrList=(1 2 3 4) for ele in${arrList[@]} do echo"$ele" done # 一行表达 for element in"${myArr[@]}"; doecho$elementdone printlnArr (){ # printlnArr ${arr[*]} for ele in$1 do echo"$ele" done }
# 去除重复 # 去除重复 dupArr=(1 2 3 4 5 5 6 7 8 9 4 3 2 4 5 6 8) # 去除dupArr中相同元素 dupArrlen=${#dupArr[@]} for((i=0;i<$dupArrlen;i++)) do for((j=$dupArrlen-1;j>i;j--)) do if [[ ${dupArr[i]} = ${dupArr[j]} ]];then unset dupArr[i] fi done done result=() index=0 for each in${dupArr[@]} do result[$index]=$each index=$((index+1)) done arrRemoveDup() { # arrRemoveDup ${dupArr[*]} # echo ${result[*]} arrRemoveDupArr=($*) arrRemoveDupArrlen=${#arrRemoveDupArr[@]} for ((i = 0; i < $arrRemoveDupArrlen; i++)); do for ((j = $arrRemoveDupArrlen - 1; j > i; j--)); do if [[ ${arrRemoveDupArr[i]} = ${arrRemoveDupArr[j]} ]]; then unset arrRemoveDupArr[i] fi done done result=() index=0 for each in${arrRemoveDupArr[@]}; do result[$index]=$each index=$((index + 1)) done }
# 数组排序 # 仅能排序数字(if判断中"-gt" 改为 “-lt"可实现降序) for ((a = 0; a < ${#mm[*]}; a++)); do for ((k = $a + 1; k < ${#mm[*]}; k++)); do if [ ${mm[$a]} -gt ${mm[$k]} ]; then qq=${mm[$a]} mm[$a]=${mm[$k]} mm[$k]=$qq fi done done sortNumArr1() { # sortNumArr ${toSort[*]} # bb=${mm[*]} mm=($*) for ((a = 0; a < ${#mm[*]}; a++)); do for ((k = $a + 1; k < ${#mm[*]}; k++)); do if [ ${mm[$a]} -gt ${mm[$k]} ]; then qq=${mm[$a]} mm[$a]=${mm[$k]} mm[$k]=$qq fi done done }
# 仅能排序数字 sortNumArr2() { # 定义函数 exchangeEle() 交换数组中两个元素的位置 exchangeEle() { # 使用临时变量保存数组元素 local temp=${exchangeEleArr[$1]} # 交换元素的位置 exchangeEleArr[$1]=${exchangeEleArr[$2]} exchangeEleArr[$2]=$temp return } # sortArrByStr ${aa[*]} # echo ${rr[*]} exchangeEleArr=($*) # 通过 for 循环对数组排序,注意此时是以字符串来比较的 for ((last = ${#exchangeEleArr[@]}; last > 1; last--)); do for ((i = 0; i < last - 1; i++)); do [[ "${exchangeEleArr[$i]}" -gt "${exchangeEleArr[$((i+1))]}" ]] && exchangeEle $i $((i+1)) done done rr=${exchangeEleArr[*]} }
# 根据字符串排序 sortArrByStr() { # 定义函数 exchangeEle() 交换数组中两个元素的位置 exchangeEle() { # 使用临时变量保存数组元素 local temp=${exchangeEleArr[$1]} # 交换元素的位置 exchangeEleArr[$1]=${exchangeEleArr[$2]} exchangeEleArr[$2]=$temp return } # sortArrByStr ${aa[*]} # echo ${rr[*]} exchangeEleArr=($*) # 通过 for 循环对数组排序,注意此时是以字符串来比较的 for ((last = ${#exchangeEleArr[@]}; last > 1; last--)); do for ((i = 0; i < last - 1; i++)); do [[ "${exchangeEleArr[$i]}" > "${exchangeEleArr[$((i + 1))]}" ]] && exchangeEle $i $((i + 1)) done done rr=${exchangeEleArr[*]} }
# 判断文件存在 fileToCheck="" if [ -f "$fileToCheck" ];then echo"File "$fileToCheck" exist." else echo"File "$fileToCheck" not found." fi
# 判断文件夹存在 注意:"~/.config"的表达无效 pathToCheck="" if [ -d "$pathToCheck" ];then echo"文件夹存在" else echo"文件夹不存在" fi
# 否定直接加! if [ ! -f "$pathToCheck" ]; then touch "$pathToCheck" fi
判断是否安装某命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 方法1 cmdToCheck="git" if ! [ -x "$(command -v "$cmdToCheck")" ]; then echo"Error: "$cmdToCheck" is not installed." >&2 else echo"Command found! : "$cmdToCheck"" >&1 fi
# 方法2 cmdToCheck="node" if ! type"$cmdToCheck" >/dev/null 2>&1; then echo"Error: "$cmdToCheck" is not installed."; else echo"Command found! : "$cmdToCheck"" >&1 fi
# 正则表达式判断 注意:输入不准为空! toCheck="" ifecho"$toCheck" | grep -q "^[0-9]*$"; then echo"String "$toCheck" is a number" else echo"String "$toCheck" is not a number" fi
# 使用echo命令进行求值 不可判断空输入! toCheck="" if [ "$(echo "$toCheck" | tr -d '[:digit:]')" = "" ]; then echo"String "$toCheck" is a number" else echo"String "$toCheck" is not a number" fi
# 使用sed (使用sed命令将字符串中的非数字字符替换为空,然后比较替换前后的字符串长度是否一致,如果一致,则原始字符串为纯数字) # 不可判断空输入! toCheck="" if [ "$(echo "$toCheck" | sed 's/[0-9]//g')" = "" ]; then echo"String "$toCheck" is a number" else echo"String "$toCheck" is not a number" fi
# 使用awk进行判断(使用awk命令对字符串进行求值,如果能够正确求值,则原始字符串为数字) # 可判断空输入 toCheck="" echo"$toCheck" | awk '{if ($0 ~ /^[0-9]+$/) printf(""$toCheck" is a number\n"); else printf(""$toCheck" is not a number\n")}'
# 使用expr进行判断(使用expr命令对字符串进行求值,如果能够求值则原始字符串为数字,否则会得到一个错误提示) # 可判断空输入 toCheck="" if [ $(expr "$toCheck" + 0 2>/dev/null) ]; then echo"String "$toCheck" is a number" else echo"String "$toCheck" is not a number" fi
# 使用BASH中的正式表达式 # 可判断空输入 toCheck="" bash_re='^[0-9]+$' if [[ $toCheck =~ $bash_re ]]; then echo"String "$toCheck" is a number" else echo"String "$toCheck" is not a number" fi
# 方案1: 用echo命令打印带有色彩的文字: 文字色: echo -e "\e[1;31mThis is red text\e[0m" This is red text?\e[1;31m 将颜色设置为红色 ?\e[0m 将颜色重新置回 颜色码:重置=0,黑色=30,红色=31,绿色=32,黄色=33,蓝色=34,洋红=35,青色=36,白色=37 echo -e "\e[1;42mGreed Background\e[0m" Greed Background颜色码:重置=0,黑色=40,红色=41,绿色=42,黄色=43,蓝色=44,洋红=45,青色=46,白色=47 文字闪动:echo -e "\033[37;31;5mMySQL Server Stop...\033[39;49;0m" 红色数字处还有其他数字参数:0 关闭所有属性、1 设置高亮度(加粗)、4 下划线、5 闪烁、7 反显、8 消隐 echo -e "\e[1;32m \e[0m"
# 方案2:推荐 # 颜色colors CDEF=" \033[0m"# default color CCIN=" \033[0;36m"# info color CGSC=" \033[0;32m"# success color CRER=" \033[0;31m"# error color CWAR=" \033[0;33m"# warning color b_CDEF=" \033[1;37m"# bold default color b_CCIN=" \033[1;36m"# bold info color b_CGSC=" \033[1;32m"# bold success color b_CRER=" \033[1;31m"# bold error color b_CWAR=" \033[1;33m"
# 获取屏幕分辨率1600 x 900 # Screen 0: minimum 8 x 8, current 1600 x 900, maximum 32767 x 32767 xrandr | grep -o "current [0-9]* x [0-9]*" | sed -e 's/current *//g' # 分别获取 SCREEN=$(xrandr | grep -o "current [0-9]* x [0-9]*" | sed -e 's/current *//g') SCREEN_W=$(echo $SCREEN | sed -e 's/ x [0-9]*//') SCREEN_H=$(echo $SCREEN | sed -e 's/[0-9]* x //')
在屏幕上关闭一个窗口的时候,有时会想起一句古老的诗,人生天地间,忽如远行客。我要写的截屏工具,也许能留住任一窗口璀璨的瞬间,只要我知道它在哪里。一个窗口在哪里,是由它的左上角坐标以及它的宽度和高度决定的。在我还没发现 X 窗口系统的 xwininfo 这个工具之前,我只能考虑使用诗歌来实现这样的截屏工具……xwininfo 说,我知道你想要知道的,只要你用鼠标点一下你想点的…… $ xwininfo xwininfo: Please select the window about which you would like information by clicking the mouse in that window. declare -a WIN_PARAMS WIN_PARAMS=($(xwininfo | sed -n -e '/^[[:space:]]*Absolute ..*[XY]/p; /^[[:space:]]*Relative ..*[XY]/p; /^[[:space:]]*Width:/p; /^[[:space:]]*Height:/p' | awk 'BEGIN{FS=":"}{print $2}'))
#!/bin/bash # http://t.zoukankan.com/yuandaozhe-p-14519296.html # 直接循环处理文件 functiondoFiles(){ for file in `ls $1` #注意此处这是两个反引号,表示运行系统命令 do if [ -d $1"/"$file ] #注意此处之间一定要加上空格,否则会报错 then echo"Msg: Directory "$1"/'$file' Passed." else if [[ "$file" == *"$2" ]]; then echo"Change this line to do sth. --> $file" fi fi done }
# 直接循环处理文件(递归) functiondoFilesRecur(){ for file in `ls $1` do if [ -d $1"/"$file ] then doFilesRecur $1"/"$file$2 else if [[ "$file" == *"$2" ]]; then echo"Change this line to do sth. --> $file" fi fi done }
# 返回gf_list文件列表 functiongetFiles(){ for file in `ls $1` do if [ -d $1"/"$file ] then echo"Msg: Directory "$1"/'$file' Passed." else if [[ "$file" == *"$2" ]]; then # 未验证 file="$1"/"$file" file_list=$file_list♨$file fi fi done OLD_IFS="$IFS" IFS="♨" gf_list=($file_list) IFS="$OLD_IFS" # 未验证 gfr_list=(${gfr_list[@]}) }
# 返回gfr_list文件列表(递归) functiongetFilesRecur(){ for file in `ls $1` do if [ -d $1"/"$file ] then getFilesRecur $1"/"$file$2 else if [[ "$file" == *"$2" ]]; then file="$1"/"$file" file_list=$file_list♨$file fi fi done OLD_IFS="$IFS" IFS="♨" gfr_list=($file_list) IFS="$OLD_IFS" gfr_list=(${gfr_list[@]}) }
# 用法:目录+扩展名 doFilesRecur ".""mp4" getFiles ".""mp4" # 打印列表 for each in${gf_list[@]} do echo"$each" done
时间日期
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
参考:https://www.cnblogs.com/howhy/p/5999762.html 当前时间:date "+%Y%m%d%H%M%S" 20220212202614 比如今日是2012-04-22 $ date -d "+1 day" +%Y-%m-%d 2012-04-23 $ date -d "2012-04-10 +1 day " +%Y-%m-%d 2012-04-11 $ date -d "-1 year " +%Y-%m-%d 2011-04-22
# 2021-02-19 23:32:18 date +"%Y-%m-%d %T" # 2021-02-19-23-34-47 date +"%Y-%m-%d %T" | sed -e "s/ /-/g; s/:/-/g"
#!/bin/bash NAME=$(date +"%Y-%m-%d %T" | sed -e "s/ /-/g; s/:/-/g") IMAGE=/tmp/${NAME}.png # echo $IMAGE # 获取分辨率 SCREEN=$(xrandr | grep -o "current [0-9]* x [0-9]*" | sed -e 's/current *//g') SCREEN_W=$(echo$SCREEN | sed -e 's/ x [0-9]*//') SCREEN_H=$(echo$SCREEN | sed -e 's/[0-9]* x //') # 获取窗口几何 declare -a WIN_PARAMS WIN_PARAMS=($(xwininfo | sed -n -e '/^[[:space:]]*Absolute ..*[XY]/p; /^[[:space:]]*Relative ..*[XY]/p; /^[[:space:]]*Width:/p; /^[[:space:]]*Height:/p' | awk 'BEGIN{FS=":"}{print $2}')) # 构造理想中的截图区。 # 之所以如此,与 xwininfo 输出的窗口左上角相对坐标有关。 # MARGIN=${WIN_PARAMS[2]} MARGIN=12 WIN_X=$((${WIN_PARAMS[0]} - ${WIN_PARAMS[2]})) WIN_Y=$((${WIN_PARAMS[1]} - ${WIN_PARAMS[3]})) WIN_W=$((${WIN_PARAMS[4]} + ${WIN_PARAMS[2]} + $MARGIN)) WIN_H=$((${WIN_PARAMS[5]} + ${WIN_PARAMS[3]} + $MARGIN))
# 截图区越界处理 if (($WIN_X < 0)); then WIN_X=0; fi if (($WIN_Y < 0)); then WIN_Y=0; fi if (($WIN_W + $WIN_X > $SCREEN_W)); then WIN_W=$(($SCREEN_W - $WIN_X)); fi if (($WIN_H + $WIN_Y > $SCREEN_H)); then WIN_H=$(($SCREEN_H - $WIN_Y)); fi ffmpeg -video_size ${WIN_W}x${WIN_H} \ -f x11grab -ss 00:00:00 \ -i :0.0+${WIN_X},${WIN_Y} \ -frames:v 1 $IMAGE 2>/dev/null gimp $IMAGE &
# 获取屏幕分辨率 SCREEN=$(xrandr | grep -o "current [0-9]* x [0-9]*" | sed -e 's/current *//g') SCREEN_W=$(echo$SCREEN | sed -e 's/ x [0-9]*//') SCREEN_H=$(echo$SCREEN | sed -e 's/[0-9]* x //')
# 获取窗口几何参数 declare -a WIN_PARAMS WIN_PARAMS=($(xwininfo | sed -n -e '/^[[:space:]]*Absolute ..*[XY]/p; /^[[:space:]]*Relative ..*[XY]/p; /^[[:space:]]*Width:/p; /^[[:space:]]*Height:/p' | awk 'BEGIN{FS=":"}{print $2}'))
# 截图区越界处理 if (($WIN_X < 0)); then WIN_X=0; fi if (($WIN_Y < 0)); then WIN_Y=0; fi if (($WIN_W + $WIN_X > $SCREEN_W)); then WIN_W=$(($SCREEN_W - $WIN_X)); fi if (($WIN_H + $WIN_Y > $SCREEN_H)); then WIN_H=$(($SCREEN_H - $WIN_Y)); fi
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !