LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 03-11-2013, 04:01 AM   #1
dragonix
Member
 
Registered: Nov 2012
Location: Belgium
Distribution: Ubuntu 12.04
Posts: 69
Blog Entries: 6

Rep: Reputation: 1
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
 
Old 03-11-2013, 04:06 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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?
 
Old 03-11-2013, 04:32 AM   #3
dragonix
Member
 
Registered: Nov 2012
Location: Belgium
Distribution: Ubuntu 12.04
Posts: 69
Blog Entries: 6

Original Poster
Rep: Reputation: 1
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 ?
 
Old 03-11-2013, 06:44 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,895

Rep: Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921
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.
 
Old 03-11-2013, 07:22 AM   #5
dragonix
Member
 
Registered: Nov 2012
Location: Belgium
Distribution: Ubuntu 12.04
Posts: 69
Blog Entries: 6

Original Poster
Rep: Reputation: 1
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
 
Old 03-11-2013, 07:45 AM   #6
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
perldoc -f readlink

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

Last edited by linosaurusroot; 03-11-2013 at 08:01 AM.
 
Old 03-12-2013, 04:45 AM   #7
dragonix
Member
 
Registered: Nov 2012
Location: Belgium
Distribution: Ubuntu 12.04
Posts: 69
Blog Entries: 6

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by shivaa View Post
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?
 
Old 03-12-2013, 05:14 AM   #8
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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
 
Old 03-12-2013, 05:15 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,895

Rep: Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921Reputation: 2921
That's what I wrote you - in shell. And here is it in perl: http://www.java2s.com/Code/Perl/File...boliclinks.htm
 
Old 03-12-2013, 05:35 AM   #10
dragonix
Member
 
Registered: Nov 2012
Location: Belgium
Distribution: Ubuntu 12.04
Posts: 69
Blog Entries: 6

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by pan64 View Post
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...
 
Old 03-12-2013, 06:17 AM   #11
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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
}
 
Old 03-12-2013, 07:20 AM   #12
dragonix
Member
 
Registered: Nov 2012
Location: Belgium
Distribution: Ubuntu 12.04
Posts: 69
Blog Entries: 6

Original Poster
Rep: Reputation: 1
oke, cheers!
I will give it a shot and will get back to you!
 
Old 03-12-2013, 10:06 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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
 
  


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
Making a script to automate slackbuilds (perl) mkoco Slackware 5 01-13-2011 03:57 PM
Need help with wild card syntax to semi-automate file conversion using a perl script kmkocot Linux - Newbie 5 12-12-2007 02:55 PM
LXer: Speaking Unix, Part 6: Automate, automate, automate! LXer Syndicated Linux News 0 01-04-2007 10:54 AM
How would I automate this? onewhoknows Linux - Newbie 5 06-22-2004 05:49 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 09:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:13 PM.

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