[ale] Does anyone have a definitive way of determining the target of a soft link in bash?

Jim Lynch ale_nospam at fayettedigital.com
Tue Aug 21 15:32:15 EDT 2007


James P. Kinney III wrote:
> On Tue, 2007-08-21 at 11:01 -0400, Jim Lynch wrote:
>   
>> I want to flip flop two directories by pointing a soft link to one and 
>> then the other the next day.  I'm trying to come up with the best way to 
>> tell which of the directories is linked so I can replace the link with 
>> the other one.
>>
>> Something like
>>
>> db1
>> db2
>> current -> db1
>>
>> Then some sort of an if test that says
>>
>> if current is linked to db1
>>     ln -sf db2 current
>> else
>>    ln -sf db1 current
>> fi
>>
>> Any suggestions?
>>     
>
> now=`ls -l current`
> if { "$now" -eq "db1" }
>   then
>     rm current && ln -sf db2 current
>   else
>     if { "$now" -eq "db2"}
>       then
>         rm current && ln -sf db1 current
>     fi
> fi
> new=`ls -l current`
> echo "old stuff $now"
> echo "new stuff $new"
>   
>> Thanks,
>> Jim.
>> _______________________________________________
>> Ale mailing list
>> Ale at ale.org
>> http://www.ale.org/mailman/listinfo/ale
>>
>>     
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Ale mailing list
>> Ale at ale.org
>> http://www.ale.org/mailman/listinfo/ale
Thanks, guys, a combination of readlink and Jim's script did what I want.

For the record:

#!/bin/bash
ls -l current
readlink -f current| grep db2
if [ $? == 0 ]
then
echo  is db2
rm current && ln -sf db1 current
else
echo is db1
rm current && ln -sf db2 current
fi
ls -l current

Jim.



More information about the Ale mailing list