2

Saw one post here: Change PS1 after running the 'script' command but it didn't seem to answer the question.

I am trying to have a script run the "script" command and also change the prompt (PS1) to indicate that the shell is now scripted. Basically, the normal PS1 has "[UNSCRIPTED] in it in red so that the assessor knows they are not using a scripted window. Once they are ready to do their assessment, they would run this script, the window would now be scripted, and the prompt would change to indicate such.

Right now it works if you paste the commands one by one but when trying to role them into a script is does not work because the "script" command starts a new process so the PS1 change will only apply to the parent process (the shell script was ran from) and doesn't take effect until exiting the "script" process.

I have tried many variations of backgrounding the script process (&), running the commands in sequence (; and &&) but those all still wait for the parent process (the script command) to finish before moving on.

Here are the commands that work if ran line by line (copy/paste) but I cannot get to work in a shell script:

script -af /opdata/logs/scripted/script.$$

# SETUP WINDOW ENVIRONMENT
PS1='\d \t \u@\h \[\e[38;5;33m\]\w \[\e[0m\]# '
date -u
date
hostname
ifconfig -a
netstat -nr
uname -a
Community
  • 1
  • 1

3 Answers3

1

You can run

script -af /tmp/script.out -c "export PS1='\d \t \u@\h \[\e[38;5;33m\]\w \[\e[0m\]# '; date -u; date; hostname; ifconfig -a; netstat -nr; uname -a; bash --norc"

Than runs the bash without parsing any rc-files and so the exported PS1 is not changed.

Mario Keller
  • 411
  • 2
  • 5
  • I was having an issue using the -c flag because the script would close after running my commands, I hand't thought to run bash as the final command. – Trevor Steen Mar 16 '17 at 15:16
1

You can try use the commands in a bash-script (let say scbash.sh) as:

script -a /opdata/logs/scripted/script.$$ bash --init-file <(cat <<'EOF'
#these commands will run BEFORE the scripted bash goes interactive
PS1='SCRIPTED >'
ls
date
EOF
)

Run scbash.sh and you will get

Script started, output file is ....
#ls output
#date output
SCRIPTED >  #< as prompt
...
#interactive scripted bash here using a prompt 'SCRIPTTED >'
exit
Script done, output file is...

when the script get finished... you will continue with your original PS1.

clt60
  • 62,119
  • 17
  • 107
  • 194
1

Another option is to create a file with your init commands for example .scriptrc

# SETUP WINDOW ENVIRONMENT
PS1='\d \t \u@\h \[\e[38;5;33m\]\w \[\e[0m\]# '
date -u
date
hostname
ifconfig -a
netstat -nr
uname -a

Then have your script command call bash with the --rcfile parameter to read that file on startup:

script -af /opdata/logs/scripted/script.$$ -c 'bash --rcfile ${HOME}/.scriptrc'
JoshMc
  • 10,239
  • 2
  • 19
  • 38