[VGLUG] Problem with shell script for multiple symbolic links

19 views
Skip to first unread message

Niyati Dave

unread,
Feb 29, 2012, 4:36:22 AM2/29/12
to VGLUG
Hello all

I have prepared a shell script for creating multiple symbolic link for
any folder , in a group of multiple directories inside apache
directory.

In details:
There is a list of folders in a file named 'list'
the script is as below, when i enter a directory name the same should
have its symbolic link created in all the folders names mentioend in
the list.

Echo " enter directory name"
read a
for i in 'cat list'
do
ln -s /apache/htdocs/$a /apache/htdocs/$i/$a
done

But this creates a symbolic link between the directory $a with itself
and thus gets created multiple subdirectories with the same name. This
is not desirable.

Please anyone can give idea in the above simple script why this is
happening.

Thanks

Alok Thaker

unread,
Feb 29, 2012, 12:44:29 PM2/29/12
to vg...@googlegroups.com
Hi,

What I see is that for i in `cat list` should be in tild and the logic seems to be correct. Can you explain with example what you want to achieve. 

Thanks & Regards,
Alok 


--
Please read http://www.catb.org/~esr/faqs/smart-questions.html before posting.
You received this message because you are subscribed to the "Vibrant GNU/Linux User Group".
To stop receiving emails from this group, mail to VGLUG+un...@googlegroups.com
To post to this group, send email to VG...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/VGLUG

ElectroMech

unread,
Mar 1, 2012, 3:41:06 AM3/1/12
to vg...@googlegroups.com
Hi,

Echo " enter directory name"
read a
for i in 'cat list'
change the line in to

for i in $(cat list)
 
do
ln -s /apache/htdocs/$a  /apache/htdocs/$i/$a
done

if it will not work execute the script with -x like

sh -x script.sh

and paste the output it may help us.

Thanks and Regards.

--
--
Nilesh Vaghela
(RHCSA RHCE)
ElectroMech Corporation
Redhat Channel Partner and Training Partner
404, Maulik arcade, Above Karnavati Pagarakha Bazar,
Nr. Mansi cross Road,
Satellite Rd, Ahmedabad
25, The Emperor, Fatehgunj, Baroda.
www.electromech.info

Niyati Dave

unread,
Mar 1, 2012, 5:23:50 AM3/1/12
to vg...@googlegroups.com
Hi

In the file name list suppose there is a list of virtual hosts, I want that any new directory in main htdocs should have its symlink in each of the vhosts

cat list

vhost1
vhost2
vhost3
....

The output of the above sample script considering the directory name is newtest.:;


Echo " enter directory name"
read a
for i in 'cat hostlist'
do
ln -s /Apache/htdocs/$a  /Apache/htdocs/$i/$a
done

 sh -x scr.sh

++ cat hostlist
+ for i in '$(cat hostlist)'
+ ln -s /Apache/htdocs/newtest /Apache/htdocs/vhost1/newtest
+ for i in '$(cat hostlist)'
+ ln -s /Apache/htdocs/newtest /htdocs/vhost2/newtest
ln: creating symbolic link `/Apache/htdocs/vhost2/newtest/newtest' to `/Apache/htdocs/newtest': File exists
+ for i in '$(cat hostlist)'
+ ln -s /Apache/htdocs/newtest /Apache/htdocs/vhost3/newtest
ln: creating symbolic link `/Apache/htdocs/vhost3/newtest/newtest' to `/Apache/htdocs/newtest': File exists
+ for i in '$(cat hostlist)'
+ ln -s /Apache/htdocs/newtest /Apache/htdocs/vhost4/newtest
ln: creating symbolic link `/Apache/htdocs/vhost4/newtest/newtest' to `/Apache/htdocs/newtest': File exists
,-----



Here I know that the link already exists and it should giv an error... still it give no error for the first one and then it start giving error with another link created inside itself 'newtest/newtest.

and when I look into
cd /Apache/htdocs/newtest
ls
lrwxrwxrwx 1 oracle oinstall 65 Mar  1 10:09 newtest -> /Apache/htdocs/newtest


 (this is a symbolic link to itself.) This is not desired.. I am not able to figure out from where it gets created.....


ElectroMech

unread,
Mar 1, 2012, 9:49:09 PM3/1/12
to vg...@googlegroups.com
Hi,

I  tried and got following as in reply.

On Thu, Mar 1, 2012 at 3:53 PM, Niyati Dave <niya...@gmail.com> wrote:
Hi

In the file name list suppose there is a list of virtual hosts, I want that any new directory in main htdocs should have its symlink in each of the vhosts

cat list

vhost1
vhost2
vhost3
....

and when I look into
cd /Apache/htdocs/newtest
ls
lrwxrwxrwx 1 oracle oinstall 65 Mar  1 10:09 newtest -> /Apache/htdocs/newtest


 (this is a symbolic link to itself.) This is not desired.. I am not able to figure out from where it gets created.....


[root@desktop100 ~]# cat test.sh
#!/bin/bash
echo " enter directory name"
read a
for i in $(cat hostlist)
do
mkdir /apache/htdocs/$i

ln -s /apache/htdocs/$a  /apache/htdocs/$i/$a
done
[root@desktop100 ~]# cat hostlist
vhost1
vhost2
vhost3
[root@desktop100 ~]#

[root@desktop100 ~]# sh -x test.sh
+ echo ' enter directory name'
 enter directory name
+ read a
example

++ cat hostlist
+ for i in '$(cat hostlist)'
+ mkdir /apache/htdocs/vhost1
+ ln -s /apache/htdocs/example /apache/htdocs/vhost1/example

+ for i in '$(cat hostlist)'
+ mkdir /apache/htdocs/vhost2
+ ln -s /apache/htdocs/example /apache/htdocs/vhost2/example

+ for i in '$(cat hostlist)'
+ mkdir /apache/htdocs/vhost3
+ ln -s /apache/htdocs/example /apache/htdocs/vhost3/example

[root@desktop100 ~]# tree /apache/htdocs/
/apache/htdocs/
├── example
├── vhost1
│   └── example -> /apache/htdocs/example
├── vhost2
│   └── example -> /apache/htdocs/example
└── vhost3
    └── example -> /apache/htdocs/example

7 directories, 0 files

[root@desktop100 ~]# ls -l /apache/htdocs/
total 16
drwxr-xr-x 2 root root 4096 Mar  2 08:11 example
drwxr-xr-x 2 root root 4096 Mar  2 08:12 vhost1
drwxr-xr-x 2 root root 4096 Mar  2 08:12 vhost2
drwxr-xr-x 2 root root 4096 Mar  2 08:12 vhost3
[root@desktop100 ~]# ls -l /apache/htdocs/vhost*
/apache/htdocs/vhost1:
total 0
lrwxrwxrwx 1 root root 22 Mar  2 08:12 example -> /apache/htdocs/example

/apache/htdocs/vhost2:
total 0
lrwxrwxrwx 1 root root 22 Mar  2 08:12 example -> /apache/htdocs/example

/apache/htdocs/vhost3:
total 0
lrwxrwxrwx 1 root root 22 Mar  2 08:12 example -> /apache/htdocs/example
[root@desktop100 ~]#

Please check if this is what you required.
 
Thanks and Regards.

ElectroMech

unread,
Mar 1, 2012, 10:27:16 PM3/1/12
to vg...@googlegroups.com
On Thu, Mar 1, 2012 at 3:53 PM, Niyati Dave <niya...@gmail.com> wrote:
Hi

In the file name list suppose there is a list of virtual hosts, I want that any new directory in main htdocs should have its symlink in each of the vhosts

cat list

vhost1
vhost2
vhost3
....

I think your requirement is mass virtual hosting, this can be done with apache configuration itself.

Please check the following link

http://httpd.apache.org/docs/2.4/vhosts/mass.html

Dynamically Configured Mass Virtual Hosting

Available Languages:  en  |  fr  |  ko  |  tr 

This document describes how to efficiently serve an arbitrary number of virtual hosts with the Apache HTTP Server. A separate document discusses using mod_rewrite to create dynamic mass virtual hosts.

-------------------

Thanks and Regards.
 
--

Niyati Dave

unread,
Mar 2, 2012, 7:16:50 AM3/2/12
to vg...@googlegroups.com
Hi Sir...  thanks for trying that....

can you please try one more thing just to reproduce the problem. (For me it happens every time... not only in second attempt... )

you entered the directory name 'example'. please try to run the same script again with the same dir name 'example'... it should give error for all the
"ln -s ..  " statements...
but also it creates the symbolic link with itself  like if you look into
cd /apache/htdocs/example
ls -l
example -> /apache/htdocs/example

There will be a symbolic link created with itself..  This is the actual problem.... I want to get rid of .

Thanks
Niyati



ElectroMech

unread,
Mar 2, 2012, 7:28:25 AM3/2/12
to vg...@googlegroups.com
Hi,

Let me check.

Niyati Dave

unread,
Mar 5, 2012, 7:45:58 AM3/5/12
to vg...@googlegroups.com
Hi Sir,
Did you check?

Hello all,
can anyone else give some idea on this... ?

Deep Patel

unread,
Mar 6, 2012, 4:41:53 AM3/6/12
to VGLUG
Dear Niyati,

Greetings from CodeLogic !!

Here's your solution !!

[root@desktop8 ~]# cat test.sh
#!/bin/bash
echo "Enter Directory Name"
# Get the input from the user
read -p "Directory Name: " DIR
# To check whether the directory exists
if [ -d /apache/htdocs/$DIR ];
then
echo "$DIR directory exists"
else
mkdir -p /apache/htdocs/$DIR
fi
# Now we start our loop to create symbolic links !!
for link in $(cat /root/hostlist)
do
#first we check whether the link exists !!
if [ -L /apache/htdocs/$link/$DIR ];
then
echo "$DIR link exists"
else
ln -s /apache/htdocs/$DIR /apache/htdocs/$link/$DIR
fi
done

Deep Patel ( RHC{X,I,SS,VA,DS}, CCNA, CE|H )
CodeLogic
C/3, Sandeep Appartments,
Opp Windsor Plaza, R.C.Dutt Road,
Alkapuri, Vadodara 390007
www.codelogic.co.in
M: +91 9601009898

On Mar 5, 5:45 pm, Niyati Dave <niyati...@gmail.com> wrote:
> Hi Sir,
> Did you check?
>
> Hello all,
> can anyone else give some idea on this... ?
>
> On Fri, Mar 2, 2012 at 5:58 PM, ElectroMech <electrom...@electromech.info>wrote:
>
> > Hi,
>
> > Let me check.
>
> > On Fri, Mar 2, 2012 at 5:46 PM, Niyati Dave <niyati...@gmail.com> wrote:
>
> >> Hi Sir...  thanks for trying that....
>
> >> can you please try one more thing just to reproduce the problem. (For me
> >> it happens every time... not only in second attempt... )
>
> >> you entered the directory name 'example'. please try to run the same
> >> script again with the same dir name 'example'... it should give error for
> >> all the
> >> "ln -s ..  " statements...
> >> but also it creates the symbolic link with itself  like if you look into
> >> cd /apache/htdocs/example
> >> ls -l
> >> example -> /apache/htdocs/example
>
> >> There will be a symbolic link created with itself..  This is the actual
> >> problem.... I want to get rid of .
>
> >> Thanks
> >> Niyati
>
> >> On Fri, Mar 2, 2012 at 8:57 AM, ElectroMech <electrom...@electromech.info
> >> > wrote:
>
> >>> On Thu, Mar 1, 2012 at 3:53 PM, Niyati Dave <niyati...@gmail.com> wrote:
>
> >>>> Hi
>
> >>>>> In the file name list suppose there is a list of virtual hosts, I want
> >>>>> that any new directory in main htdocs should have its symlink in each of
> >>>>> the vhosts
>
> >>>>> cat list
>
> >>>>> vhost1
> >>>>> vhost2
> >>>>> vhost3
> >>>>> ....
>
> >>> I think your requirement is mass virtual hosting, this can be done with
> >>> apache configuration itself.
>
> >>> Please check the following link
>
> >>>http://httpd.apache.org/docs/2.4/vhosts/mass.html
>
> >>> Dynamically Configured Mass Virtual Hosting
>
> >>> Available Languages:  en <http://httpd.apache.org/docs/2.4/en/vhosts/mass.html>|
> >>>  fr  <http://httpd.apache.org/docs/2.4/fr/vhosts/mass.html> |  ko <http://httpd.apache.org/docs/2.4/ko/vhosts/mass.html>|
> >>>  tr  <http://httpd.apache.org/docs/2.4/tr/vhosts/mass.html>
>
> >>> This document describes how to efficiently serve an arbitrary number of
> >>> virtual hosts with the Apache HTTP Server. A separate document<http://httpd.apache.org/docs/2.4/rewrite/vhosts.html>discusses using
> >>> mod_rewrite <http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html> to

Niyati Dave

unread,
Mar 6, 2012, 7:38:07 AM3/6/12
to vg...@googlegroups.com
Hello Deep sir
Thanks.. it works

Appreciate this

Regards
Niyati

>
>

--
Please read http://www.catb.org/~esr/faqs/smart-questions.html before posting.
Reply all
Reply to author
Forward
0 new messages