Bash IFS


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash IFS
# 15  
Old 09-06-2012
However I would keep the one described below, yes?

Code:
IFS="|="         
set -- $*
unset IFS

---------- Post updated at 06:51 PM ---------- Previous update was at 06:43 PM ----------

Quote:
Originally Posted by Corona688
You shouldn't be unsetting IFS, that could cause weird things later.

I'd reccomend doing things the way I showed you, instead.
I always thought that if I set a different IFS, I would need to unset it, so it keeps its shell default. Is that not so?

---------- Post updated at 06:56 PM ---------- Previous update was at 06:51 PM ----------

Quote:
Originally Posted by Corona688
Using arrays in shell often makes a problem harder. It looks like you've been struggling, but it's simple enough the traditional way... Just read into the variables you want and prefix it with IFS to let it split on what you want. Three things in one shot and no arrays. And it'll work in any shell, not just BASH.

Code:
# If variable is blank, set it
[ -z "$val_xSrc" ] && val_xSrc="-1.0/0/1.0"

# Prefixing a variable to a command sets that variable for only that line
IFS="/" read xSrc1 xSrc2 dxSrc <<EOF
${val_xSrc}
EOF

echo "xSrc1 = $xSrc1"
echo "xSrc2 = $xSrc2"
echo "dxSrc = $dxSrc"

I can't quite understand what you mean by your second comment

Code:
# Prefixing a variable to a command sets that variable for only that line

Do you mean that the change in IFS is only temporary when filling

Code:
xSrc1 xSrc2 dxSrc

but continues to be the bash default for the rest of the script?
# 16  
Old 09-06-2012
Quote:
Originally Posted by kristinu
However I would keep the one described below, yes?

Code:
IFS="|="         
set -- $*
unset IFS

What about the original code you had, where you did OLDIFS="$IFS" to put it back later? You want IFS to be default. You don't want it to actually be blank.
Quote:
I always thought that if I set a different IFS, I would need to unset it, so it keeps its shell default. Is that not so?
You want IFS to be default. You don't want it to actually be blank.
Quote:
I can't quite understand what you mean by your second comment

Code:
# Prefixing a variable to a command sets that variable for only that line

Do you mean that the change in IFS is only temporary when filling

Code:
xSrc1 xSrc2 dxSrc

but continues to be the bash default for the rest of the script?
Yes. Putting X=Y before a command sets that variable only for that line, and leaves it unchanged in the rest. It doesn't work everywhere, but it works for any external commands and also for the read builtin.
# 17  
Old 09-06-2012
I thought reset IFS puts the default and not blank.
# 18  
Old 09-06-2012
It does not.
# 19  
Old 09-06-2012
This might work better.

Code:
IFS="/" read xSrc1 xSrc2 dxSrc <<< "${val_xSrc}"


Last edited by kristinu; 09-06-2012 at 09:52 PM..
# 20  
Old 09-06-2012
Quote:
Originally Posted by kristinu
I thought reset IFS puts the default and not blank.
If IFS is unset, or its value is exactly <space><tab><newline>, the default, then sequences of <space>, <tab>, and <newline> at the beginning and end of the results of the previous expansions are ignored, and any sequence of IFS characters not at the beginning or end serves to delimit words.

Code:
mute@flo-rida:~$ echo $'[ space\t tab\nnewline]' | { IFS=/ read -rd '' lb s t n rb; echo "$lb:$s:$t:$n:$rb"; }
[ space  tab
newline]
::::
mute@flo-rida:~$ echo $'[ space\t tab\nnewline]' | {  read -rd '' lb s t n rb; echo "$lb:$s:$t:$n:$rb"; }
[:space:tab:newline]:
mute@flo-rida:~$ unset IFS
mute@flo-rida:~$ echo $'[ space\t tab\nnewline]' | {  read -rd '' lb s t n rb; echo "$lb:$s:$t:$n:$rb"; }
[:space:tab:newline]:

# 21  
Old 09-06-2012
Is Corona correct?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash : More parameter expansion and IFS

I am trying to become more fluent with the interworking of bash and minimize the number of external calls. Sample Data. This will be the response of the snmp query. SNMPv2-MIB::sysName.0 = STRING: SomeHostName SNMPv2-MIB::sysObjectID.0 = OID: SNMPv2-SMI::enterprises.9.1.1745... (5 Replies)
Discussion started by: sumguy
5 Replies

2. Shell Programming and Scripting

Remote while IFS

Hello masters of scripting, I've been working to develop some basic monitoring scripts. I have solved one problem, but want to know how to solve the other. I have a script that runs locally to create an output file with the Linux system kernel paramters, preceeded by the system name: ... (2 Replies)
Discussion started by: LinuxRacr
2 Replies

3. Shell Programming and Scripting

Changing IFS in bash function

I have a function in bash that takes arguments. does IFS work in a function or does it apply only to the main script? (1 Reply)
Discussion started by: kristinu
1 Replies

4. Shell Programming and Scripting

Not able to understand IFS

Hi , i am in my initial learning phase of unix. i was going thru the function part. below is the example which was there but i am not able to understand logic and the use of IFS(internal field separator) lspath() { OLDIFS="$IFS" IFS=: for DIR in $PATH ; do echo $DIR ; done IFS="$OLDIFS"... (8 Replies)
Discussion started by: scriptor
8 Replies

5. Shell Programming and Scripting

How to use IFS in this scenario?

Given the scenario like this, if at all if have to use IFS on the below given example, how it should be used. IFS=/ eg: /xyz/123/348/file1 I want to use the last slash /file1 . So can anyone, suggest me how to pick the last "/" as a IFS. (4 Replies)
Discussion started by: raghunsi
4 Replies

6. Shell Programming and Scripting

read and IFS

Hi, This is out of curiosity: I wanted to extract year, month and date from a variable, and thought that combining read and IFS would help, but this doesn't work: echo "2010 10 12" | read y m d I could extract the parts of the date when separated by a -, and setting IFS in a subshell: ... (3 Replies)
Discussion started by: raphinou
3 Replies

7. Shell Programming and Scripting

while loop with 3 ifs

im messing up somehwere...and can't seem to clean up the script...for it to work objectives: 1. check for today's file, and sleep 30 secs between retries 2. only allow 5 tries before script should fail. 3. if today's file found, wait 30 seconds for it to process.. code: count=0... (8 Replies)
Discussion started by: sigh2010
8 Replies

8. Shell Programming and Scripting

regarding IFS=

hi i am a learner can some explain "export IFS=$(echo "\n\t\a")" i am not able to understand the functionality please help thanks Satya (1 Reply)
Discussion started by: Satyak
1 Replies

9. Shell Programming and Scripting

problem with IFS

hi, :) I set IFS=":" But when i try to echo $IFS,i am not getting any thing on the screen escept a blank line. any help pls. cheers RRK (11 Replies)
Discussion started by: ravi raj kumar
11 Replies

10. UNIX for Dummies Questions & Answers

IFS variable

How can I set the value for IFS variable (2 Replies)
Discussion started by: mahabunta
2 Replies
Login or Register to Ask a Question