dcds
dcds

Reputation: 447

undefined variable error in csh script

I have one function in csh script and in this function I am using one variable which is sourced from one file. But while using script its throwing undefined error for same variable. I am using Linux.

My Code

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
}

below is the error:

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

Please anyone suggest what I am missing?

Upvotes: 0

Views: 4033

Answers (2)

Matheus Garcia
Matheus Garcia

Reputation: 41

The C Shell lacks a function feature. Aliases may serve as workaround, but are somewhat painful to work with. A better workaround is to use goto and source:

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"

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249133

The C Shell csh does not have functions. It does have aliases, but those are harder to write and read. For exmaple, see here: https://unix.stackexchange.com/questions/62032/error-converting-a-bash-function-to-a-csh-alias

It might be a good idea to simply switch to Bash, where your existing code may already be working.

Upvotes: 1

Related Questions