swapping the values of variable!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting swapping the values of variable!
# 1  
Old 12-20-2011
swapping the values of variable!

Hi All,

newbie here, i'm just wondering how can i swap the two values of variables without using the third variable.

Please advise,
Thanks,
# 2  
Old 12-20-2011
One way:
Code:
$ a=5
$ b=6
$ eval $(echo a=$b\;b=$a)
$ echo $a
6
$ echo $b
5
$

This User Gave Thanks to clx For This Post:
# 3  
Old 12-20-2011
Homework?

x = x - y
y = y + x
x = y - x
This User Gave Thanks to balajesuri For This Post:
# 4  
Old 12-20-2011
thx all.. actually exam hehe..

---------- Post updated at 06:49 AM ---------- Previous update was at 06:48 AM ----------

i don't get it.. how it is swap? can you tell me the explanation if it's ok ^^
# 5  
Old 12-20-2011
Just for interest (using files not Enviroment Variables":
Code:
a="abcdef"
b="ghijkl"
echo "Before"
echo "a=${a}"
echo "b=${b}"
echo "${a}" >a.txt
echo "${b}" >b.txt
a="`cat b.txt`"
b="`cat a.txt`"
echo "After"
echo "a=${a}"
echo "b=${b}"

./scriptname
Before
a=abcdef
b=ghijkl
After
a=ghijkl
b=abcdef


Another way - append to $a with a delimiter:
Code:
a="abcdef"
b="ghijkl"
echo "Before"
echo "a=${a}"
echo "b=${b}"
a="${a}:${b}"
b=`echo "${a}"|awk -F: '{print $1}'`
a=`echo "${a}"|awk -F: '{print $2}'`
echo "After"
echo "a=${a}"
echo "b=${b}"

./scriptname
Before
a=abcdef
b=ghijkl
After
a=ghijkl
b=abcdef


Last edited by methyl; 12-20-2011 at 08:08 PM..
These 2 Users Gave Thanks to methyl For This Post:
# 6  
Old 12-22-2011
Initially, x = 3, y = 2
x = x - y # Capture the difference of two variables in x --> x = 3 - 2 = 1
y = y + x # Add the difference to y to get the initial value of x --> y = 2 + 1 = 3
x = y - x # Subtract the difference from y to get the initial value of y --> x = 3 - 1 = 2

Finally, x = 2, y = 3.

But this won't work for variables containing string values. In such cases, you'd have to go for methods as suggested by methyl.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Swapping a string of numbers between higher and lower order values(HEX)

I have this below string in a variable cutString=21222222222222222122222222222222 this string is nothing but hex values depicted as below 21:22:22:22:22:22:22:22:21:22:22:22:22:22:22:22 so what i want to achieve is swap the lower order with higher order values in the... (3 Replies)
Discussion started by: vivek d r
3 Replies

2. Shell Programming and Scripting

Subtracting values from variable

Legends, Please help me in , how do i subtract the variable values listed like below. the first value of orig should be subtracted from first value of prev and so on. san> echo $orig 346 316 340 239 410 107 291 139 128 230 167 147 159 159 172 116 110 260 177 0 177 169 168 186 165 366 195... (15 Replies)
Discussion started by: sdosanjh
15 Replies

3. Shell Programming and Scripting

Changing and swapping the Values in the files

Hi all we have a file ONE like this 12345 98765 67222 74252 76991 90091 and we have one more file TWO like huiiii 67jjjj u988 99999 uj99j 98765 hujg 7yhh ij999 78688 ijo99 74252 Now i want create THREE file which is like huiiii 67jjjj u988 12345 uj99j 98765 hujg 7yhh ij999... (2 Replies)
Discussion started by: polineni
2 Replies

4. UNIX for Dummies Questions & Answers

Variable is not substituting values

Hi All, OS HPUX 11.11 I am using following script to take controlfile backup. I have used SID variable to hold "ffin1" value, which I again subsitute in "'/db/ffin1/home/oraffin1/$SID_$wdate.ctl'" command. Well, after running this, SID variable does not subsittue it's value, while wdate... (6 Replies)
Discussion started by: alok.behria
6 Replies

5. Shell Programming and Scripting

Variable with several values and spaces.

Hello all. I am a newb obviously and a bit stumped on this, so any help gratefully accepted. The script is extracting metadata from individual mp3 files, then (hopefully will be) sorting them into newly-created subdirectories. I have filtered out the relevant metadata and have the album names... (8 Replies)
Discussion started by: spoovy
8 Replies

6. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies

7. Shell Programming and Scripting

getting values from variable in a loop

I have a set of variables: f1="./someFolder" . . f10="./someOtherFolder" And I'm trying to use the following loop for (( i = 0; i <= 10; i++ )) do temp=f$i done I'm trying the get the values from my set of variable to make directories, but I can't seem the get those value... (3 Replies)
Discussion started by: kriuz
3 Replies

8. UNIX for Dummies Questions & Answers

assigning values to a variable

i try to get the year and month values using the below shell script when i enter the script like this #!/usr/bin/ksh dd=`DATE +%Y%M` echo $dd it is showing the error as shown below abc.ksh: DATE: not found any suggestions please (3 Replies)
Discussion started by: trichyselva
3 Replies

9. Shell Programming and Scripting

Substitute variable values

Hi, I am trying to redefine the value of a variable based on another variable value. And I want to read in my variables from a enviroment file in the end -- at least I think so. But 1st here's what I want I need to get working: var_1="11 10 9 8 7 6 5 4 3 2 1" var_2=3 var_3=4 So I want... (12 Replies)
Discussion started by: John Rihn
12 Replies

10. Shell Programming and Scripting

Storing values in variable

Hi All, Here is the description of the problem. I am scripting for database access using k-shell on solaris box dbaccess <databasename> - << EOF 2>/dev/null | awk 'BEGIN {FS=" "}\ {printf "%s", $1}' | grep -v "^$" | \ read cnt1 OUTPUT TO PIPE cat WITHOUT HEADINGS select count(*) from... (1 Reply)
Discussion started by: matrixmadhan
1 Replies
Login or Register to Ask a Question