0

My script is as below.

#!/bin/bash

version = 1.1

echo "Enter username"

read UserName

ssh -t $UserName@server bash -c " '

./runSomeScript

echo "Entering Sudo"

sudo -s -u user1 -c "cd random; ./randomscrip xx-$version-yy"

'"

But this is not working.

Basically i want to do a ssh to a account. And then runSomeScript Then do a sudo with user as user1 and then run commands cd random and ./randomscrip (with xx-Version-yy as argument) as the sudo user only. But the commands inside sudo are not working.

1 Answer 1

2

Your quoting is a little careless. You're using double-quotes for the first and third levels of quoting, and the shell can't tell one from the other. Do something like this instead:

sudoScript="cd random; ./randomscrip xx-${version}-yy"
sshScript='
  ./runSomeScript
  echo "Entering Sudo"
  sudo -s -u user1 bash -c '"'${sudoScript}'"'
'
ssh -t ${UserName}@server "${sshScript}"

But beware that if you embed any single-quotes, it will still go wrong unless you add a layer of shell-quoting.

Finally, remove the spaces around = when you assign to version.

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

2 Comments

Not working, commands in sudoScript fails. Commands in sudoScript runs in ssh session instead of running in sudo mode.
@A.G. You're right. It seems sudo -s doesn't really run the command in a shell as the man page promised. I've edited it and tested it to make sure it works.

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.