Re: string split, bash and IFS
#!/bin/bash # Split the command line argument on the colon character. SaveIFS=$IFS IFS=: declare -a Array=($*) IFS=SaveIFS echo Array[0]=${Array[0]} echo Array[1]=${Array[1]} echo Array[2]=${Array[2]} echo Array[3]=${Array[3]} Unga wrote: Hi all How to use bash and IFS to split a string? eg. $string = Name:Surname:10 IFS=: echo $string | read name surname age This does not work for some reason. The read does not create name, surname and age variables. Any idea why? Appreciate your reply. Kind regards Unga ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED] -- View this message in context: http://www.nabble.com/string-split%2C-bash-and-IFS-tp19140697p19335104.html Sent from the freebsd-questions mailing list archive at Nabble.com. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: string split, bash and IFS
Please allow me a sidenote: On Fri, 5 Sep 2008 09:38:29 -0700 (PDT), Jim Hertzler [EMAIL PROTECTED] wrote: #!/bin/bash ^ Isn't compatible to FreeBSD, I think, because BASH is an additional package and the bash binary will be installed into /usr/local/bin/bash; unless you're not using any features that are exclusively in bash (and not in sh), declare /bin/sh as shell (standard scripting shell in UNIX). So if you use BASH on FreeBSD, BASH scripts would need to have the header #!/usr/local/bin/bash on order to operate correctly - unless, of course, you modify your system to have BASH as /bin/bash (copying, symlinking)... -- Polytropon From Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ... ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: string split, bash and IFS
Go to: http://www.linuxquestions.org/questions/programming-9/bash-shell-script-split-array-383848/?posted=1#post3270996 And see: IP=1.2.3.4; IP=(${IP//./ }); Rev=${IP[3]}.${IP[2]}.${IP[1]}.${IP[0]} Unga wrote: Hi all How to use bash and IFS to split a string? eg. $string = Name:Surname:10 IFS=: echo $string | read name surname age This does not work for some reason. The read does not create name, surname and age variables. Any idea why? Appreciate your reply. Kind regards Unga ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED] -- View this message in context: http://www.nabble.com/string-split%2C-bash-and-IFS-tp19140697p19340538.html Sent from the freebsd-questions mailing list archive at Nabble.com. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
string split, bash and IFS
Hi all How to use bash and IFS to split a string? eg. $string = Name:Surname:10 IFS=: echo $string | read name surname age This does not work for some reason. The read does not create name, surname and age variables. Any idea why? Appreciate your reply. Kind regards Unga ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
RE: string split, bash and IFS
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Unga Sent: 25 August 2008 10:40 To: freebsd-questions@freebsd.org Subject: string split, bash and IFS How to use bash and IFS to split a string? eg. $string = Name:Surname:10 IFS=: echo $string | read name surname age This does not work for some reason. The read does not create name, surname and age variables. Any idea why? Appreciate your reply. [EMAIL PROTECTED] Unga: I think your problem is that each element of the pipeline runs in a separate process, so has no access to the variables from other processes. You could try something like: echo Name:Surname:10 | ( IFS=: ; read name surname age ; echo $surname) Also, probably a typo - but you're assignment of string in the first line should omit the $ sign. - barry ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
RE: string split, bash and IFS
--- On Mon, 8/25/08, Barry Byrne [EMAIL PROTECTED] wrote: From: Barry Byrne [EMAIL PROTECTED] Subject: RE: string split, bash and IFS To: [EMAIL PROTECTED], freebsd-questions@freebsd.org Date: Monday, August 25, 2008, 7:54 PM -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Unga Sent: 25 August 2008 10:40 To: freebsd-questions@freebsd.org Subject: string split, bash and IFS How to use bash and IFS to split a string? eg. $string = Name:Surname:10 IFS=: echo $string | read name surname age This does not work for some reason. The read does not create name, surname and age variables. Any idea why? Appreciate your reply. [EMAIL PROTECTED] Unga: I think your problem is that each element of the pipeline runs in a separate process, so has no access to the variables from other processes. You could try something like: echo Name:Surname:10 | ( IFS=: ; read name surname age ; echo $surname) Thanks for the reply. Your statement prints the Surname but the variable $surname disappear after that and not available for further processing. I'm referring to two sources for this: 1. Learning the bash shell, 2nd Edition. O'Reilly publishers Where on page 170, under read section The basic syntax is: read var1 var2... This statement takes a line from the standard input and breaks it down into words delimited by any of the characters in the value of the environment variable IFS. The words are assigned to variables var1, var2, etc. 2. http://www.unix.com/shell-programming-scripting/29202-perl-like-split-function-bash.html It looks like it works for others but here on FreeBSD 7.0, bash 3.2.33(0)-release, I find it difficult to get this syntax to work. Also, probably a typo - but you're assignment of string in the first line should omit the $ sign. Yes, sure is a typo. Regards Unga ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
RE: string split, bash and IFS
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Unga Sent: 25 August 2008 15:11 To: Barry Byrne Cc: freebsd-questions@freebsd.org Subject: RE: string split, bash and IFS --- On Mon, 8/25/08, Barry Byrne [EMAIL PROTECTED] wrote: From: Barry Byrne [EMAIL PROTECTED] Subject: RE: string split, bash and IFS To: [EMAIL PROTECTED], freebsd-questions@freebsd.org Date: Monday, August 25, 2008, 7:54 PM -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Unga Sent: 25 August 2008 10:40 To: freebsd-questions@freebsd.org Subject: string split, bash and IFS How to use bash and IFS to split a string? eg. $string = Name:Surname:10 IFS=: echo $string | read name surname age This does not work for some reason. The read does not create name, surname and age variables. Any idea why? Appreciate your reply. [EMAIL PROTECTED] Unga: I think your problem is that each element of the pipeline runs in a separate process, so has no access to the variables from other processes. You could try something like: echo Name:Surname:10 | ( IFS=: ; read name surname age ; echo $surname) Thanks for the reply. Your statement prints the Surname but the variable $surname disappear after that and not available for further processing. I'm referring to two sources for this: 1. Learning the bash shell, 2nd Edition. O'Reilly publishers Where on page 170, under read section The basic syntax is: read var1 var2... This statement takes a line from the standard input and breaks it down into words delimited by any of the characters in the value of the environment variable IFS. The words are assigned to variables var1, var2, etc. 2. http://www.unix.com/shell-programming-scripting/29202-perl-lik e-split-function-bash.html It looks like it works for others but here on FreeBSD 7.0, bash 3.2.33(0)-release, I find it difficult to get this syntax to work. Unga: The variables in the read command are only available in the same shell process as the read command. In my example, that would be the commands within the () brackets, as the () cause a subshell to be spawned. In the most common situation, a read command is going to be part of a shell script, so the variables will be available within the script. Most often, a read command is going to read from standard input, so you'll have something like: - myscript.sh -- #!/usr/local/bin/bash IFS=: while read var1 var2 var3; do echo The values are $var1, $var2 and $var3 done - myscript.sh -- Then call the script: echo apple:orange:banana | ./myscript.sh Or, maybe use the values from a file. ./myscript.sh mylist.txt where mylist.txt contains one or more lines -- mylist.txt -- apple:banana:orange aea:coffee:milk green:blue:red -- mylist.txt -- - Barry ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]