0

I have a bash script and I want to run commands like these.

First-Command \
&& exec bash \
&& Last-Command

The exec bash will throw a error in the above way. ^^

If I write like this:

First-Command;
exec bash;
Last-Command

This way is not suitable for me because I want to run exec bash only after First-Command has completed.

Is there a work around of this scenario?

Can we connect some editor application to a terminal and submit commands to the terminal through that editor application.

Thank You. Regards, Yusuf

6
  • what is the error? You might need to join the three lines by ending the first two with a backslash. But also note the answer below, run bash without exec, else last-command won't be run. Commented Feb 10, 2015 at 5:19
  • Yes I missed the \. Read my Reply to John Commented Feb 10, 2015 at 5:25
  • Ok i got it. I dont have to run exec bash. Just running bash will refresh the bash and I am good to go. so I can add && bash at the last line. But If I add && bash in between the commands the commands after it wont work. Commented Feb 10, 2015 at 5:51
  • the inner bash would have to exit successfully for the && last command to run. To allow for errors, you could separate the last command with ; to run it unconditionally after the bash finishes. Semicolon-separated commands run in turn, each one starting after the one before it has finished. Commented Feb 10, 2015 at 5:59
  • Right Andras. I got you. After changing Hostname If I use sudo command the os is unable to resolve the host.. How can refresh the bash through script so that the sudo command and everything works fine after chaning hostname.. Without using exec bash or bash since that will discard the current bash and load new or current bash will be pushed to background... Commented Feb 10, 2015 at 10:45

1 Answer 1

2

exec replaces the running bash with the command (in this case, a new bash). It will never return, therefore putting && after it is nonsensical. You could fix it by removing exec.

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

5 Comments

I am trying to automate hadoop via bash script. After changing .bashrc file, hadoop config files, /etc/hosts, /etc/hostname I need to run exec bash. Because I cant run exec bash from script as it will start a new bash I have to do the things manually. Is there a work around where I dont have to do it manually? Check this video you will understand what I'm trying to say: youtube.com/watch?v=J61W9qXtuUE . At the end the hadoop command doesnt works so I had to run exec bash.
Ok I ran this. 1st line: {echo "hello" \} 2nd line: {&& bash \} 3rd line: {&& echo "bye"}. The "bye" wont print on the screen?
The "bye" will print once you exit the bash in the middle. In other words, you need to type "exit" or "Ctrl-D" (to give EOF).
Ok now I understood the concept of bash command. It will start a new bash. I thought it just refreshes the current bash. Can you check this: sudo sed -i '1s/.*/newhostname/' /etc/hostname \ && sudo hostname newhostname \ && sudo sed -i 's/old-hostname/newhostname/g' /etc/hosts. Now if I do sudo after these 3 commands sudo wont work before i do exec bash or bash. Is there a way to "Refresh" bash without restarting/closing the current bash (since bash starts new one and exec bash starts new and exits old)?
What do you mean "sudo won't work"? Is it because the host has been renamed? Does it give you an error message? What does "not work" mean?

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.