Bash IFS


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash IFS
# 1  
Old 09-06-2012
Bash IFS

I am using bash and resetting IFS as below when reading the command line arguments. I do this so I can call my script as in Ex1.

Code:
Ex1: ./synt2d-ray3dmod.bash --xsrc=12/20/30

This allows me to split both sides so that when I do "shift"
I can get 12/20/30

What I do not understand is how calling Ex2 works also.

Ex2: ./synt2d-ray3dmod.bash --xsrc 12/20/30

Code:
OLDIFS="$IFS"
IFS="|="                # IFS controls splitting. Split on "|" and "="
set -- $*               # Set the positional parameters to the command line arguments.
IFS="$OLDIFS"

narg="$#"

while [ "$#" -gt 0 ]
do

  case "$1" in

  # name of output segy file
  "--ofl")
    shift
    val_ofl="${1}"
    hasArg_ofl="true"
  ;;

  # number ot time samples
  "--nt")
    shift
    val_nt="${1}"
    hasArg_nt="true"
  ;;

  # x-positions of sources
  "--xsrc")
    shift
    val_xSrc="${1}"
    hasArg_xSrc="true"
  ;;

  *)
    value_errLst="$arg_errLst ${1}"
    hasArg_errLst="true"
  ;;

  esac

  shift                 # Skip ahead to the next argument

done

# 2  
Old 09-06-2012
IFS works both ways. The shell splits strings apart on IFS, and when you put together a string with $*, it also puts them back together with IFS.

So, when you do ./myscript --xsrc 1234, $* actually turns it into --xsrc|1234. You just don't see it because you're letting the shell split it right back apart afterwards.

To prevent the shell from splitting something, you put it in quotes, so try this:

Code:
OLDIFS="$IFS"
IFS="|="                # IFS controls splitting. Split on "|" and "="
echo "$*"
set -- $*               # Set the positional parameters to the command line arg$
IFS="$OLDIFS"

Code:
$ ./script --arg=1234 # One argument

--arg=1234

$ ./script --arg 1234 # Two arguments

--arg|1234

$

So you see, the first time it comes in as 1 argument and nothing gets inserted by $*.

The second time it comes as two arguments, and gets stuck together into one string with | between.

IFS splits on both | and =, so set -- $* splits it apart where appropriate either way.

Last edited by Corona688; 09-06-2012 at 03:37 PM..
# 3  
Old 09-06-2012
This is not a "BASH thing", by the way. Any Bourne shell does this.
# 4  
Old 09-06-2012
There is another thing I am unsure about. It is when I am resetting IFS. My dilemma is what happens when I use the code I attached and the following

Code:
OLDIFS="$IFS"
IFS="|="                # IFS controls splitting. Split on "|" and "="
set -- $*               # Set the positional parameters to the command line arguments.

narg="$#"

while [ "$#" -gt 0 ]
do

  case "$1" in

  # name of output segy file
  "--ofl")
    shift
    val_ofl="${1}"
    hasArg_ofl="true"
  ;;

  # number ot time samples
  "--nt")
    shift
    val_nt="${1}"
    hasArg_nt="true"
  ;;

  # x-positions of sources
  "--xsrc")
    shift
    val_xSrc="${1}"
    hasArg_xSrc="true"
  ;;

  *)
    value_errLst="$arg_errLst ${1}"
    hasArg_errLst="true"
  ;;

  esac

  shift                 # Skip ahead to the next argument

done

IFS="$OLDIFS"

---------- Post updated at 02:06 PM ---------- Previous update was at 02:01 PM ----------

Originally my thinking was to reset IFS after the while (after processing all arguments). However, doing
Code:
OLDIFS="$IFS"
IFS="|="                # IFS controls splitting. Split on "|" and "="
set -- $*               # Set the positional parameters to the command line arg$
IFS="$OLDIFS"

while [ "$#" -gt 0 ]
do

...

does not seem to pose problems. What's happening?
# 5  
Old 09-06-2012
Changing IFS won't change splitting that's already happened. All the splitting for arguments happens in the third line, and gets saved into $1 $2 etc. The content and order of the $1 $2 ... variables won't change unless you do another set --.

I'd put the fourth line right below the first three just so you don't forget and try and split something later, and get weird results.

Last edited by Corona688; 09-06-2012 at 04:13 PM..
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 09-06-2012
Also, doing echo $* after set removes all = and replaces them with |

Code:
echo $*
set -- $*
echo $*

results is

Code:
./synt2d-ray3dmod.bash --xsrc=12/20/30 --verbose=3 --ofl=test.sgy

--xsrc=12/20/30|--verbose=3|--ofl=test.sgy
--xsrc|12/20/30|--verbose|3|--ofl|test.sgy

# 7  
Old 09-06-2012
That's not the code you ran. You must have been running echo "$*" quotes and all, otherwise you'd have never seen the pipes.

$* after the set didn't remove the equals, it was already gone. If you ran ./script a b --arg=c, $* on line 3 wopuld be the string a|b|--arg=c and split apart on "|=" into the arguments "a", "b", "--arg", and "c" The equals sign is already gone, deleted.

So when you do $* after that, it just smashes $1 $2 ... all together with | inbetween: a|b|--arg|c

Now remember, $* always, always does this, even if you don't quote it. Smilie You just don't see it without quotes, because the shell splits it. It'd see a|b|--arg|c, give "a" as echo's first argument, "b" as echo's second argument, "--arg" as echo's third argument, and "c" as echo's fourth argument. (echo doesn't split the arguments; that's the shell's job.) echo is not controlled by IFS, and will just put spaces inbetween...

Last edited by Corona688; 09-06-2012 at 04:52 PM..
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