#!/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
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:
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.
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
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
```text
and
```
tags around your posted code to keep the format nice:
#!/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
surround the variable names with curley braces
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
You are assigning to pwd1, but your are using pwd.
Really thanks alot to Chubler_XL , SriniShoo and RudiC
it's working..
thanks............