Find the answer to your Linux question:
Results 1 to 4 of 4
There are a couple of tasks I would like to accomplish, but I am not sure which approach would be best. I could either use shell scripting or Perl. I ...
  1. #1
    Just Joined!
    Join Date
    Feb 2008
    Posts
    5

    Right tool for the job?

    There are a couple of tasks I would like to accomplish, but I am not sure which approach would be best. I could either use shell scripting or Perl. I should state that regardless of the method, I will have to invest considerable time in learning it. I am a complete novice in each.

    I will describe the tasks I'd like to do, and perhaps you can tell me which would be better suited, shell scripting or Perl?

    Task 1:
    I have "SourceDirectory" which is a tree, and I need to parse through it recursively, checking for the existence of files "*.foo". For each "somefile.foo", then "somefile.bar" should also exist. If it does not, I need to write a line in a report text file, ideally containing the full path and not just the filename.

    Task 2:
    Once the above report is complete, and I have corrected the errors manually, I will move onto this task. Once again, I will traverse "SourceDirectory" recursively. This time I will move files "*.bar" out to "DestinationDirectory", preserving the directory structure. So Source and Destination trees should be the same, with Source containing *.foo's and Destination containing *.bar's.

    I have looked into shell scripting and Perl, briefly. The thing about shell scripting is there are so many different commands, and you have to learn the options of each. Then you have to learn how to direct the outputs between them correctly. Just looking at an example of Sed syntax had me quaking in my boots.

    Perl seems daunting as well, however at least I could perhaps get a book and hopefully have all the commands explained in that one location, rather than having to chase down countless man pages to learn syntax.

    Your thoughts? Thank you for reading!

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    If I had to do this, I would use the find program, and a Bash script to help with that.

    find is very good for looking in a directory tree for certain files. And you can then have it run some command on each file it finds. I would write a Bash script to check for the existence of the .bar file, and maybe one for the moving of the files. Both of these could be done without an explicit script, but that might help clean things up.

    Now then, on to the meat of the matter:

    Bash and Perl are _very_ different beasts. Bash scripting is all about automating shell tasks. You can try and introduce new functionality, but Bash was not really designed for this.

    Perl can be used for automation of tasks (and indeed, it's very good at this, and particularly at text parsing), but Perl is generally overkill for most tasks that Bash can easily accomplish. This is because Perl is a full-on programming language, while Bash is really more of an automation language.

    Having said that, I suggest you learn Bash first, as it can help you learn the shell, and introduce you to a lot of subjects that make other parts of scripting easier. However, if you're interested in programming and scripting, I _highly_ suggest learning Perl.

    Let's start with Bash. I recommend these two resources. The first is a beginner's guide to Bash, while the second is the best reference I have ever found:
    http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
    http://www.tldp.org/LDP/abs/html/

    If you're interested in learning Perl, I suggest you check out the aptly-named "Learning Perl" book that is published by O'Reilly. I used it to teach myself Perl, and it is fantastic. "Programming Perl" (also from O'Reilly) is essentially the bible of Perl programming, and once you've learned the basics, you could move on to it. It's a fabulous guide and reference.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by pedx1ng View Post
    I have looked into shell scripting and Perl, briefly. The thing about shell scripting is there are so many different commands, and you have to learn the options of each. Then you have to learn how to direct the outputs between them correctly. Just looking at an example of Sed syntax had me quaking in my boots.

    Perl seems daunting as well, however at least I could perhaps get a book and hopefully have all the commands explained in that one location, rather than having to chase down countless man pages to learn syntax.

    Your thoughts? Thank you for reading!
    You can try Python. Its easy to learn and can do basically anything the shell, perl etc can.

  4. #4
    Linux Engineer
    Join Date
    Nov 2004
    Location
    Ft. Polk, LA
    Posts
    796
    This is exactly the kind of task that bash is the best at. You don't need sed or awk or anything, just regular bash commands will accomplish it nicely and in few lines.

Posting Permissions

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