执行脚本(假设命令都在变量 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
文章评论