Search code examples
linuxbashshellgentoo

How to select an option inside of while loop in bash


Package names are stored in a text file and grouped by row symbol #step{1,2,3...}, what I'd like to do is : step1)to execute emerge --pretend package, step2) manually collect USE flags from pretendedly emerging execution result shown on the screen, set make.conf, and then step3) execute emerge package. Charles gave me an excellent demonstration of how to deal with row symbol #step at here.

My question is if the text file looks like below which only contained with packagenames, is it possible to use Charles' demonstration, or how should I rewrite it? In the function getSteps() at the line printf '%s\n' "$line", I've modified it a little bit, it didn't work.

Any ideas? Thank you! The text file contained with package names:

#step1
grub
genkernel
sys-kernel/gentoo-sources
sys-apps/pciutils
...
#step2
dev-libs/boost
sys-cluster/ceph
sys-fs/lvm2
...

Charles' demonstration is at: here

What I'd like to have is something like:

getSteps() {
  local running=0
  while read -r line; do
    if (( running )); then
      if [[ $line = "#"* ]]; then
        return
      else
 #       printf '%s\n' "$line"
         #step 1)
         emerge --pretend $line
         #step 2)
         select packageType in "PACKAGEUSE" "PACKAGEKEYWORDS" "PACKAGELICENSE"
         do
             case $REPLY in 
             1) read USE flags as an input
                set make.conf
                #step 3)
                emerge $line
                ;;
             2) ....
         done
      fi
    else
      [[ $line = "#"$1 ]] && running=1
    fi
  done <stepFile
}

This line below worked, but the problem is the 'while loop' will keep going, there is no way for user to select an option and then continue. Any ideas?

printf '%s\n' "emerge --pretend $line" | sh -

Furthermore, is it possible to store the output into a variable for further analysis?


Solution

  • Problem solved by trying this. Add (your code here) </dev/tty in between of your code, as shown below.

       getSteps() {
          local running=0
          while read -r line; do
    (        if (( running )); then
              if [[ $line = "#"* ]]; then
                return
              else
    
         #       printf '%s\n' "$line"
                 #step 1)
                 emerge --pretend $line
                 #step 2)
                 select packageType in "PACKAGEUSE" "PACKAGEKEYWORDS" "PACKAGELICENSE"
                 do
                     case $REPLY in 
                     1) read USE flags as an input
                        set make.conf
                        #step 3)
                        emerge $line
                        ;;
                     2) ....
                 done) </dev/tty
              fi
            else
              [[ $line = "#"$1 ]] && running=1
            fi
          done <stepFile
        }