Run three commands on one command line on Linux

How can I execute three commands on the same command line in Linux? I tried the following:

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

When I execute this, only the sudo command is executed.

Please advise me

+5
source share
5 answers

After executing sudo new shell and the rest of the "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 
+3
source

Use && Separator

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

You can also use a semicolon to separate your command

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

The difference between using && and a semicolon is that if you want to execute each command only if the previous successfully completed, you can use the && operator. However, if you want to execute commands, regardless of whether they were executed earlier or not, you can use a semicolon (;) to separate the commands.

+3
source

I will simply add to Piperoman, and Rahul will answer that with && a later command is only executed if the first is successfully completed and with ; the following command is executed.

So

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

if you don’t care if everything in the chain is executed, and

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

if you do that.

+3
source

If you do

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

You will inform that you need to run the login shell as user oracle . This happens, and 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 is basically mentioned on the sudo man page.

+3
source

All Articles