Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: printing terminal stout to text file

  1. #11
    Join Date
    Dec 2014
    Beans
    1,906

    Re: printing terminal stout to text file

    What I meant was to have 'tee' multiple times inside the script. For example instead of
    Code:
    echo  -e "${blue}What is $x + $y?${nc}" | cowsay
    you'd do
    Code:
    echo  -e "${blue}What is $x + $y?${nc}" | tee --append ~charles/Tests/$file.txt | cowsay
    and get just the text without the ASCII-cow. This would also clean up most of the control sequences since for example 'clear' works by printing the sequence for 'move cursor to the home position', 'clear screen above the cursor', and 'clear screen below the cursor'. That's why I mentioned the option '--append' in my earlier post.

    As for using arrays for the messages: you can either use 'declare -a goodmsgs; mapfile goodmsgs < /home/charles/Scripts/Bash_in-progress/right_answer.txt' (don't try to use mapfile in a pipeline; each element of a pipeline runs in it's own subshell so the changes to the array variable would get lost). Or you could pull the messages into the script and do something like 'declare -a goodmsgs=("First message" "Second message" "Message number three")'. To avoid repeat messages, something like this should work:
    Code:
    declare -a goodmsgs=("First message" "Second message" "Message number three") # set up the array with the messages
    gml=$((${#goodmsgs[@]}-1))                                                    # limit for the random numbers for selecting from the array; we never select the last element; "used" messages get moved to the end
    index=$(($RANDOM%$gml))                                                       # select an element ...
    echo ${goodmsgs[${index}]}                                                    # ... print it ...
    tempvar=${goodmsgs[${index}]}                                                 # ... store it in a temporary variable ...
    goodmsgs[${index}]=${goodmsgs[$((${#goodmsgs[@]}-1))]}                        # ... overwrite it with the contents of the last element ...
    goodmsgs[$((${#goodmsgs[@]}-1))]=$tempvar                                     # ... then store the used message in the last element of the array.
    The thing to remember to understand this is that array indices in the shell (and in C) run from 0 to one less than the number of elements (so 0,1,2 for a three element array ...).

    Holger

  2. #12
    Join Date
    Oct 2018
    Beans
    99

    Re: printing terminal stout to text file

    Thanks Holger. I've looking for a way to use tee within the script. Your tips helps a lot, and being able to print only the needed stuff to the file saves me from having to rewrite the entire script. I'll also be taking a much closer look at the array script addition you gave, this looks like another perfect change. I really appreciate all the help, not just from you but the forums in general.
    I'm a firm believer that the only stupid question is the one you don't ask.
    Lenovo ideapad320-15iap, 1.1G Intel processor with onboard graphics, x64, 1TB SSD, 8GB ram
    lubuntu 20.04.2

  3. #13
    Join Date
    Dec 2014
    Beans
    1,906

    Re: printing terminal stout to text file

    Another thing: hard-coding terminal escape sequences is not a good idea. They are hard to read. Also while actual hardware terminals have become a historical curiosity they do exist and not all of them use the same escape sequences. Ubuntu comes with a database of terminal codes (terminfo) and a program named 'tput' to translate terminal independent mnemonics into actual escape sequences. So
    Code:
    green='\033[0;32m'
    can slightly more readably be written as
    Code:
    green=$(tput setaf 2)
    'setaf' in this means 'set a foreground colour' (three guesses what 'setab' means ...).
    There's manual pages both for 'tput' and 'terminfo'. The latter has a long list of terminal capabilities and the mnemonics for them.

    Holger
    Last edited by Holger_Gehrke; November 29th, 2021 at 01:08 AM.

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •