简体   繁体   中英

How to redirect output of an entire shell script within the script itself? ... with a twist

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM