目录
文件操作
sed 替换内容
# 替换ip
sed -i -e "s/gateway1 value=\"192.168.8.1/gateway1 value=\"${GATEWAY}/g" -e "s/ip1 value=\"192.168.8.124/ip1 value=\"${IP}/g" /oem/app/config/network.xml
修改配置文件redis.conf,将端口6379改为7001,将rdb文件保存位置"dir ."修改为dir /tmp/7001/
sed -i -e 's/6379/7001/g' -e 's/dir .\//dir \/tmp\/7001\//g' 7001/redis.conf
判断文件夹是否存在
if [[ ! -d "${BUILD_DIR}" ]]; then
mkdir -p ${BUILD_DIR}
fi
判断文件是否存在
# 判断更新VI内核文件
if [ -f "/oem/app/algo/zboot.img" ]
then
mkdir /tmp/update
mv /oem/app/algo/zboot.img /tmp/update/
flash_erase /dev/mtd2 0 0
nandwrite -p /dev/mtd2 /tmp/update/zboot.img
rm -rf /tmp/update/zboot.img
fi
# 不存在
if [ ! -f "/oem/app/algo/zboot.img" ]
# 也可以这样写
if [ -f "/oem/app/gettime.sh" ];then
cd /oem/app
chmod +x gettime.sh
./gettime.sh &
echo "${time} exec gettime" >> "${logFile}"
fi
判断文件软连接是否存在
if [ ! -L /oem ]
then
ln -s /userdata/oem /oem
fi
读取文件内容
content=`cat ./$APP`
echo "content: $content"
读取文件大小
filesize=`ls -l $logFile | awk '{ print $5 }'`
写内容到文件
echo "${time} re exec $2 fail" >> "${logFile}"
删除文件
# 删除/tmp/warn/前3分钟的文件,属于垃圾文件
find /tmp/warn/ -type f -mmin +3 -name "*" -exec rm -rf {} \;
数字
比较大小
if [ $year -gt 2000 ]
then
date -s $year.$mon.$day-$hrs:$min:$sec
echo "date -s $year.$mon.$day-$hrs:$min:$sec" >> "${logFile}"
hwclock -w
fi
字符串
判断是否为空
if [ -z "$content" ]
then
echo "\$content is empty"
else
echo "\$content is not empty"
fi
从字符串中提取数据(解析json)
{“code”:0,“year”:“2021”,“second”:“12”}
code=`echo $content | sed 's/,/\n/g' | grep "code" | sed 's/:/\n/g' | sed '1d' | sed 's/"//g'`
echo "code: $code"
if [ "$code" != "0" ];then
echo "错误"
exit 1
fi
year=`echo $content | sed 's/,/\n/g' | grep "year" | sed 's/:/\n/g' | sed '1d' | sed 's/"//g'`
echo "year: $year"
second=`echo $content | sed 's/,/\n/g' | grep "second" | sed 's/:/\n/g' | sed '1d' | sed 's/"//g' | sed 's/}//g'`
echo "second: $second"
日期时间
获取当前时间
time=$(date "+%Y-%m-%d %H:%M:%S")
logFile='/oem/data/log/start.log'
echo "${time}###" >> "${logFile}"
访问接口
wget
请求一个接口并把结果保存到本地
wget -q http://kf.zhuzhu.biz:7102/time
cat time
运算符 &&
if [ -f "/oem/app/algo/algo_config.xml" ] && [ ! -f "/oem/app/config/algo_config.xml" ]
then
mv /oem/app/algo/algo_config.xml /oem/app/config
fi
运行执行程序
后台运行并传参数
nohup ./wdt_live.sh Rv1126Live ./rv1126_live &
获取进程id
休眠
sleep 5
加载环境变量
. /oem/app/profile.env
引入自定义lib库
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/oem/app/lib
函数
定义函数
function limit_size_log(){
if [ ! -f "$logFile" ];
then
sudo touch $logFile
fi
filesize=`ls -l $logFile | awk '{ print $5 }'`
if [ $filesize -gt $maxSize ]
then
echo "$filesize > $maxSize"
cd /mnt/data/log
sudo cp -rf $logFile monitorLast.log
sudo rm -rf $logFile
sudo cp /dev/null $logFile
fi
}
调用
# 调用
limit_size_log
用户
检查本机当前用户是否超级管理员,如果是管理员则yum安装,
#!/bin/bash
if [ $USER == "root" ];then
yum -y install vsftpd
else
echo "您不是管理员,没有权限安装"
fi