[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Temporary color prompt in bash script



On Tue, Sep 03, 2019 at 01:07:08PM -0500, David Wright wrote:
> Trap is designed to break the sequential flow of commands.
> I can't see any reason to do that when the OP wants the
> sequence:
> 
>     execute a script
>     print a string

Are you actually able to discern the OP's desires?  I can't.  I found
everything they said in this thread completely impenetrable.  At first
I thought they wanted to write a script that would change the bash
prompt by setting the PS1 variable....

> If you make your screen narrow before you cut and paste, you won't end
> up with this mess, but something instead like:
> 
>        trap [-lp] [[arg] sigspec ...]
>               The command arg is to  be  read  and
>               executed  when  the  shell  receives
>               signal(s) sigspec.  If arg is absent
>               (and  there  is a single sigspec) or
>               -, each specified signal is reset to
>               its  original disposition (the value
>               it had upon entrance to the  shell).

An improvement to be sure, but you're still omitting the part about EXIT
which was, if I'm not mistaken, the entire purpose of bringing up 'trap'
in this context.

*If* the OP's desire is "Whenever my script exits, whether it's by normal
means or by crashing after an error, I want it to print the terminal
sequence that will turn text red." then the solution would be:

#!/bin/bash
trap 'tput setaf 1' EXIT
... rest of the script ...

If the OP's desire is something else, then the answer will be something
else, but we can't answer the question until we can figure out what the
question is.


Reply to: