I am running the latest mac OS and using zshell, and having trouble writing a shell script. I do most of my work from the command line, and wanted to have a bash script automatically save and log all of my input commands and output.
My most obvious choice was the bash "script" command, generally initiated on the command line as
script -a <filename>.txt
I wrote a shell script, log.sh, whose contents are below:
#! /bin/sh
echo "Welcome to the jungle."
# get date
now=$(date +%Y_%m_%d)
# create filename
fname='log_'$now'.txt'
# file save location
floc=~/path/to/directory/$fname
# start script
script -a -q $floc
This starts a script where the filename is today's date and saved in a designated directory. This works fine, except, however, the contents that are written to the file. Instead of just the plan text input/outputs I would expect, I get what I believe to be ANSI characters that make the file difficult to read.
Here is what my command line looks like:
~$ bash log.sh
Welcome to the jungle.
~$ echo "Hello world"
Hello world
~$ exit
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
And how it is recorded in the log file:
[1m[7m%[27m[1m[0m
]7;file://<myname>/Users/<myusername>
[0m[27m[24m[J[36m<myusername> [38;5;246m~[39m$ [K[?2004heecho "Hello world! "[?2004l
Hello world
[1m[7m%[27m[1m[0m
]7;file://<myname>/Users/<myusername>
[0m[27m[24m[J[36m<myusername> [38;5;246m~[39m$ [K[?2004heexit[?2004l
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
I am aware there are a few ways to handle the ANSI keys after creating the file, such as using the cat command to display the text, or a perl script to remove the keys; However, using the cat command is annoying when I am trying to look through a large file, and having to cleanup the file manually is tedious. Is there any way I can record my command line inputs and outputs without the ANSI keys?
Thank you!