Re: Temporary color prompt in bash script
On Tue 03 Sep 2019 at 14:20:11 (-0400), Greg Wooledge wrote: > 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 I thought that at first, too, which is why I posted some of my .bashrc which is a sensible place to set it and is also sourced. When it appeared they just wanted to print the red string in much the same way as printing a string that "rings a bell", ie to attract attention, then I posted the ";" example. The sequence quoted above would work, but typing "print a string" while the script is possibly printing output of its own is error-prone, which is why $ execute a script ; print a string is safer. And it prints regardless, unless you hit ^C in which case it's unnecessary anyway. If the OP wants a different solution, the ball is in their court as far as I'm concerned: I think I quizzed them enough already. > > 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. man bash is a heavy read at the best of times, and that posting made it almost incomprehensible. I was only quoting the "newspaper column" version to demonstrate its legibility, not its content. I don't know quite what the point of quoting it was in any case. Bash is packaged with man bash so we've all got it. And will it help the OP without an example? > *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 ... That would have been a lot clearer. > 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. Cheers, David.
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.
Re: Temporary color prompt in bash script
On Tue 03 Sep 2019 at 15:07:43 (+), Larry Dighera wrote: > On Mon, 02 Sep 2019 08:48:50 +0200, Computer Planet > > wrote: > > >Is It possible to print of a string at the exit of a bash script? > > Have a look at `man bash` and search for 'trap.' 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 > 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 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). Cheers, David.
Re: Temporary color prompt in bash script
On Mon 02 Sep 2019 at 08:48:50 (+0200), Computer Planet wrote: > Thanks guys, > but this is not the solution I'm looking for ... It's always nice to get feedback on why, so that we're more likely to understand similar questions in future. For example, you never really explained whether "command no problem, write in red until exit or reboot" was something you want (everything red, or just prompts in red?) or something you don't want (nothing red after the one string) and whether you rebooted to clear a problem demonstrate that red prompts were lost (against your wishes). We don't read minds. > Now, I ask the question in other terms: > Is It possible to print of a string at the exit of a bash script? > e.g.: user@mypc: # bash script has just finished! [prompt] > with the prompt that remains immediately after the string printed. > > Thanks, as always, for reply. Sure: $ ./my-script ; echo -n "the string" or: $ ./my-script ; echo -n -e "\e[0;31mthe string\e[m" if you want "the string" in red. So, using cat to stand for your script, and sleep to stand for the time it takes your script to run: wren!david 12:46:59 ~ $ cat /etc/network/interfaces ; date ; sleep 5 ; echo -n -e "\e[0;31mthe string\e[m" # This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback # Mon Sep 2 12:47:01 CDT 2019 the stringwren!david 12:47:06 ~ $ Cheers, David.
Re: Temporary color prompt in bash script
On Mon, Sep 02, 2019 at 09:30:44AM +0200, Thomas Schmitt wrote: > Hi, > > Computer Planet wrote: > > Is It possible to print of a string at the exit of a bash script? > > e.g.: user@mypc: # bash script has just finished! [prompt] > > with the prompt that remains immediately after the string printed. > > Do you mean something like this ? > > $ echo -n 'user@mypc: # bash script has just finished! ' > user@mypc: # bash script has just finished! $ Yep, that's possibly the easiest option: doing the "painting" from whithin the script and not from the enclosing shell. Another avenue would be, since PS1 does shell expansion dynamically, to embed some command à la $(foo) whithin PS1. How that command communicates with your script is left as an exercise to the reader :) To illustrate what I may have expressed poorly above, try export PS1='Today is $(date). Your wish is my command: ' Have fun :) -- t signature.asc Description: Digital signature
Re: Temporary color prompt in bash script
Hi, Computer Planet wrote: > Is It possible to print of a string at the exit of a bash script? > e.g.: user@mypc: # bash script has just finished! [prompt] > with the prompt that remains immediately after the string printed. Do you mean something like this ? $ echo -n 'user@mypc: # bash script has just finished! ' user@mypc: # bash script has just finished! $ The string "$ " is the prompt as of shell variable PS1. Note that you need to set PS1 in the dialog shell, not in the script, because the setting in the script is not present any more when the dialog shell prints its prompt after the script ended. Have a nice day :) Thomas
Re: Temporary color prompt in bash script
On Mon, Sep 02, 2019 at 08:48:50AM +0200, Computer Planet wrote: > Thanks guys, > but this is not the solution I'm looking for ... > Now, I ask the question in other terms: > Is It possible to print of a string at the exit of a bash script? Hm. You mean echo Still a bit confused about where you are "in". Cheers -- t signature.asc Description: Digital signature
Re: Temporary color prompt in bash script
Thanks guys, but this is not the solution I'm looking for ... Now, I ask the question in other terms: Is It possible to print of a string at the exit of a bash script? e.g.: user@mypc: # bash script has just finished! [prompt] with the prompt that remains immediately after the string printed. Thanks, as always, for reply. > [...] > > > You have to source the script instead of running it. > > Yes, exactly. > > > I'm not entirely clear about a new shell gets it's own env that > > disappears when the shell exits thing, but try this: > > It's not only a shell thing. It's a basic Unix process thing: > child processes inherit (well, almost always) their parent's > environment -- more precisely a copy of it. They can change > their copy, but not the parent's. That's by design: lots of > loosely coupled programs calling each other (and that's what > Unix is, after all ;-) would degenerate into an unmaintainable > mess otherwise... > > > $ cat setps1 > > ##!/bin/bash > > # can i change ps1 from a script? > > echo "FOO = ${FOO}" > > export FOO=BAR > > echo "FOO now = ${FOO}" > > export PS1='\[\e]0;\u@\h: > > \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w > > #\[\033[91m\] ' > > > > $ export FOO=FOO > > $ ./setps1 > > FOO = FOO > > FOO now = BAR > > $ echo $FOO > > FOO > > > > See? FOO is back to it's pre ./setps1 value > > > > Now try > > $ source setps1 > > That's the way. There's a shorthand for "source", which is ".". So instead > of doing > > source setps1 > > you can say > > . setps1 > > Cheers > -- tomás >
Re: Temporary color prompt in bash script
On Sun, Sep 01, 2019 at 03:22:32PM -0400, Lee wrote: [...] > You have to source the script instead of running it. Yes, exactly. > I'm not entirely clear about a new shell gets it's own env that > disappears when the shell exits thing, but try this: It's not only a shell thing. It's a basic Unix process thing: child processes inherit (well, almost always) their parent's environment -- more precisely a copy of it. They can change their copy, but not the parent's. That's by design: lots of loosely coupled programs calling each other (and that's what Unix is, after all ;-) would degenerate into an unmaintainable mess otherwise... > $ cat setps1 > ##!/bin/bash > # can i change ps1 from a script? > echo "FOO = ${FOO}" > export FOO=BAR > echo "FOO now = ${FOO}" > export PS1='\[\e]0;\u@\h: > \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w > #\[\033[91m\] ' > > $ export FOO=FOO > $ ./setps1 > FOO = FOO > FOO now = BAR > $ echo $FOO > FOO > > See? FOO is back to it's pre ./setps1 value > > Now try > $ source setps1 That's the way. There's a shorthand for "source", which is ".". So instead of doing source setps1 you can say . setps1 Cheers -- tomás signature.asc Description: Digital signature
Re: Temporary color prompt in bash script
On 9/1/19, Computer Planet wrote: > Hi guys! > I'm trying, trying and trying but... > > How I Can put in hte end of a bash script this command: > PS1="\[\e]0;\u@\h: > \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w > #\[\033[91m\] " > so that after finishing the script the prompt will write in red...? > > If I try in prompt # PS1="\[\e]0;\u@\h: > \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w > #\[\033[91m\] " > command no problem, write in red until exit or reboot. You have to source the script instead of running it. I'm not entirely clear about a new shell gets it's own env that disappears when the shell exits thing, but try this: $ cat setps1 ##!/bin/bash # can i change ps1 from a script? echo "FOO = ${FOO}" export FOO=BAR echo "FOO now = ${FOO}" export PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w #\[\033[91m\] ' $ export FOO=FOO $ ./setps1 FOO = FOO FOO now = BAR $ echo $FOO FOO See? FOO is back to it's pre ./setps1 value Now try $ source setps1 Lee
Re: Temporary color prompt in bash script
On Sun 01 Sep 2019 at 17:53:51 (+0200), Computer Planet wrote: > Hi guys! > I'm trying, trying and trying but... > > How I Can put in hte end of a bash script this command: > PS1="\[\e]0;\u@\h: > \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w > #\[\033[91m\] " > so that after finishing the script the prompt will write in red...? > > If I try in prompt # PS1="\[\e]0;\u@\h: > \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w > #\[\033[91m\] " > command no problem, write in red until exit or reboot. Rather than figure out what exactly you're putting into your bash prompt, I'll just post mine, for my host that uses red, and let you pick out anything you want. Localcolor comes from a separate subfile. The warning is to remind me that PS1 should be set at the *end* of .bashrc because some of the subfiles identify themselves if PS1 is set. But you don't want any output generated from those subfiles, though, if .bashrc is running to initialise a non-interactive shell. The extra xterm item is for labelling xterm's title bars. --✄ Localcolor='\e[0;31m' # red ### # Don't change PS1 until after this point because its empty value # # prevents sourced files from writing output when non-interactive # ### # tty in the title bar makes it easier to kill an xterm should it freeze. export PROMPT_COMMAND='Myprompt="$? " && [ "$Myprompt" = "0 " ] && Myprompt=""' case "$TERM" in *xterm*) export PROMPT_COMMAND+=" ; printf '%b' '\e]0;${HOSTNAME^^} $(tty) ${HOSTNAME^^}\a'" ;; esac export PS1='\[\e[1;33;41m\]$Myprompt\[' export PS1+="$Localcolor" export PS1+='\]\H!\u \t \w \$ \[\e[m\]' unset Localcolor --✄ Cheers, David.