JCats
JCats

Reputation: 141

Using "while read" and "for i in" resulting in ambiguous redirect

# while read a b ; do echo $a $b ; done < `for i in \`cat users.txt\` ; do PHPVER=\`selectorctl --user-current --user=$i\` ; echo $i $PHPVER ; done | awk '{print $1, $2}' | sed 's/native/5.3/g'`

-bash: `for i in \`cat users.txt\` ; do PHPVER=\`selectorctl --user-current --user=$i\` ; echo $i $PHPVER ; done | awk '{print $1, $2}' | sed 's/native/5.3/g'`: ambiguous redirect

If you remove the while read statement, the output of:

# for i in `cat users.txt` ; do PHPVER=`selectorctl --user-current --user=$i` ; echo $i $PHPVER ; done | awk '{print $1, $2}' | sed 's/native/5.3/g'

user1 5.4
user2 5.4
user3 5.3

So I need to take these 2 columns and turn each into a variable this way I can then run them in a command like so:

# selectorctl --set-user-current=$b --user=$a

however when I add the while read statement, it throws the ambiguous redirect

Upvotes: 2

Views: 3241

Answers (2)

glenn jackman
glenn jackman

Reputation: 246774

It's really unclear what you're trying to do. You might want to do something like

while read -r user; do
    read phpver rest < <(selectorctl --user-current --user="$user")
    output="$user $phpver"
    echo "${output//native/5.3}"
done < users.txt

Don't use for to read lines from a file.

Upvotes: 4

JCats
JCats

Reputation: 141

while read -r user; do
    phpver=$(selectorctl --user-current --user="$user")
    output="$user $phpver"
    echo "${output//native/5.3}" | awk '{print $1, $2}'
done < users.txt

Is the placement of awk ok here or would there be a more proper way of removing the extra lines from the output which in this case is:

user 5.4    5.4.45  /opt/alt/php54/usr/bin/php-cgi

but I need

user 5.4

Upvotes: 0

Related Questions