2

I'm working on a function which is intended to analyse result of executing emerge --pretend $package, and set USE flag in make.conf, and then execute emerge $package. Example code as below, at line 5, emerge --pretend worked fine, but at line 13, I got an error emerge: command not found.

Even if I removed the parenthesis between line 8 and line 15, nothing changed, same error, any idea? If replacing emerge with echo, same error, echo: command not found. It seemed both outside and inside of while loop are not at the same shell. Why and how to solve that?

Thank you very much!

1    #/bin/bash
2    function emgRecursion() {
3        local result
4        local str
5        result="$(emerge --pretend "="$1 | grep "\[ebuild")"
6        while read -r line
7        do  ( 
8            if [[  $line = *"USE=\""* ]]; then
9                echo "====="
10           else
11               str="${line#*"] "}"
12               str="${str%%" "*}"
13               emerge --pretend "="$str
14           fi
15           ) </dev/tty
16       done <<<"$result"
17    }
18    emgRecursion "sys-cluster/ceph-0.94.4"
agc
  • 7,973
  • 2
  • 29
  • 50
Tao Wang
  • 186
  • 1
  • 18
  • You don't get the same benefit of the `$PATH` that you do at a terminal. So, `which emerge` are you trying to run? – Jeff Bowman Nov 20 '15 at 21:37
  • 1
    "Command not found" means command not found. The command cannot be found on your `PATH`. Doesn't matter it's in a loop or not. – 4ae1e1 Nov 20 '15 at 21:37
  • @JeffBowman Please don't spread the bad habit of using `which`, which is not a builtin in bash. Use `type` instead. – 4ae1e1 Nov 20 '15 at 21:41
  • 2
    @4ae1e1 It didn't fit into a sentence. Yes, `type` is a better option for a script, but I was hoping there was room for a sense of humor here. – Jeff Bowman Nov 20 '15 at 21:50
  • Neither `which` nor `type` works. `which` or `type` command not found. Thanks for your opinions. If command `emerge` didn't exist in `path`, why did it work outside of while loop? – Tao Wang Nov 20 '15 at 22:11
  • @TaoWang I'd only meant to use `which`/`type` to get the full path for your `emerge`. To get a better sense of whether parts of your environment (including `PATH`) are available to be read and modified, I'd read up on [subshells](http://www.vidarholen.net/contents/blog/?p=178), which will also affect whether you can reset `str` the way you have above. – Jeff Bowman Nov 21 '15 at 02:10
  • 1
    Something is seriously wrong if you are getting `echo: command not found`; as a `bash` builtin, `echo` should not be subject to path lookup. – chepner Nov 21 '15 at 02:41
  • 7
    I'm guessing your problem is the bang line. You want `#!/bin/bash`, not `#/bin/bash`. Without the proper syntax, this may be executed by `/bin/sh` instead, and one of the string substitution bash-isms could confuse it. – Jonathan Ross Nov 21 '15 at 14:56

0 Answers0