Click to See Complete Forum and Search --> : Need help with a bash script...


George Kilroy
04-28-2002, 08:54 PM
I'm running an older terminal and midnight commander (curses) doesn't clear the screen right when I quit mc. Rather than type 'clear' three million more times in my life, I've decided to make a little bash script that will do it for me. I'll name that script 'mc2' and run that instead of 'mc'.
#!/bin/sh
exec mc
clear
This clears the terminal just as mc finishes starting up, so how can I make the 'clear' part waits until after mc been killed?

[ 28 April 2002: Message edited by: George Kilroy ]

Disc0stoo
04-28-2002, 09:06 PM
I think if you take out the "exec" it'll do what you want. But lemme make a suggestion: instead of doing that just put an alias in your ~/.bashrc that says:

alias mc="mc && clear"

Then when you type mc it'll do just that.

George Kilroy
04-28-2002, 10:18 PM
Ah, thanks much.

stiles
04-29-2002, 07:11 PM
Originally posted by George Kilroy:
<STRONG>
#!/bin/sh
exec mc
clear
This clears the terminal just as mc finishes starting up, so how can I make the 'clear' part waits until after mc been killed?
</STRONG>

exec executes whatever command in place of the current process and does not fork. Take out exec and your script should work as expected. When you fork mc the partent shell will wait till the child is finnished before executing the rest of the script (same thing applies to the alias that Disc0stoo shared, which is a cool way to get the job done).

bwkaz
04-30-2002, 09:09 AM
But note that if you ever pass options to mc, the alias will not work. What an alias does is wherever the shell sees the left-hand side of the alias, it replaces it with the right-hand side. So if you said, for example:

mc /usr/local/lib

Then the shell would turn it into this:

mc && clear /usr/local/lib

You might get a very strange error from clear, or you might get no errors but mc won't open up in the right place, or something else entirely might happen.

However, if your script looks like this:

#!/bin/sh
mc "$@"
clear

Then you will be able to pass your script options, and they will be passed on to the mc executable.