0

I have seen this (and I upvoted it), but it falls a bit short from my needs because:

  • I have to I need to give on-terminal feedback (that is easy, just | tee /dev/tty)
  • I have to get input from users in several ways:
    • read -p 'prompt: ' var
    • select yn in "Yes" "No"; do ...
    • git clone from private repository asking user/pass

How can I do this (especially the select stuff)?

My current test setup (cut down to size) is:

#/bin/bash
set -e
set -x
{
if lxc list | grep container
then
  :
else
  lxc launch images:ubuntu/20.04 container
  echo 'waiting for networking to be up-and-running...' | tee /dev/tty
  sleep 5
  echo 'installing base system...' | tee /dev/tty
  lxc exec container -- apt update
  lxc exec container -- apt install -y git
  echo "install documentation processor? " | tee /dev/tty
  select yn in "Yes" "No"
  do
    if [ $yn == "Yes" ]
    then
      echo 'installing documentation processor...' | tee /dev/tty
      lxc exec container -- apt install -y wget
      lxc exec container -- wget https://github.com/plantuml/plantuml/releases/download/v1.2022.12/plantuml-1.2022.12.jar -O /usr/local/bin/plantuml.jar
    fi
    break
  done
  read -p 'enter your EMAIL address: ' email | tee /dev/tty
  lxc exec container --user 1000 --group 1000 --cwd /home/ubuntu --env HOME=/home/ubuntu -- git config --global user.email "$email"
  read -p 'enter your FULL NAME: ' name | tee /dev/tty
  lxc exec container --user 1000 --group 1000 --cwd /home/ubuntu --env HOME=/home/ubuntu -- git config --global user.name "$name"
  lxc exec container --user 1000 --group 1000 --cwd /home/ubuntu --env HOME=/home/ubuntu -- git config --global credential.helper store
  echo 'cloning repository...' | tee /dev/tty
  lxc exec container --user 1000 --group 1000 --cwd /home/ubuntu --env HOME=/home/ubuntu -- git clone git@github.com:gabime/spdlog.git
  echo "enable audio in container? " | tee /dev/tty
  select yn in "Yes" "No"
  do
    if [ $yn == "Yes" ]
    then
      for dev in $(find /dev/snd -type c)
      do
        lxc config device add container snd_$(basename $dev) unix-char source=$dev path=$dev gid=1000
        lxc exec container -- apt install -y alsa-utils
      done
    fi
    break
  done
fi
} >lxd_build.log 2>&1

This has all said defects:

  1. read -p prompts are not seen on terminal (but are in log file)
  2. select yn in "Yes" "No" prompts are not seen on terminal (but are in log file); this is unsurprising as select doesn't accept redirection.
  3. git clone prompt for user/pass are not seen on terminal.

How can I fix this?

ZioByte
  • 2,690
  • 1
  • 32
  • 68
  • So how your question is not a duplicate of https://stackoverflow.com/questions/314675/how-to-redirect-output-of-an-entire-shell-script-within-the-script-itself ? – KamilCuk Nov 13 '22 at 11:46
  • @KamilCuk: no, that is the base and works well, but I also need to have some direct output to terminal, in specific places, to handle interactivity. Especially problematic is `select` which does not seem to allow redirection. – ZioByte Nov 13 '22 at 12:23
  • 1
    Does `exec 1> >(tee file.txt)` answer your question? https://unix.stackexchange.com/questions/641042/sh-shell-redirect-output-to-both-terminal-and-file-inside-script . I still do not know, if you know that `that is easy, just | tee /dev/tty` so why not `{ ... } | tee file.txt`? – KamilCuk Nov 13 '22 at 12:26
  • @KamilCuk: thi does *not* answer my question because I do *not* want to see on terminal all programs output, but I *do* want everything on file and I want to see on terminal what needed for user interaction (obviously); I want to see question raised by `echo "do you want to install whatever"; select yn in "Yes" "No"; do ...`, but I *do not* want to see `apt install` output. OTOH I need to have in log file everything, possibly including user choices. I googled a bit, but all solutions are "all or nothing", with little space for "exceptions". Please notify if you think I should update OP. – ZioByte Nov 13 '22 at 14:38
  • Sooo `apt install > file.txt` but `echo "do you want to install whatever" | tee file.txt`, or the same with some file descriptors not to repeat yourself. `possibly including user choices` I guess `exec 0> >(tee user_choices.txt >&0)`, but I am not 100% sure it will work. `if you think I should update OP` I think "OP" usually refers to the person who posted "original poster". I think you should update your question. – KamilCuk Nov 13 '22 at 15:39
  • @KamilCuk: done. please see if it is more understandable, now. Thanks. – ZioByte Nov 13 '22 at 16:16
  • 1
    I would redesign the script so that the interactive prompting takes place before you redirect everything to a log file. Even better, let the user provide answers on the command line if they already know what they want. Scripts which require interactive I/O are problematic because you can't use them as building blocks for higher-level scripts. – tripleee Nov 14 '22 at 05:54

1 Answers1

1

read -p and select both output their prompts to standard error. Make sure you're capturing the right stream if you want to copy their prompts to both the terminal and a file. For example:

read -p 'enter your EMAIL address: ' email | tee /dev/tty # prompt doesn't get duplicated
read -p 'enter your EMAIL address: ' email 2>&1 | tee /dev/tty # prompt does get duplicated

###
# Note: These cases might get a bit annoying because the redirection
# applies to both the prompt and everything inside the select statement
# You may have to do some additional redirection inside the select
# statement to make everything work exactly the way you want
###
select yn in "Yes" "No" ; do
  break
done | tee /dev/tty # prompt does not get duplicated

select yn in "Yes" "No" ; do
  break
done 2>&1 | tee /dev/tty # prompt does get duplicated

git clone might be more complicated. If it's using something like OpenSSH under the covers to perform ssh operations then it could be writing directly to the TTY so you can't redirect its prompts just by redirecting standard out or standard error. You may have to dive into tools like expect to capture the password prompts and write them somewhere else.

tjm3772
  • 2,346
  • 2
  • 10