shell script using bricks

Arithmetic operations commonly used in the shell

1. Use the expr external program

x=4
y=5
# 方法1
r=`expr $x + $y + 1` # 注:变量需要加$,运算符之间需要有空格

# 方法2
r=$((x+y+1))  # 或者
r=$(($x+$y+1)) # 注:变量前$可加可不加,运算符之间空格可有可无

# 方法3(类似方法2)
r=$[x+y+1]  # 或者
r=$[$x+$y+1] # 注:变量前$可加可不加,运算符之间空格可有可无

# 方法4
let r=x+y+1  # 或者
let "r=$x+$y+1" # 或者
let r=$x+$y+1  # 注:变量前$可加可不加,运算符之间空格可有可无,引号可有可无(除非有括号)

# 方法5
echo $x + $y + 1 | bc   # 或者
echo "$x + $y + 1" | bc   # 注:变量前$必须加,运算符之间空格可有可无,引号可有可无(除非有括号)

# 方法6
r=`echo $x $y | awk '{print $1+$2}'`

The functions implemented by the following shell script:

  • Continuously execute sql statements
  • Do not hit the screen (2>&1)
  • Generate a log regularly (to prevent the log file from being too large)
  • Each loop adds a timestamp
  • Application of text flashing effect
#!/bin/sh
db=postgres
port=6000
num=1
echo -e "\033[37;31;5mI am running,don't touch me!!!!!!!!\033[39;49;0m"
echo "clean test1" > test1
for ((i=1;i<=51840;i++));do
{
        echo $(date +%F%n%T) >>  test$num
        gsql -d $db -p $port -a -f "mysql_create_alter_table_function_scence.sql"  >> test$num 2>&1 &
        wait
        gsql -d $db -p $port -a -f "mysql_create_alter_table_abnormal_scence.sql"  >> test$num 2>&1 &
        wait
        if [ $(($i%1000)) == 0 ];then
                num=$(($num+1))
        fi
}
done

Guess you like

Origin blog.csdn.net/weixin_45541762/article/details/132607893