Shell脚本传递星号

时间:2015-12-02 07:32:20

标签: shell

我继承了一个用变量执行命令的shell脚本。 我已经添加了更多细节,抱歉我之前的细节还不清楚,我对Shell脚本不是很熟悉

#!/bin/sh
ExecuteCommand () {
    Command=`echo -e ${2}`
    Comment=${4}
    echo "INFO: ${Comment}"
    echo "# ${Comment}" >> ${CommandsLog}
    echo "${Command}" | paste -s -d' ' | tee -a ${CommandsLog}
    echo "" | tee -a ${CommandsLog}
    eval ${Command} | grep -v "ERROR : bad number of fields"
    ExitCode=$?
    if [ "${ExitCode}" != "0" ] && [ "${ExitCode}" != "" ]
    then
        exit ${ExitCode}
    fi
}

set Option = echo ` '.*(def).*'`
echo $Option
ExecuteCommand -command "abc ${Option}"

查看日志,正在执行的命令没有&{39; *&#39 ;; 即

.*(def).*
abc '. (def). '

由于命令缺少' ',命令失败 我试图在' $ Option'中传递变量。但那就失败了。 如何通过' '对命令。

1 个答案:

答案 0 :(得分:0)

看起来原作者对shell脚本也不是很熟悉。

我不确定你到底想要做什么,但这里是我对我认为应该在这里发生的事情的解释(?):

#!/bin/sh 

LOGFILE=~/mylog
PS4="Command: ->  " ## You can set this you don't like the default "+".

dowithlog(){
  (set -x; $1) 2>>"$LOGFILE"
  echo "Comment: $2" >> "$LOGFILE"
}

dowithlog "ls /tmp" "Ok, cool beans."