Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"
The ONE Campaign to make poverty history

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents

· Introduction to Operating Systems
· Linux Basics
· Working with the System
· Shells and Utilities
· Editing Files
· Basic Administration
· The Operating System
· The X Windowing System
· The Computer Itself
· Networking
· System Monitoring
· Solving Problems
· Security
· Installing and Upgrading
· Linux and Windows

Glossary
MoreInfo
Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
FAQ
Copyright Info
Terms of Use
Privacy Info
Disclaimer
WorkBoard
Thanks
Donations
Advertising
Masthead / Impressum
Your Account

Communication
Feedback
Forums
Private Messages
Surveys

Features
HOWTOs
News Archive
Submit News
Topics
User Articles
Web Links

Google
Google


The Web
linux-tutorial.info

Who's Online
There are currently, 88 guest(s) and 0 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here

  
Linux Knowledge Base and Tutorial: Forums



Linux Tutorial :: View topic - help with shell script
 Forum FAQForum FAQ   SearchSearch   UsergroupsUsergroups   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

help with shell script

 
Post new topic   Reply to topic    Linux Tutorial Forum Index -> Commands and Utilities
View previous topic :: View next topic  
Author Message
CBarcley
Newbie


Joined: Aug 28, 2005
Posts: 3

PostPosted: Sun Aug 28, 2005 6:52 pm    Post subject: help with shell script Reply with quote

I'd like to write a shell script which extracts every rar file in a directory.
I extract a rar file with 'unrar x file.rar'
I get all rar files with 'ls -A | grep ".rar"'
But how do I put this things together?

Thanks,
Calvin
Back to top
View user's profile Send private message
jimmo
Administrator


Joined: Jul 27, 2002
Posts: 309
Location: Coburg, Germany

PostPosted: Mon Aug 29, 2005 12:35 pm    Post subject: Reply with quote

This ought to do it:

ls -1A *.rar | while read FILE
do
echo "Doing file $FILE..."
unrar x $FILE
done

You can avoid the grep simply specifying the files you want with an a wild card. The -1 says to list a single column. The -A is not necessary unless you are looking for files which start with a dot.

The ls is piped into the "while read" which reads a line at a time and assigns this to the variable $FILE. When you assign it, there is no $ in front. Then we have do-done block which is everything that you want to do during the loop. The variable name is case sensitive.

I would test it first with this line:

echo "unrar x $FILE"

Instead of calling unrar directly. That way you can see what unrar command would be called before you actually run.

Let us know if that solves your problem or if you have any more questions.
Back to top
View user's profile Send private message
CBarcley
Newbie


Joined: Aug 28, 2005
Posts: 3

PostPosted: Mon Aug 29, 2005 2:08 pm    Post subject: Something unexpected Reply with quote

Thank you very much for your reply jimmo

I tried your script and I get:
[code:1]
cbarcley@sun home # ./shell_test.sh
Doing file -rw-r--r-- 1 cbarcley users 310358016 Aug 22 09:44 Some File with Spaces.rar...

UNRAR 3.40 freeware Copyright (c) 1993-2004 Alexander Roshal

Cannot open 1.rar
No files to extract
[/code:1]

I tried
ls *.rar instead of ls -lA *.rar
[code:1]
Doing file Some File with Spaces.rar...

UNRAR 3.40 freeware Copyright (c) 1993-2004 Alexander Roshal

Cannot open Some.rar
No files to extract
[/code:1]
Good, if I have spaces in the filename unrar gets the wrong filename. I tried to rename the file to Some_File_with_Spaces.rar
[code:1]
Doing file Some_File_with_Spaces.rar...

UNRAR 3.40 freeware Copyright (c) 1993-2004 Alexander Roshal


Extracting Some_File_with_Spaces.rar

Creating dir OK
Extracting dir/filename.txt 0%
User break
[/code:1]

How do I pass the correct filename to unrar like:
unrar x Some\ File\ with\ Spaces.rar

Thank you
Calvin
Back to top
View user's profile Send private message
jimmo
Administrator


Joined: Jul 27, 2002
Posts: 309
Location: Coburg, Germany

PostPosted: Mon Aug 29, 2005 10:13 pm    Post subject: Reply with quote

The problem is that the shell is passing the individual components of the files as separate arguments to unrar. Try putting double-quotes around the file name, like this:

unrar x "$FILE"

By the way, I know it is often hard to tell, but you obviously used an "ell" when you created the script and not a "one". There is a big difference between "ls -lA *." and "ls -1A*.".

Regards,

jimmo
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Linux Tutorial Forum Index -> Commands and Utilities All times are GMT
Page 1 of 1

 

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001-2007 phpBB Group
  

Looking for a "printer friendly" version?


Login
Nickname

Password

Security Code
Security Code
Type Security Code


Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!


Amazon Wish List

Did You Know?
The Linux Tutorial can use your help.


Friends



Tell a Friend About Us

Bookmark and Share



Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.05 Seconds