shell脚本使用砖块

shell中常用的算术运算

1.使用 expr 外部程式

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}'`

下面shell脚本实现的功能:

  • 连续执行sql语句
  • 不打屏(2>&1)
  • 定时生成一个日志(防止日志文件过大)
  • 每个循环添加一个时间戳
  • 文字闪烁效果的应用
#!/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

猜你喜欢

转载自blog.csdn.net/weixin_45541762/article/details/132607893