Search code examples
sshsudoscp

How to tell the difference between scp and ssh in .bash_profile


I have the following line embedded in .bash_profile for a remote server:

export JMXPASS=`sudo /bin/cat /opt/jmxr.pass`

When I ssh to the remote server, I get prompted for my password (expected behavior). However, when I scp from or to this remote server, I get the following error

sudo: no tty present and no askpass program specified

I want to insert an if condition inside .bash_profile that has the effect of:

if [ssh'ing] then 
    export JMXPASS=`sudo /bin/cat /opt/jmxr.pass`
fi;

Can someone tell me what needs to be the condition check?


Solution

  • The common condition used in the beginning of ~/.bashrc is

    # If not running interactively, don't do anything
    case $- in
        *i*) ;;
          *) return;;
    esac
    

    It will return if the bash is not interactive (scp case) and run further in the other cases.