Sponsored Content
Top Forums Shell Programming and Scripting How to set IFS for a specific command Post 302296770 by chebarbudo on Wednesday 11th of March 2009 07:53:04 PM
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"

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 08:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy