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
Use && separator
sudo -u oracle -i && cd /lo2/ram/daska && ./script.sh
&& if any command in the chain fails, the rest won't be executed; with && makes no difference. In any case, this is not the issue and doesn't address the question.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
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.
sudo -u oracle sh -c 'cd /lo2/ram/daska; ./script.sh'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.
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.