贴一段shell代码

时间:2022-04-17 15:54:35

好久没用shell了,呵呵,这个是当counter < 1000的时候,往tmp.sql里填这两个字符串,生成假数据。

#!/usr/bin/env bash

COUNTER=

while [  $COUNTER -lt  ];
do echo The counter is $COUNTER
cat<<EOF >>tmp.sql
INSERT INTO "wcdata" VALUES('`(printf "%03d" $COUNTER)`','0.00','0.00','0.00183','85.47943','4773267.7216','-10669.18');
EOF echo The counter is $COUNTER
cat<<EOF >>tmp.sql
INSERT INTO "wcxx" VALUES('`(printf "%03d" $COUNTER)`','同步输出','模拟小电压,0.5V','模拟小电压,0.5V','','2015-01-23-19-52-36');
EOF let COUNTER=COUNTER+ done

另一个,在rcS中调用这个脚本,每次开机去检查/home/plg,删除image目录,并删除/pcap中旧的文件(只保留最新的6个)。

#!/bin/sh
counter=
app_path=/home/plg if [ -e $app_path/pcap ] ;then file_list=`ls -ct $app_path/pcap/*.pcap` for file in $file_list; do counter=`expr $counter + 1` if [ $counter -gt 6 ] ;then
echo "Remove old file, rm file $file"
rm -rf $file
fi
done
fi if [ -e $app_path/image ] ;then
echo "Remove the image dir!"
rm -rf $app_path/image
fi

上面的代码当“$app_path/pcap”中的文件名有空格,程序会出错,所以,对它进行了改进

if [ -e  $app_path/pcap ] ;then

    ls -ct $app_path/pcap/*.pcap | while read file; do
counter=`expr $counter + 1`
if [ $counter -gt 6 ] ;then
echo "Remove old file, rm file $file"
rm -rf "$file"
fi
done fi