Ok right I didn't read in details the function you wrote, here is a working one ;
global proc myscript() {
string $sel[] = "ls -sl";
if ($sel[0] == "pCube1") {
setAttr("pCube2.translateX", 15);
} else if ($sel[0] == "pCube3") {
setAttr("pCube2.translateX", 25);
}
}
scriptJob -e "SelectionChanged" "myscript";
Well... There is an issue due to the text editor of the website so you can find the working code here , this text editor just doesn't allow to put a diacritical grave accent which is problematic for MEL code !
And yes this is the common way to declare what is named a callback - which means something triggered everytime the event occurs, in our case a selection change - to write a line like the one at the end of the expression !
By the way you can have a look on this tutorial if you want an introduction to scripting (in Python) in Maya : https://3dbunk.com/introduction_to_python_script
Python is a (little) bit slower than MEL but you can do more advanced tools, scripts and more easy to read
Hope it helps,
Peace,
Mehdi