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

Re: using `myscript.sh` to change current env



On 9/7/13, Zenaan Harkness <zen@freedbms.net> wrote:
> On 9/7/13, Zenaan Harkness <zen@freedbms.net> wrote:

>> So I thought, run the script in a subshell, executing the result, like:
>> $ `ps1`
>>
>> The following 3-line script is meant to test exactly this:
>>
>> #!/bin/bash
>> PS1=': '
>> echo "export PS1=$PS1"

After trying a few other options, it appears that the only simple
solution is to type a few extra chars:
PS1=`ps1`

where the ps1 script spits out the prompt content, not the command to
change the prompt. And that leads me to another potential solution,
see below.

This is reasonable given that all I want to do is embed my 'preferred
alternative prompts' in a script, so I can pass in a cmd line arg into
ps1 to pick a prompt on the fly, without having to go in and
cut-and-paste, or tediously locate the actual location of my ps1
script every time "source"ing it.

Now, the alternate solution arises from the above, and from the
/etc/network/interfaces suggestion made yesterday by someone:

ps1 script can create itself a temporary file, and echo the command to
source that file. This might be solution solved ... exploring now ...

By the way, I've searched the net, from stackexchange to
comp.unix.shell, and have not yet found a solution to this seemingly
so simple "problem".

Voila! It works!

My .bashrc used to have: source $BASHDIR/prompt
Now I just use `ps1` (after setting my path to include my bin/ dir
with the ps1 script of course).

And here it is, script "ps1" to modify parent shell environment (by
running the script with command execution):

#!/bin/bash

# Bash script to change parent environment.
# Relies on storing the desired changes into a tmp file
# (see TMPFILE below),
# as well as on using command substitution when script is run.
# So therefore must be run as eg: `ps1`

GREEN_PROMPT='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]
\D{%Y%m%d} \t\[\033[00;36m\] \[\033[01;33m\]\w\[\033[00m\]\$ '

case "$1" in
   0) # simplest prompt (zero jazz):
      case `whoami` in
         root)
            PS1='# '
            ;;
         *)
            PS1='\$ '
            ;;
      esac
      ;;
   t*)
      # 't'imed - add date-time stamp to prompt:
      case `whoami` in
         root)
            PS1='\D{%Y%m%d}-\t\# '
            ;;
         *)
            PS1='\D{%Y%m%d}-\t\$ '
            ;;
      esac
      ;;
   *)
      # default: reset back to my preferred default prompt:
      case `whoami` in
         root)
            PS1='\D{%Y%m%d}-\t\# '
            ;;
         *)
            case "$TERM" in
               xterm* | rxvt* | linux | screen* | cygwin)
                  PS1=$GREEN_PROMPT
                  ;;
               *)

PS1='${debian_chroot:+($debian_chroot)}\u@\h:$(__git_ps1 "%s"):\w\$ '
                  ;;
            esac
            ;;
      esac
      ;;
esac

TMPFILE=~/.`basename $0`.current-prompt
echo "export PS1=\"$PS1\"" > $TMPFILE
echo "source $TMPFILE"

Thanks for your assistance :)
Zenaan


Reply to: