How to set IFS for a specific command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to set IFS for a specific command
# 1  
Old 03-11-2009
Question How to set IFS for a specific command

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:
Code:
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:
Code:
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:
Code:
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:
Code:
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:
Code:
read a b c <<< "$($MYSQL -Ne "SELECT COUNT(1), MAX(name), MAX(space) FROM terminal")"

Instead of:
Code:
IFS=' '
result=$($MYSQL -Ne "SELECT COUNT(1), MAX(name), MAX(space) FROM terminal")
IFS=$(echo -e "\t")
read a b c <<< "$result"

# 2  
Old 03-11-2009
I got lost in your post, but in general I leave IFS alone so it has its default global value. Then if I need a special setting, I set it for that one command:

while IFS="" read line ; do

as one example. There the read statement gets its own IFS setting, but everything else gets the default. You just put the IFS value in front on the command.
# 3  
Old 03-11-2009
Well, I think I found something.
Don't really know why it works, just tried several possibilities.
Code:
MYSQL="mysql -u user -ppassword database"
TAB=$(echo -e "\t")

{ IFS="$TAB" read a b c; } <<< "$($MYSQL -Ne "SELECT id, nom, is_master FROM terminal WHERE id = 4")"
echo "$a-$b-$c"

{ IFS="$TAB" read a b c; } <<< "$($MYSQL -Ne "SELECT id, nom, is_master FROM terminal WHERE id = 14")"
echo "$a-$b-$c"

# 4  
Old 03-11-2009
Thanks Perderabo for your help.
I knew this trick for while read and that was the bases of my attempts.
Could you then explain me why I have to put the statement between braces?

Update: If I don't put the braces, IFS is modified for the entire script.

Last edited by chebarbudo; 03-11-2009 at 09:57 PM.. Reason: update
# 5  
Old 03-11-2009
You should not need the braces and I cannot explain why you do. I'll try it tomorrow. The bash man page states:
The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described above in PARAMETERS. These assignment statements affect only the environment seen by that command.


The behavior you report violates that last sentence. Smilie
# 6  
Old 03-12-2009
Quote:
Originally Posted by Perderabo
I got lost in your post, but in general I leave IFS alone so it has its default global value. Then if I need a special setting, I set it for that one command:

while IFS="" read line ; do

as one example. There the read statement gets its own IFS setting, but everything else gets the default. You just put the IFS value in front on the command.
I find that some shells(bash) treat IFS specially, overwriting it when you wouldn't expect it to.
# 7  
Old 03-12-2009
Quote:
Originally Posted by Corona688
I find that some shells(bash) treat IFS specially, overwriting it when you wouldn't expect it to.
One more reason for me to stay with ksh. I do that all the time with ksh and I never observe unexpected behavior involving IFS. I'll try it with bash tomorrow and see what happens.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to update field based on a specific set of rules

In the perl below, which does execute, I am having trouble with the else in Rule 3. The digit in f{8} is extracted and used to update f accordinly along with the value in f. There can be either - * or + before the number that is extracted but the same logic applies, that is if the value is greater... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Odd Behaviour for Set and IFS variable

Ok, so I'm playing around with delimters and reading files. and I came across this behaviour that I thought was a bit odd, regarding how the set command takes values... If I run this: IFS=$'-' #Assigns the - as the default delimiter for bash set I-love-my-gf-a-lot #uses set to put a bunch of... (1 Reply)
Discussion started by: Lost in Cyberia
1 Replies

3. Solaris

Set script to run during specific times

Hi all, I have this script which sends mail whenever the system is down. It works fine. Normally the system is down from 21 00 to 21 30 from Monday to Saturday and from 21 00 on Sunday to Monday 06 00 for maintenance. So I want the below script to run only when the system is up, i.e outside the... (2 Replies)
Discussion started by: frum
2 Replies

4. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

5. Shell Programming and Scripting

Print specific lines of a repeated set of data

I have a file that needs 1st line, 2nd line, and 26th line printed from every chunk of data. Each chunk of data contains 26 lines (#line+%line+24 data lines = 26 lines of data repeated). Input file: # This is a data file used for blockA (chunk 1). % 10576 A 10 0 1 04 (data1) 03 (data2)... (2 Replies)
Discussion started by: morrbie
2 Replies

6. HP-UX

What is the use of command set -- and set - variable?

Hi, I am using hp unix i want to know the use of the following commands set -- set - variable thanks (4 Replies)
Discussion started by: gomathi
4 Replies

7. Shell Programming and Scripting

BASH - set specific user variable via string operators

Apologies for the utter triviality of this question, but we all have to start somewhere! I've also tried searching but this question is pretty vague so I didn't (a) really know what to search for or (b) get many relevant hits to what I did search for. Anyway, I'm in the process of self-teaching... (1 Reply)
Discussion started by: u5j84
1 Replies

8. Shell Programming and Scripting

Perform a set of actions for a specific file type

Hello, I have a problem that I'm having quite a bit of trouble with. I am trying to create a script that performs a specific sequence of actions for a file of a specific type. This is an abbreviated version of my basic script: #!/bin/sh #coulombic calculations... (2 Replies)
Discussion started by: oehtus
2 Replies

9. Shell Programming and Scripting

Set specific part in command output into variable

I am trying unsuccessfully to set into a variable a specific part of command output: The command output will be as: line 1: <varied> line 2: 2 options: option 1: Set view: ** NONE ** or option 2: Set view: <different_name_of_views_always_without_spaces> and I would like to get into... (7 Replies)
Discussion started by: orit
7 Replies

10. UNIX for Dummies Questions & Answers

Help on IFS command!

Hi! I am working in korn shell. I want to reset the dimiliter for the set command to "|" but instead of a command prompt return I am getting something as below After issuing the command I am getting this....as if the shell is expecting something else. Can anybody suggest what's the problem. ... (2 Replies)
Discussion started by: udiptya
2 Replies
Login or Register to Ask a Question