LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-17-2009, 01:20 PM   #16
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,396

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814

Quote:
Originally Posted by catkin View Post
I set it in the shell initialisation; why not?
There could be a small annoyance if you send a script to someone else, your script will assume extglob is set, and it won't work with the default settings.

Quote:
My own scripting idiom is
Code:
oIFS="$IFS"
IFS="<whatever>"
<commands>
IFS="$oIFS"
Too bad bash doesn't have something like Lisp's let, then you could do
Code:
let IFS="<whatever>"
  <commands>
end
# IFS is restored
 
Old 07-17-2009, 04:03 PM   #17
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by ntubski View Post
There could be a small annoyance if you send a script to someone else, your script will assume extglob is set, and it won't work with the default settings.
Good point; thanks
Quote:
Originally Posted by ntubski View Post
Too bad bash doesn't have something like Lisp's let, then you could do
Code:
let IFS="<whatever>"
  <commands>
end
# IFS is restored
The closest we get is the illegible and inefficient
Code:
eval $( IFS=<whatever>; <do something using new $IFS>; echo <statements for the parent shell to execute>)
 
Old 07-17-2009, 04:50 PM   #18
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Quote:
Originally Posted by ghostdog74 View Post
an example might be more clear in what you are trying to explain. i am an english idiot.
What's so hard to understand? I'm simply pointing out that the "set --" command clears all previous positionals before creating new positionals with the new values. If later parts of your script rely on the previous values, they will break.

Quote:
#!/bin/bash

echo -----
echo $1
echo $2
echo $3
echo $4
echo -----

IFS="-"
a="C9-1.4-1.6"
set -- $a

echo -----
echo $1
echo $2
echo $3
echo $4
echo ------



david:[~]$ ./test.sh Fry Bender Leela Nibbler
-----
Fry
Bender
Leela
Nibbler
-----
-----
C9
1.4
1.6

------
All the previous values are lost, even the unused $4. That's ok if you don't need them anymore, but what if you do?
 
Old 07-17-2009, 05:07 PM   #19
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
As for using "unset" on IFS, I was just playing with this a day or two ago. I'm not sure what's going on with it exactly. The bash man page is unclear on what happens, and whether it returns it to the default value or not. But I ran some tests and it does seem to return to the default <space><tab><newline> behavior. I can't be perfectly sure it returns to the exact same state as before however.
 
Old 07-17-2009, 05:48 PM   #20
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by David the H. View Post
That's ok if you don't need them anymore, but what if you do?
you just have to save them if you want to use them later
Code:
echo -----
echo $1
echo $2
echo $3
echo $4
echo -----
save=("$@")
IFS="-"
a="C9-1.4-1.6"n
set -- $a

echo -----
echo $1
echo $2
echo $3
echo $4
echo ------
# 
unset IFS
set -- "${save[@]}"
echo -----
echo $1
echo $2
echo $3
echo $4
echo -----
Hence the reason why i can't understand why you say positional parameters are being lost (as in lost and can't be retrieved)

Last edited by ghostdog74; 07-17-2009 at 05:51 PM.
 
Old 07-17-2009, 06:09 PM   #21
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Sigh.

I never said that it couldn't be worked around; only that it's something that you need to be aware of. You can't just drop it into an existing script without there being possible ramifications. It's not the "cleanest" solution to the original request.

Besides, it also forces it farther and farther away from the "simple" one-liner, if you need to go to all that trouble to save the old positions.
 
Old 07-17-2009, 07:31 PM   #22
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by David the H. View Post
Sigh.

I never said that it couldn't be worked around; only that it's something that you need to be aware of. You can't just drop it into an existing script without there being possible ramifications. It's not the "cleanest" solution to the original request.

Besides, it also forces it farther and farther away from the "simple" one-liner, if you need to go to all that trouble to save the old positions.
well, if OP wants to learn bash, then he has to take note of these. For myself, i wouldn't even bother with bash when it comes to parsing text/files
Code:
awk 'BEGIN{ string="C9-1.4-1.6"; split(string, s,"-");print s[1]}'
 
Old 07-18-2009, 02:21 AM   #23
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by David the H. View Post
As for using "unset" on IFS, I was just playing with this a day or two ago. I'm not sure what's going on with it exactly. The bash man page is unclear on what happens, and whether it returns it to the default value or not. But I ran some tests and it does seem to return to the default <space><tab><newline> behavior. I can't be perfectly sure it returns to the exact same state as before however.
If IFS is unset then a lot of Bash behaves as if IFS had the default value of <space><tab><newline> so many tests are inconclusive about what IFS contains.
Code:
c@CW8:~$ echo -n "$IFS" | od -x
0000000 0920 000a
0000003
c@CW8:~$ unset IFS
c@CW8:~$ echo -n "$IFS" | od -x
0000000
c@CW8:~$
That's a little obfuscated by endian and padding effects but it take it that 0920 000a (<tab><space><null><newline>) actually denotes <space><tab><newline>
 
Old 07-19-2009, 08:27 AM   #24
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Quote:
Originally Posted by ghostdog74 View Post
well, if OP wants to learn bash, then he has to take note of these.
Which is exactly the reason I mentioned it. I was just trying to point out a possible "gotcha" that inexperienced bashers may not realize. I didn't intend for this to turn into a big debate or anything.

But as for "never using bash for text parsing", well, if all you need is to extract a short substring from a variable, then that's usually exactly what you do want to do. In most cases, using a bash built-in is more efficient than calling on an external tool like awk or sed. It's only when the text parsing becomes too complex to do quickly and easily in bash that calling on another tool becomes the better choice.

@catkin: Well, if bash generally behaves the same when IFS is unset as if it were the default, is there any situation in which unsetting it would cause problems? I suppose it would matter if you needed to specifically test for existence of one of the values, but would there be any serious effects otherwise?

Last edited by David the H.; 07-19-2009 at 08:41 AM.
 
Old 07-19-2009, 09:13 AM   #25
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by David the H. View Post
@catkin: Well, if bash generally behaves the same when IFS is unset as if it were the default, is there any situation in which unsetting it would cause problems? I suppose it would matter if you needed to specifically test for existence of one of the values, but would there be any serious effects otherwise?
Default and unset IFS are functionally identical according to the Open Group's (POSIX) shell definition. GNU's bash reference is not explicit but its section on differences in POSIX mode says nothing about IFS so presumably the default behaviour conforms with POSIX.

I'm going to try it and see if anything breaks.

EDIT:

Clarification: unset is not the same as empty (which is the same as null). Once a variable has been set it can only be unset using the unset command, according to The GNU Bash Reference Manual.

Last edited by catkin; 09-14-2009 at 01:35 PM.
 
  


Reply

Tags
bash, strings


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
chopping away unwanted parts in string C's Brother Programming 3 11-02-2008 07:56 AM
LXer: The trouble with rounding floating point numbers LXer Syndicated Linux News 0 08-12-2006 01:54 PM
IEEE 754 floating point numbers dmail Programming 3 02-05-2006 05:10 AM
modulo of 2 floating point numbers? jenna_h Programming 6 09-24-2004 09:39 AM
floating point numbers in java spyghost Programming 8 09-06-2003 06:21 PM


All times are GMT -5. The time now is 04:22 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration