零号智能

  • Python
  • 部署
  • go
  • VPS
  • 运维
  • 生活琐事
  • html
零号智能
Fantasia's blog npc0.com.
  1. 首页
  2. 运维
  3. 正文

shell 执行回调脚本 与 exec执行脚本获取不到输出问题

2021年9月15日 3390点热度 0人点赞 0条评论

执行脚本(假设命令都在变量 xxx 中, xxx 为 ping baidu.com -c 3):有

eval $xxx

exec $xxx

${xxx}

而获取输出有:

1:输出到变量 log=${xxx}

2:输出到文件 eval $xxx >/tmp/log.log 2>&1

#!/bin/bash

# init
touch_log="/tmp/`cat /proc/sys/kernel/random/uuid`.log"

# $1 exec type 1:ok, 2:error
exec_out() {
   curl_get="curl $RUN_HOST/base/exec -F \"log=@$touch_log\" -F \"state=$1\""
   for i in ${RUN_JSON//&/ }; do curl_get+=" -F \"${i//\"/\\\"}\""; done
   echo $curl_get > /tmp/curl.log
   eval $curl_get
   rm -Rf $touch_log
}

help() {
   echo "Usage:"
   echo "exec.sh [options] <mainclass> [args]"
   echo ""
   echo "where options include"
   echo "    -d:<exec file path> "
   echo "        Execute directory. If it is not filled in. the default is /"
   echo "    -c:<exec code>"
   echo "        Exec command executed"
   echo "    -j:<exec json>"
   echo "        Command return json"
   echo "    -h:<get url host>"
   echo "        get host"
   exit -1
}

while getopts 'd:c:t:j:h:' opt; do
   case $opt in
      d) RUN_DIR="$OPTARG";;
      c) RUN_CODE="$OPTARG";;
      j) RUN_JSON="$OPTARG";;
      h) RUN_HOST="$OPTARG";;
      ?) help;;
   esac
done


if [ "$RUN_CODE" != "" ]; then
    source /etc/profile
    eval $RUN_CODE > $touch_log 2>&1
    if [ $? -eq 0 ]; then
        exec_out 1
    else
        exec_out 2
    fi
else
    echo "code nil" > touch_log
    exec_out 2
fi

# delete run file
if [[ "$0" != "/" || ${#0}>2 ]]; then
    rm -Rf $0
fi
本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: shell
最后更新:2021年9月17日

vpc

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2021 npc0.com. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

粤ICP备19157245号-1

粤公网安备 44030702002746号