5

How can I execute three commands on one command line in Linux? I tried the below:

sudo -u oracle -i ; cd /lo2/ram/daska; ./script.sh

When I execute this only the sudo command is executing.

Please advise me

5 Answers 5

4

Use && separator

sudo -u oracle -i && cd /lo2/ram/daska && ./script.sh
Sign up to request clarification or add additional context in comments.

3 Comments

just bear in mind that when using && if any command in the chain fails, the rest won't be executed
Nope, this is not working I've tried with semicolon and && symbols ,its executing only first command .. please check again
If all commands succeed, then replacing ; with && makes no difference. In any case, this is not the issue and doesn't address the question.
3

After executing sudo there's a new shell and the rest of "commands" are not part of it but part of the parent shell. You can do:

 sudo -u oracle -i bash -c "cd /lo2/ram/daska && ./script.sh"

Or directly,

 sudo -u oracle -i /lo2/ram/daska/script.sh

1 Comment

The latter depending on if the script needs this particular working directory or not...
3

You can also use semicolon to seperate your command

sudo -u oracle -i ; cd /lo2/ram/daska ; ./script.sh

The difference between using && and semicolon is that if you want to execute each command only if the previous one succeeds, then you can use the && operator. However if you want to execute commands, irrespective of previous one executes or not, you can use semicolon (;) to separate the commands.

3 Comments

Nope, this is not working I've tried with semicolon and && symbols ,its executing only first command .. please check again
@Ramdas:- Did you see the answer given by glglgl sudo -u oracle sh -c 'cd /lo2/ram/daska; ./script.sh'
Yes, but the oracle profile not getting there when i using the above command with -i .. i'm getting errors failing sqlplus command not found, When i execute the command manually its working fine
3

I'll just add to Piperoman's and Rahul's answers that with && the later command is only executed if the first succeeds and with ; following command is always executed.

So

sudo -u oracle -i ; cd /lo2/ram/daska ; ./script.sh

if you don't care whether everything in the chain executes, and

sudo -u oracle -i && cd /lo2/ram/daska && ./script.sh

if you do.

Comments

3

If you do

sudo -u oracle -i ; cd /lo2/ram/daska; ./script.sh

you tell that a login shell running under user oracle should be started. That happens, and the other commands are executed after you leave this shell.

This is probably not what you want.

I see the following option:

sudo -u oracle sh -c 'cd /lo2/ram/daska; ./script.sh'

which in principle is mentioned in sudo's man page.

Comments

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.