linux csh脚本中未定义变量错误

kadbb459  于 2023-06-21  发布在  Linux
关注(0)|答案(2)|浏览(140)

我在csh脚本中有一个函数,在这个函数中,我使用了一个来自一个文件的变量。但是当使用脚本时,它会为同一个变量抛出未定义的错误。我使用Linux。
我的代码

function init_remote_commands_to_use
{
    # Test if the environment variable SSH_FOR_RCOMMANDS is present in .temip_config file,
    # use secured on non secured rcommand depending on the result

    if [  "$SSH_FOR_RCOMMANDS" != "" ]
    then
        if [ "$SSH_FOR_RCOMMANDS" = "ON" ]
        then
            # Check if the environment variable SSH_PATH is specified in .temip_config file
            if  [ "$SSH_PATH" != "" ]
            then
                SH_RCMD=$SSH_PATH
            else
                SH_RCMD=$SSH_CMD
            fi
            # Check if a ssh-agent is already running
            if [ "$SSH_AGENT_PID" = "" ]
            then
                #Run ssh-agent for secured RCommands
                eval `ssh-agent`
                ssh-add
                STARTEDBYME=YES
            fi

        else
            if [ "$SSH_FOR_RCOMMANDS" = "OFF" ]
            then
                SH_RCMD=$RSH_CMD
            else
                echo "Please set the SSH_FOR_RCOMMANDS value to ON or OFF in the .temip_config file"
                exit 1
            fi
        fi
    else
        SH_RCMD=$RSH_CMD

    fi
}

下面是错误:

function: Command not found.
{: Command not found.
SSH_FOR_RCOMMANDS: Undefined variable.

请问有没有人告诉我我错过了什么?

ds97pgxw

ds97pgxw1#

C Shell csh没有函数。它确实有别名,但那些更难写和读。例如,请参见此处:https://unix.stackexchange.com/questions/62032/error-converting-a-bash-function-to-a-csh-alias
简单地切换到Bash可能是一个好主意,因为您现有的代码可能已经在工作了。

2sbarzqh

2sbarzqh2#

C Shell缺少一个函数特性。别名可以作为解决方法,但使用起来有些麻烦。更好的解决方法是使用gotosource

alias function 'set argv = ( _FUNC \!* ) ; source $0'

if ( "$1" == "_FUNC" ) goto "$2"

set str = "`function myfunc`"
set ret = "$status"
echo "$str"
if ( "$ret" < 0 ) exit -1
exit

myfunc:
set ret = 0
if ( "$SSH_FOR_RCOMMANDS" != "" ) then
  if ( "$SSH_FOR_RCOMMANDS" == "ON" ) then
    # Check if the environment variable SSH_PATH is specified in .temip_config file.
    if ( "$SSH_PATH" != "" ) then
      echo "$SSH_PATH"
    else
      echo "$SSH_CMD"
    endif
    # Check if a ssh-agent is already running.
    if ( "$SSH_AGENT_PID" == "" ) then
      # Run ssh-agent for secured RCommands.
      eval "`ssh-agent`"
      ssh-add
      echo YES
    endif
  else
    if ( "$SSH_FOR_RCOMMANDS" == "OFF" ) then
      echo "$RSH_CMD"
    else
      echo "Please, set the SSH_FOR_RCOMMANDS value to ON or OFF in the .temip_config file."
      set ret = 1
    endif
  endif
else
  echo "$RSH_CMD"
endif
exit "$ret"

相关问题