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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-09-2008, 06:45 AM
|
#1
|
|
Member
Registered: Aug 2008
Posts: 54
Rep:
|
BASH scripting -- command saved as variable
Basically, I am trying to count how many files of a certain form are in a directory and using that number as a variable I use in a for loop.
The code is as follows:
...
end=${ls 1_* -1 | wc -l};
for ((i=1;i<=$end;i+=1)); do
...
./repeat.sh: line 28: ${ls 1_* -1 | wc -l}: bad substitution
is the error I am getting
What should I do?
|
|
|
|
09-09-2008, 07:57 AM
|
#2
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
In bash the syntax for command substitution is $(...). You used the wrong set of parentheses. See http://www.tldp.org/LDP/abs/html/commandsub.html for details.
|
|
|
|
09-09-2008, 07:59 AM
|
#3
|
|
Member
Registered: Mar 2008
Location: Malta
Distribution: Slackware 14.0
Posts: 435
Rep:
|
end=$(ls 1_* -1 | wc -l);
You might want to consider using find instead of ls. It is much more versatile.
|
|
|
|
09-09-2008, 08:00 AM
|
#4
|
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
Code:
variable_name=$(command) # like this
variable_name=${command} # not like this
|
|
|
|
09-09-2008, 08:06 AM
|
#5
|
|
Member
Registered: Aug 2008
Posts: 54
Original Poster
Rep:
|
Ah yes! I just caught that <--- definitely belong in the newbie forum.
Why would you pick find over ls?
|
|
|
|
09-09-2008, 08:21 AM
|
#6
|
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
Quote:
Originally Posted by bioinformatics_guy
Ah yes! I just caught that <--- definitely belong in the newbie forum.
Why would you pick find over ls?
|
find has a load of options which ls does not. For example, you can select files which have been modified in the last so many days (using the -mtime option). You can build up some quite complicated logic with the -o -a and ! boolean operators.
For example, find can list all the files modified in the last 7 days whose name matches the pattern *.txt, but not starting with "account"
Code:
find . -name '*.txt' -a \! -name 'account*' -a -mtime -7
Have a read of the find manual page to get a better idea of all the possibilities.
|
|
|
|
09-09-2008, 08:45 AM
|
#7
|
|
Member
Registered: Aug 2008
Posts: 54
Original Poster
Rep:
|
Thats definitely a great command. I program mostly with perl and love its regexp but know almost nothing about its equivalent in bash. Whats a good reference book for shell scripting/unix commands? I've heard of Unix Powertools but not sure if thats exactly what I am looking for.
|
|
|
|
09-09-2008, 08:48 AM
|
#8
|
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
If you have the infotext documentation installed, you might want to look at this command:
Here's a nice guide on the net: http://tldp.org/LDP/abs/html/
By the way, there's a nice tool which converts find commands into a perl script which has the same functionality: find2perl.
|
|
|
|
09-09-2008, 09:29 AM
|
#9
|
|
Member
Registered: Aug 2008
Posts: 54
Original Poster
Rep:
|
Great advice from all thank you! I actually found a command I was looking for in the coreutils documenation; sort. I'm sure I'll end up submitting a question on it but this has definitely started me off in the right direction.
|
|
|
|
09-09-2008, 02:35 PM
|
#10
|
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 3,965
|
The Advanced Bash Scripting Guide that matthewg42 mentioned is specially useful if you intend to learn serious bash scripting. Regardless of the name, it's suitable for all levels, starting from beginner.
|
|
|
|
09-09-2008, 08:24 PM
|
#11
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,021
|
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:39 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|