shell如何将文本文件字符串换行后转换为数组

1 #!/bin/sh
  2 #首先判断有没有启动失败的文件,有则处理
  3 if [ -f "./task_start_failed.log" ]&&[ ` ls -l task_start_failed.log| awk '{print $5}' ` -gt 0 ];  then
  4     # 将失败的任务id 保存在数组array中
  5     array=(`cat task_start_failed.log |awk -v RS='' '{gsub("\n"," "); print}'`)
  6     #删除日志文件
  7     rm -f task_start_failed.log
  8     rm -f console.log
  9     #对失败的任务,再次启动
 10     for task_id in ${array[@]}
 11     do
 12          result=`curl -u zhangchangchang:ec60d778b5d31777afc6dd20ee5b1ae4 http://pie.baidu.com/api/task -d                'task_id='$task_id'&method=start' -X PUT 2>console.log`
 13          # 根据启动的返回结果,将启动不成功的任务id记录下来
 14          if [[ $result =~ "task start failed" ]];then
 15              echo ${task_id}>>task_start_failed.log
 16          fi
 17     done

 18 fi


 cat task_ids | while read task_id
  8 do
  9     result=`curl -u zhangchangchang:ec60d778b5d31777afc6dd20ee5b1ae4 http://pie.baidu.com/api/task -d                     'task_id='$task_id'&method=start' -X PUT 2>console.log`
 10     # 根据启动的返回结果,将启动不成功的任务id记录下来
 11     if [[ $result =~ "task start failed" ]];then
 12      echo ${task_id}>>task_start_failed.log
 13    fi
 14 done

总结:(1)利用awk命令cat task_start_failed.log |awk -v RS='' '{gsub("\n"," "); print}

            RS=“” 表示利用“”来分割前面显示的结果,gsub将换行符\n替换为空格“ ”,打印出换行符转为空格的字符串,加括号后会形成一个数组,赋给array;

             (2)shell中逻辑&&的使用方法;

             (3)shell循环遍历一个数组;

             (4)shell如何判断一个字符串包含某个特征串;

               (5) shell读取文件

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值