0

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?

2
  • Add your code along with this error Commented Jan 7, 2015 at 6:13
  • This is not a csh script...? This looks like bash (not sh) ... Perhaps try executing it with bash? Commented Jan 7, 2015 at 6:48

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

can you pls tell me how to use above code in aliases in csh?
You would have to just string it all together as one giant string. It will be disgusting. You could instead put it in a file by itself and invoke it as a program.
It's not just the functions; the above script doesn't even remotely resemble a csh script: wrong syntax for if (correct: if ( .. ) then endif) and you assign variables with set (correct: set var = value, or setenv var = value for environment variables) ...
@Carpetsmoker: correct. If you move the whole thing to a file, it would be a bash script, not a csh one. It would still be invokable from csh.
how to source a file from csh script?
|
0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.