Need help with executing multiple commands in remote machine

Hi,
I work on a jumpserver and I wrote a script to transfer a file from source server to destination server.

#!/bin/ksh
echo "\nEnter the file name:\n"
read name
echo "\nSelect the Source server\n"
echo "1.  ODS PROD "
echo "2.  ODS DROPBOX"
echo "3.  ODS STE"
echo "4.  ODS STE DROPBOX"
echo "5.  ODS CTE "
echo "6.  ODS CTE DROPBOX"
echo "7.  ODS ITE"
echo "8.  ODS ITE DROPBOX"
echo "9.  ODS DEV"
echo "10. ODS DEV DROPBOX"
echo "11. ODS Legacy Prod"
echo "12. EXIT"
read optn
echo "\nSelect the path\n"
read path
case $optn in
  1) scp `whoami`@162.111.210.43:$path/$name .;;
         2) scp `whoami`@162.111.214.23:$path/$name .;;
        3) scp `whoami`@167.138.250.239:$path/$name .;;
        4) scp `whoami`@167.138.250.224:$path/$name .;;
        5) scp `whoami`@172.21.207.158:$path/$name .;;
        6) scp `whoami`@172.21.220.162:$path/$name .;;
        7) scp `whoami`@172.21.207.72:$path/$name .;;
        8) scp `whoami`@172.21.220.18:$path/$name .;;
         9) scp `whoami`@172.18.197.41:$path/$name .;;
        10) scp `whoami`@172.18.197.43:$path/$name .;;
         11) scp `whoami`@170.13.54.102:$path/$name .;;
 
 
esac
chmod 777 $name
echo "\nSelect the dest server\n"
echo "1.  ODS PROD "
echo "2.  ODS DROPBOX"
echo "3.  ODS STE"
echo "4.  ODS STE DROPBOX"
echo "5.  ODS CTE "
echo "6.  ODS CTE DROPBOX"
echo "7.  ODS ITE"
echo "8.  ODS ITE DROPBOX"
echo "9.  ODS DEV"
echo "10. ODS DEV DROPBOX"
echo "11. ODS Legacy Prod"
echo "12. EXIT"
read optn2
echo "\nSelect the destination path\n"
read destpath
case $optn2 in 
 1) scp $name `whoami`@162.111.210.43:/home/`whoami`;;
 2) scp $name `whoami`@162.111.214.23:/home/`whoami`;;
 3) scp $name `whoami`@167.138.250.239:/home/`whoami`;;
 4) scp $name `whoami`@167.138.250.224:/home/`whoami`;;
 5) scp $name `whoami`@172.21.207.158:/home/`whoami`;;
 6) scp $name `whoami`@172.21.220.162:/home/`whoami`;;
 7) scp $name `whoami`@172.21.207.72:/home/`whoami`;;
 8) scp $name `whoami`@172.21.220.18:/home/`whoami`;;
 9) scp $name `whoami`@172.18.197.41:/home/`whoami`;;
 10) scp $name `whoami`@172.18.197.43:/home/`whoami`;;
        11) scp $name `whoami`@170.13.54.102:/home/`whoami`;;
        12) exit;;
esac
 
case $optn2 in 
 #9) ssh -l x772525 172.18.197.41 'su - d_od; cd /home/x772525;chmod 777 $name;cp -p $name $destpath;';;
 9) ssh 172.18.197.41 "cd /home/x772525;chmod 777 $name;cp -p $name $destpath"
 
esac

the whole thing is executing fine, but at the end when i use su - d_od, it is giving me the following error:
Must be attached to terminal for 'am I' option
stty: : Invalid argument

I need to change the permissions of the file using d_od. Can someone please help me with this?

Thanks
Ajay

In ksh, you might want to use the variable $LOGNAME instead of executing `whoami`.

Thanks dsw. Now it is working, but unable to read the variables defined in the local machine when doing su - d_od. How can I do that ?

ssh -t 172.18.197.41 'su - d_od -c "cd /home/x772525;"cp -p $name $destpath;""';;

If I hardcode the variables, it is working fine. How can I do this?

Thanks
Ajay

[EDIT: Oops, I see you are doing this over ssh, so ignore the below (it's valid for local purposes, however]

That's because you gain a new environment when you su with the "-", and the variables are defined in the current user's environment, hence they don't persist across the su.

If this is linux, you can try "su -m" to preserve the environment, otherwise just drop the "-" and see what you get.

Even after dropping the -, it is still giving the same error :frowning:

Yeah, I edited my post above.

You have to use double quotes for the command so the shell can expand the var ahead of time. Single quotes pass it literally.

You'll have to escape the double "" with \"\" though, as you want *that* to be passed literally I assume.

---------- Post updated at 10:00 AM ---------- Previous update was at 09:58 AM ----------

Try this instead.

"su - d_od -c \"cd /home/x772525;cp -p $name $destpath\""

1 Like

Thanks a lot. It is working :slight_smile: