Execute commands using sudo in remote server after logging into PuTTY through batch file
Because the commands that are contained in the command.txt
are executed by a master shell one-by-one.
So the master shell executes the sudo
, waits for it to exit, before it proceeds with the other commands (ls
and cd
). And the sudo
does not exit (at least not on its own).
While you want the ls
and cd
to execute within a child shell executed by the su
.
You have to tell the su
to execute the commands.
This should work:
sudo su - -c "ls ; cd directory" user1
or this
echo "ls; cd directory" | sudo su - user1
Though I expect that you actually want to continue working in the shell. While the above will exit once the commands are executed.
So you want to add a shell to the list of commands:
sudo su - -c "ls ; cd directory ; /bin/bash" user1
or
echo "ls ; cd directory ; /bin/bash" | sudo su - user1