Unable to swap values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to swap values
# 1  
Old 07-01-2014
Unable to swap values


#!/bin/bash
pwd1=`cat 1.ini | grep -w PWD | cut -d'=' -f2` //value is /home/java/Desktop/sh_h
echo the value is `pwd`

pwd2=`cat 2.ini | grep -w PWD | cut -d'=' -f2` //value is /home/unix/Desktop/sh_h
echo the value is $pwd2

if [[ $pwd == $pwd2 ]]
then
echo both are same
else
pwd2=$pwd /* here i want to asign pwd2 value to pwd but its not happening. pls help men

fi

Last edited by anandpasunoori; 07-02-2014 at 10:46 PM.. Reason: added [/code] tags
# 2  
Old 07-01-2014
This is a classic programming exercise given to most 1st year IT students.

The answer is to use a temporary variable to keep the original pwd2 value:

Code:
if [[ "$pwd" == "$pwd2" ]]
then
    echo both are same
else
    temp_pwd=$pwd2
    pwd2=$pwd
    pwd=$temp_pwd
fi

Also note the quotes in the if statement these protect against errors that will arise if your PWD value contains spaces.
# 3  
Old 07-01-2014
really thanks alot for the reply. but i dont see the swap values in between pwd and pwd2, the value of pwd2 is copied to temo_pwd only. i see the same value in pwd.

pls help me on this.

---------- Post updated at 09:44 PM ---------- Previous update was at 09:41 PM ----------

this is the code i tired..

#!/bin/bash
pwd1=`cat 1.ini | grep -w PWD | cut -d'=' -f2`
echo the value is `pwd`

pwd2=`cat 2.ini | grep -w PWD | cut -d'=' -f2`
echo the value is $pwd2

if [[ "$pwd" == "$pwd2" ]]
then
echo both are same
else
temp_pwd=$pwd2
pwd2=$pwd
pwd=$temp_pwd

echo ------------------
echo the value is pwd is $pwd
echo the value is pwd2 is $pwd2
echo the value is temp_pwd is $temp_pwd
fi
# 4  
Old 07-02-2014
You could be deceiving yourself with the first echo statement which is printing your current directory using the pwd executable not the value of $pwd

Remember to use [code] and [/code] tags around your posted code to keep the format nice:

Code:
#!/bin/bash
pwd=`cat 1.ini | grep -w PWD | cut -d'=' -f2` 
echo "the value is $pwd"

pwd2=`cat 2.ini | grep -w PWD | cut -d'=' -f2` 
echo "the value of pwd2 is $pwd2"

if [[ "$pwd" == "$pwd2" ]]
then
  echo both are same
else
  temp_pwd=$pwd2
  pwd2=$pwd
  pwd=$temp_pwd

  echo ------------------
  echo the value is pwd is $pwd
  echo the value is pwd2 is $pwd2
  echo the value is temp_pwd is $temp_pwd
fi

# 5  
Old 07-02-2014
surround the variable names with curley braces
Code:
if [[ "${pwd}" == "${pwd2}" ]]
then
echo both are same
else
temp_pwd=${pwd2}
pwd2=${pwd}
pwd=${temp_pwd}
echo ------------------
echo the value is pwd is ${pwd}
echo the value is pwd2 is ${pwd2}
echo the value is temp_pwd is ${temp_pwd}
fi

# 6  
Old 07-02-2014
You are assigning to pwd1, but your are using pwd.
# 7  
Old 07-02-2014
Really thanks alot to Chubler_XL , SriniShoo and RudiC

it's working..

thanks............
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to swap the values in array using for loop?

array=( 8 5 6 2 3 4 7 1 9 0 ) for i in "${array}" do echo $i done # i need the output like this by swapping of array values 0 9 1 7 4 3 2 6 5 8 (7 Replies)
Discussion started by: Meeran Rizvi
7 Replies

2. Solaris

Explain the output of swap -s and swap -l

Hi Solaris Folks :), I need to calculate the swap usage on solaris server, please let me understand the output of below swap -s and swap -l commands. $swap -s total: 1774912k bytes allocated + 240616k reserved = 2015528k used, 14542512k available $swap -l swapfile dev swaplo... (6 Replies)
Discussion started by: seenuvasan1985
6 Replies

3. Shell Programming and Scripting

Unable to read assign values to two variables in while loop

I am trying to read a input file which has two columns separated by space Input file server1 server2 server3 server4 server5 server6 When i execute the below while code it reads line by line and a and b variables are able to successfully fetch the values while read a b do echo "$a" echo... (5 Replies)
Discussion started by: chidori
5 Replies

4. Shell Programming and Scripting

Converting odd values to even values(or vice-versa) located in a column

Hello All, I have a below data in a .csv file where all rows where col1 is A, col2 is odd numbers, similarly even numbers for all rows where col1 is B. Note that my data has some other columns(not shown here) too (around 100) after col2. Tool,Data A,1 A,3 A,5 .... so on B,2 B,4 .... ... (4 Replies)
Discussion started by: ks_reddy
4 Replies

5. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

6. Shell Programming and Scripting

unable to return multilple values in perl

hello friends, i have written one perl script.Which opens a file and search for some parameter's value and gets the status of these parameters. but while i am trying to return these value always i am getting false. Can any one please help me.. here is that function: =======================... (5 Replies)
Discussion started by: harpal singh
5 Replies

7. HP-UX

Swap device file and swap sapce

Hi I have an integrity machine rx7620 and rx8640 running hp-ux 11.31. I'm planning to fine tune the system: - I would like to know when does the memory swap space spill over to the device swap space? - And how much % of memory swap utilization should be specified (swap space device... (6 Replies)
Discussion started by: lamoul
6 Replies

8. Red Hat

swap not defined as swap

free -m : 1023 total swap space created default partition /dev/sdb1 50M using fdisk. i did write the changes. #mkswap /dev/sdb1 #swapon /dev/sdb1 free -m : 1078 total swap space this shows that the swap is on Question : i did not change the type LINUX SWAP (82) in fdisk. so why is... (5 Replies)
Discussion started by: dplinux
5 Replies

9. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

10. Solaris

Swap config - Mirror swap or not?

Hello and thanks in advance. I have a Sun box with raid 1 on the O/S disks using solaris svm. I want to unmirror my swap partition, and add the slice on the second disk as an additional swap device. This would give me twice as much swap space. I have been warned not to do this by some... (3 Replies)
Discussion started by: BG_JrAdmin
3 Replies
Login or Register to Ask a Question