使用参数作为函数内部变量的一部分

时间:2018-07-09 03:43:22

标签: function variables

如果我输入:

 function chk_is_it_started(){
   PROCC_NAME_$1="my_process_$1";
   echo "PROCC_NAME_$1 is: $PROCC_NAME_$1";
   PID_FILE_OF_APP_$1="/run/pidfile_$PROCC_NAME_$1.pid"
   PATH_OF_PROCCESS_NAME_$1=`ps -aux|grep $PROCC_NAME_$1|grep -v grep|awk -F" " '{print $12}'`
   PID_NUMBER_OF_APP_$1=`ps -aux|grep $PROCC_NAME_$1|grep -v grep|awk -F" " '{print $2}'`
   NUMBER_OF_OCCURENCE_$1=`echo ${#PID_NUMBER_OF_APP_$1[@]}`
   if [[ "$NUMBER_OF_OCCURENCE_$1" == 0 ]];then
     echo -e "Proccess isn't started..\nNow process $PATH_OF_PROCCESS_NAME_$1 is running and I'm creating a PID file..."
     python /emu/script/$PROCC_NAME_$1.py  & disown & echo $! > $PID_FILE_OF_APP_$1
   else
     echo "Proccess is STARTRED"
   fi
  }
  chk_is_it_started blabla;

我会得到错误:

root@orangepipc:~# chk_is_it_started blabla;
Could not find the database of available applications, run update-command-not-found as root to fix this
PROCC_NAME_blabla=my_process_blabla: command not found
PROCC_NAME_blabla is: blabla
-bash: PID_FILE_OF_APP_blabla=/run/pidfile_blabla.pid: No such file or directory
Could not find the database of available applications, run update-command-not-found as root to fix this
PATH_OF_PROCCESS_NAME_blabla=: command not found
Could not find the database of available applications, run update-command-not-found as root to fix this
PID_NUMBER_OF_APP_blabla=: command not found
-bash: ${#PID_NUMBER_OF_APP_$1[@]}: bad substitution
Could not find the database of available applications, run update-command-not-found as root to fix this
NUMBER_OF_OCCURENCE_blabla=: command not found
Proccess is STARTRED

但事实并非如此! 我在哪里犯错? 如果我使用的是不带功能的代码,则可以正常工作! 谢谢

1 个答案:

答案 0 :(得分:0)

我找到了解决方法...

function chk_is_it_started(){
    PROCC_NAME="dht22_$1"
#   echo "PROCC_NAME_$1 is: $PROCC_NAME"
    PID_FILE_OF_APP="/run/pidfile_$PROCC_NAME.pid"
#   echo "PID_FILE_OF_APP is: $PID_FILE_OF_APP"
    PATH_OF_PROCCESS_NAME=`ps -aux|grep $PROCC_NAME_$1|grep -v grep|awk -F" " '{print $12}'`
#   echo "PATH_OF_PROCCESS_NAME is: $PATH_OF_PROCCESS_NAME"
    PID_NUMBER_OF_APP=`ps -aux|grep $PROCC_NAME_$1|grep -v grep|awk -F" " '{print $2}'`
#   echo "PID_NUMBER_OF_APP is $PID_NUMBER_OF_APP"
    PID_NUMBER_OF_APP=( $PID_NUMBER_OF_APP )
#   echo "PID_NUMBER_OF_APP is $PID_NUMBER_OF_APP"
    NUMBER_OF_OCCURENCE=`echo ${#PID_NUMBER_OF_APP[@]}`
#   echo "NUMBER_OF_OCCURENCE is: $NUMBER_OF_OCCURENCE"
    if [[ "$NUMBER_OF_OCCURENCE" == 0 ]];then
        echo -e "Proccess isn't started..\nNow process $PATH_OF_PROCCESS_NAME is running and I create a PID file..."
        python /emu/script/$PROCC_NAME.py  & disown & echo $! > $PID_FILE_OF_APP
    #   exit
    else
        echo "Proccess is STARTRED"
    fi
    if [[ "$NUMBER_OF_OCCURENCE" > 1 ]];then
        echo -e "Process $PROCC_NAME.py is started more than 1x"
        echo -e "Now killing all proccess one by one"
        while [ "$NUMBER_OF_OCCURENCE" != "1" ];
        do
            echo "Usao sam u while"
            PID_NUMBER_OF_APP=`ps -aux|grep $PROCC_NAME|grep -v grep|awk -F" " '{print $2}'`
            echo "PID_NUMBER_OF_APP is: $PID_NUMBER_OF_APP"
            PID_NUMBER_OF_APP=( $PID_NUMBER_OF_APP )
            echo "PID_NUMBER_OF_APP is $PID_NUMBER_OF_APP"
            NUMBER_OF_OCCURENCE=`echo ${#PID_NUMBER_OF_APP[@]}`
            echo "NUMBER_OF_OCCURENCE is: $NUMBER_OF_OCCURENCE"
            kill $PID_NUMBER_OF_APP
            rm -fr $PID_FILE_OF_APP

        done
        echo -e "Starting process $PROCC_NAME.py and creating a PID file..."
        python /emu/script/$PROCC_NAME.py & echo $! > $PID_FILE_OF_APP
    fi
}

chk_is_it_started bla1
chk_is_it_started bla2

向我的问题投了-1票的敬礼用户:)