Hi there,
I'm using two commands that need different IFS.
The mysql command need IFS to include space because I'm given the mysql command as a variable:
supernova:~# cat myscript
IFS=' '
MYSQL="mysql -u user -ppassword database"
$MYSQL -Ne "SELECT COUNT(1), MAX(id), MAX(name) FROM terminal"
supernova:~# bash myscript
+---+-------------+---------+
| 8 | wide screen | library |
+---+-------------+---------+
If IFS doesn't include space, then $MYSQL is considered as one word and not command + switch:
supernova:~# cat myscript
IFS=
MYSQL="mysql -u user -ppassword database"
$MYSQL -Ne "SELECT COUNT(1), MAX(id), MAX(name) FROM terminal"
supernova:~# bash myscript
myscrypt: line 3: mysql -u user -ppassword database: command not found
The read command needs IFS to include tab and exclude space:
supernova:~# cat myscript
IFS=' '
MYSQL="mysql -u user -ppassword database"
result=$($MYSQL -Ne "SELECT COUNT(1), MAX(name), MAX(space) FROM terminal")
IFS=$(echo -e "\t")
read a b c <<< "$result"
echo $a-$b-$c
supernova:~# bash myscript
8-wide screen-library
If IFS include space, then mysql output is not correctly interpreted:
supernova:~# cat myscript
IFS=' '
MYSQL="mysql -u user -ppassword database"
result=$($MYSQL -Ne "SELECT COUNT(1), MAX(name), MAX(space) FROM terminal")
IFS=$(echo -e "\t ")
read a b c <<< "$result"
echo $a-$b-$c
supernova:~# bash myscript
8-wide-screen library
I have to run this kind of queries many time in the script and thought it's a pain in the ass to set IFS two times every time I run a query.
Is there any way I can write something like:
read a b c <<< "$($MYSQL -Ne "SELECT COUNT(1), MAX(name), MAX(space) FROM terminal")"
Instead of:
IFS=' '
result=$($MYSQL -Ne "SELECT COUNT(1), MAX(name), MAX(space) FROM terminal")
IFS=$(echo -e "\t")
read a b c <<< "$result"