Find the answer to your Linux question:
Results 1 to 4 of 4
Hello, let me start with a little background. We moved to a new server and a lot of our directories changed. When we submit programs to the remote server they ...
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    2

    Searching for directory then symbolic link

    Hello, let me start with a little background. We moved to a new server and a lot of our directories changed. When we submit programs to the remote server they go into a "work" directory. The "work" directory is not in our home directory, which makes it difficult to find our datasets, especially for new users. An ex employee had written a script that lists that dir, greps for your username and then symbolic links that folder into your home folder. We have found issues with it (if you have more than one dir for one) but none of us have the scripting experience to fix it. I ahve tried and tried but have come up short. What is happening is it will find the first two directories, link 1 and 2, but 3 and 4 will be the same dir as 2. Can you guys help out?

    edit: Here is the directory(directories) we need linked..They change each time new code is run. SAS_work and lpima9001 will never change. (i removed some of the listing output)

    [10:59:45 lewisjd]$ ls -la /opt2/saswork | grep lewisjd

    SAS_work7A660003C0D2_lpima9001
    SAS_workDDCA0006F0F6_lpima9001


    Heres the code...

    WSTR="SAS_work"
    WPATH="/opt2/saswork/"

    OWORK=`ls $HOME | grep work`
    MWORK=`ls -la ${WPATH} | grep $USER`

    case $OWORK in
    *"work4"*)
    rm -rf $HOME/work
    rm -rf $HOME/work2
    rm -rf $HOME/work3
    rm -rf $HOME/work4;;
    *"work3"*)
    rm -rf $HOME/work
    rm -rf $HOME/work2
    rm -rf $HOME/work3;;
    *"work2"*)
    rm -rf $HOME/work
    rm -rf $HOME/work2;;
    *"work"*)
    rm -rf $HOME/work;;
    *) echo;;
    esac

    case $MWORK in
    *"$WSTR"*)
    SWORK=${MWORK%%"$WSTR"*}
    IDX=${#SWORK}
    MWORK=${MWORK:$IDX}

    SWORK2=${MWORK%"$WSTR"*}
    IDX2=${#SWORK2}
    MWORK2=${MWORK:$IDX2}

    SWORK3=${MWORK%"$WSTR"*}
    IDX3=${#SWORK3}
    MWORK3=${MWORK:$IDX3}

    SWORK4=${MWORK%"$WSTR"*}
    IDX4=${#SWORK4}
    MWORK4=${MWORK:$IDX4}

    if [ "$SWORK2" == "" ]; then
    ln -sf $WPATH$MWORK $HOME/work
    else
    MWORK=${MWORK:0:30}
    ln -sf $WPATH$MWORK $HOME/work
    ln -sf $WPATH$MWORK2 $HOME/work2
    ln -sf $WPATH$MWORK2 $HOME/work3
    ln -sf $WPATH$MWORK4 $HOME/work4
    fi;;
    *) echo "No work.";;
    esac

  2. #2
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    Give this a try:

    Code:
    #!/bin/bash
    
    WSTR="SAS_work"
    WPATH="/opt2/saswork/"
    
    for TEMP in `ls -d $HOME/work* 2>/dev/null`; do rm -rf $TEMP; done
    
    let COUNT=0
    for TEMP in $WPATH$WSTR*
    do
      TEST=`ls -Ald $TEMP | grep $USER`
      if [ "$TEST" != "" ]; then
        let COUNT=$COUNT+1
        ln -sf $TEMP $HOME/work$COUNT
      fi
    done
    Please understand that 'rm -rf $TEMP' can be very dangerous! This will delete anything in the user's home directory beginning with 'work'...

    If it is not absolutely necessary for the links to start with 'work', I would like to suggest that you use something like 'SAS_work#' -- otherwise, it would be good to add some code that would at least check to make sure the target is a soft link...

    One other thing -- I hope it is OK to use 'work1' instead of 'work'...

  3. #3
    Just Joined!
    Join Date
    Apr 2007
    Posts
    2
    Great, thanks I will ty that. I understand about the rm-rf. I warned the guy before me regarding the usage of it. For the most part this server isn't being used a whole lot so there aren't any files out there to delete...yet. Before I push it to everyone, I will definately NOT be using rm -rf.Thanks for the reply!

  4. #4
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    Quote Originally Posted by lewisjd View Post
    Great, thanks I will ty that. I understand about the rm-rf. I warned the guy before me regarding the usage of it. For the most part this server isn't being used a whole lot so there aren't any files out there to delete...yet. Before I push it to everyone, I will definately NOT be using rm -rf.Thanks for the reply!
    You are very welcome...

    Please feel free to come back for more help if you need it to improve upon your script. (I speak with regard to the forums, not just me -- I believe there are several people here who write BASH scripts, many/most of whom are surely far more experienced with BASH than I am -- of course, I will also be more than happy to help if I can... )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...