LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Trying to automate using Perl (https://www.linuxquestions.org/questions/linux-newbie-8/trying-to-automate-using-perl-4175453584/)

dragonix 03-11-2013 03:01 AM

Trying to automate using Perl
 
Hi all

I want to create a script in Perl that automates some stuff for me.
But let me start with another question.

When do I do the following:
Code:

$ ls -haltr
total 1.7G
lrwxrwxrwx 1 user user  24 Feb 28 15:04 file.xml -> /path/to/file/file.xml

I see that my 'file.xml' is not physically present, it's linked to another file somewhere else.
The question is, is there a way to 'grep' that part and use it in a script?

Thanks!
More question are on the way ;)

Dragonix

shivaa 03-11-2013 03:06 AM

Yes, you can either user link name or orignial file. A soft link is nothing but just as a shortcut of any file/dir.

Let's know what you want to grep out of this?

dragonix 03-11-2013 03:32 AM

Hi shivaa

thanks for your fast reply.

What I eventually want to do is to copy that file to the local directory and change the name to the original file.

Example:
file is called test.xml
schortcut is like /path/to/file/blablabla.doc

So I want to copy the blablabla.doc file to my local directory, then remove the original test.xml and finally rename the copied blablabla.doc to test.xml (so that I don't have 2 test.xml files).

Is that a bit clear :p?

pan64 03-11-2013 05:44 AM

You can have the following:
Code:

FILE=<local filename>
[[ -L "$FILE" ]] && {
    FILE2=`ls -1ld "$FILE" | awk ' { print $NF } ' `
    rm "$FILE"
    cp "$FILE2" "$FILE"
}

take care about your script, do not delete important files!
you need another script if you have directories also.

dragonix 03-11-2013 06:22 AM

Thanks!
I will give it a shot later on the day.

Yeah, I know so I will make sure to use some checks like 'Are you sure to delete <filename>?'.
But it's still a work in progress and it's the first time that I will be using Perl so a lot of trial and error will be involved ;)

linosaurusroot 03-11-2013 06:45 AM

perldoc -f readlink

$link_target=readlink("test.xml"); # edit correction to scalar type

dragonix 03-12-2013 03:45 AM

Quote:

Originally Posted by shivaa (Post 4908990)
Yes, you can either user link name or orignial file. A soft link is nothing but just as a shortcut of any file/dir.

Let's know what you want to grep out of this?

how precisely can I get the shortcut in a variable?

shivaa 03-12-2013 04:14 AM

Suppose link is like:
Code:

lrwxrwxrwx 1 user user  24 Feb 28 15:04 test.xml -> /path/to/file/file.xml
Then in your script, you can:
Code:

cp -p /path/to/file/file.xml /path/to/local_dir
unlink test.xml
mv -i /path/to/local_dir/file.xml  /path/to/local_dir/test.xml      # Option -i will make mv interactive, so it will ask you before rename


pan64 03-12-2013 04:15 AM

That's what I wrote you - in shell. And here is it in perl: http://www.java2s.com/Code/Perl/File...boliclinks.htm

dragonix 03-12-2013 04:35 AM

Quote:

Originally Posted by pan64 (Post 4909791)
That's what I wrote you - in shell. And here is it in perl: http://www.java2s.com/Code/Perl/File...boliclinks.htm

ow oke, I'm not a pro and it looked a bit chinese too me .. Sorry
I'm still at the level like shivaa posted...

shivaa 03-12-2013 05:17 AM

pan64's code is not much tough. It can simply be understood, if you've tried:
Code:

FILE=<local filename>                # Saving local finename in FILE variable
[[ -L "$FILE" ]] && {                # Testing if file FILE exists and is a link file
    FILE2=`ls -1ld "$FILE" | awk ' { print $NF } ' `  # Saving link name in FILE2 variable
    rm "$FILE"                        # Removing file i.e. link
    rm -i "$FILE"                    # Removing file i.e. link interactively
    cp "$FILE2" "$FILE"              # Renaming link
}


dragonix 03-12-2013 06:20 AM

oke, cheers!
I will give it a shot and will get back to you!

chrism01 03-12-2013 09:06 PM

If you are new to Perl, here are some good links
http://perldoc.perl.org/
http://www.tizag.com/perlT/index.php
http://www.perlmonks.org/?node=Tutorials

Incidentally, the Perl Cookbook is worth its weight in $valuable_mineral :)


All times are GMT -5. The time now is 12:57 AM.