LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 08-04-2004, 11:15 AM   #1
neorion
LQ Newbie
 
Registered: Aug 2004
Posts: 13

Rep: Reputation: 0
splitting variables In Bash Programming


Hello,
I am a writing a program which has a variable defined to a list of numbers seperated by spaces. I wish to make each number in that initial variable each their own variables. The ammounts of numbers may not always be the same so sometimes there might be 10 numbers in the initial string I have experimented with awk but i dont even know if i am going in the right direction with that . Any help is really appreciated... Thanks
-NeorioN
 
Old 08-04-2004, 03:44 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Try using arrays -- here is a ksh example:

Code:
#!/bin/ksh  
# arrays
myarray=''               
# build an array
to_array ()
{
  unset myarray
  set -A myarray `echo $*`
}                 

# display an array                                     
disp_array () {
let i=0
while (( i < ${#myarray[*]} )) ; do
   echo "array element $i = ${myarray[i]} "
   let i=i+1
done
}                  

#demo of arrays

a="10 13 12 9"
b="1 2 3 4 5 6 7 8 9 10"  
to_array "$a" 
echo "array created from: $a"
disp_array
to_array "$b"               
echo "array created from: $b"
disp_array

exit
 
Old 08-04-2004, 05:28 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
In bash it's quite easy. Just execute the "set" command with you list-variable. You can then acces them with $1, $2, $3, ... or iterate through them using the "shift" command. Of course the original contents of $1, $2,... (the script arguments) are lost. But you can save them before in seperate var's, or in just one var, e.g: SAVED_ARGS=$* and then restore the arguments with "set $SAVED_ARGS" later on.

Demo:
Code:
#!/bin/bash

# We are going to lose the arguments passed to the this
# script. So store them in other var's before.

LIST="3 76 23 22 345 22 1 9 8 6  3  3     3 2 2  1"

# Set the items in the postitional parameters ($1, $2, $3, ...)
# If the list is comma-seperated, you need to set the IFS var to a comma
# before the set command. E.g:  IFS=,

set $LIST

# Now the original argument to the script have disappeared.
# They have been replaced by the items from $LIST

echo $1
echo $2
echo $3
echo $4
echo $5
echo $6
echo $7
echo $8
echo $9
echo ${10}  # When you need two digits, curly braces are needed.
echo ${11}
echo ${12}
echo ${13}
echo ${14}
echo ${15}
echo ${16}
echo ${17}

echo ==============================

# Or you can iterate through them, with shift.
# After a shift:
#   $1 becomes $2
#   $2 becomes $3
#   ...and so on.  ($0 is not touched by shift)

echo $1
while shift ; do
	echo $1
done
 
  


Reply



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
variables in bash haddad Linux - General 6 09-22-2004 05:29 PM
Bash Script String Splitting MurrayL Linux - Newbie 1 09-21-2004 03:20 AM
New to bin/bash variables programming help jobesd Programming 3 03-24-2003 05:02 AM
Variables in bash tcaptain Programming 1 03-03-2003 02:07 PM
Bash variables pk21 Programming 2 01-09-2003 03:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:07 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration