LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-04-2007, 02:57 PM   #1
ZuG
LQ Newbie
 
Registered: Jun 2003
Posts: 6

Rep: Reputation: 0
stuck on a shell script


Hello,

I'm getting frustrated attempting to write this shell script.

What it needs to do is parse all the symbolic links in the /home/sites directory and change the links so instead of being linked to /home/sites/some/path/ it's linked to ./some/path.

Any ideas?
 
Old 06-04-2007, 03:17 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
What books, etc. are you using to learn shell scripting?

The way to develop a script is to first test the various commands to make sure they do what you want. The first thing is to find all the lines in "ls -l" that begin with "l"
ls -l | grep "^l"

AFAIK, you cannot modify the target of a link--I think you have to delete them and make new ones. One straightforward way would be to store the link name & target in an array. Then read the array and process the target to create the new name. Finally, issue ln -s for each element of the array.

To grab the link names, etc. you can use awk:
Here is a sample output from the command above:
lrwxrwxrwx 1 mherring users 3 2007-06-04 12:03 das -> DAS
The link name and the target are simply the 8th and 10th fields (make sure awk is using "space" as the delimiter)

I hope this at least gets you started in the right direction.
 
Old 06-04-2007, 03:17 PM   #3
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
How does your script look so far?
 
Old 06-04-2007, 03:22 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
PS:

Instead of modifying existing links, how about simply deleting all of them and creating new ones by looping thru the TARGETs?
 
Old 06-04-2007, 05:34 PM   #5
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
You will need to remove the link and recreate it. Moving a link only moves the source, not the target of the link. Hope this helps.

#!/bin/sh
# Use at your own risk. Feel free to modify and share.

# used /etc to test the script since I don't have a "/home/sites" directory
SRCDIR=/etc
#SRCDIR=/home/sites

# save the current directory so we can come back.
CURRENT_DIR=`pwd`

# change to the directory containing the links
cd $SRCDIR

# List any links in the directory, get their target and source, and then see if
# the target is is in the source directory (so we can make it relative)
for FILE in `ls -la $SRCDIR | grep "^l" | awk '{print $10 ":" $8}'| grep "^$SRCDIR"`; do
TARGET=`echo $FILE | awk -F: '{print $1}'`
SRC=`echo $FILE | awk -F: '{print $2}'`

echo $SRC '->' $TARGET

# I'm not exactly sure how you want to change TARGET to NEW_TARGET
# so I'm guessing you simply want to remove the source directory from the
# beginning of the path.

sep=$'\001' # Use Control-a as separator, it will never be in a dir name
replace=.

NEW_TARGET=`echo $TARGET | sed "s${sep}^${SRCDIR}${sep}$replace${sep}"`

echo $SRC '->' $NEW_TARGET

# commenting out the dangerous stuff until you test it.
# rm $SRC
# ln -s $NEW_TARGET $SRC
done

cd $CURRENT_DIR

Last edited by forrestt; 06-04-2007 at 05:39 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 07:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 03:34 PM
Stuck in my Shell!!! TracyLynnZ Linux - Networking 6 01-25-2006 05:41 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM
Stuck at login (shell doesn't load) clee01 Linux - General 2 02-15-2001 05:50 PM


All times are GMT -5. The time now is 03:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration