LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-09-2008, 06:45 AM   #1
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Rep: Reputation: 15
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?
 
Old 09-09-2008, 07:57 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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.
 
Old 09-09-2008, 07:59 AM   #3
ChrisAbela
Member
 
Registered: Mar 2008
Location: Malta
Distribution: Slackware 14.1
Posts: 467

Rep: Reputation: 87
end=$(ls 1_* -1 | wc -l);

You might want to consider using find instead of ls. It is much more versatile.
 
Old 09-09-2008, 08:00 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Code:
variable_name=$(command)   # like this
variable_name=${command}   # not like this
 
Old 09-09-2008, 08:06 AM   #5
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
Ah yes! I just caught that <--- definitely belong in the newbie forum.

Why would you pick find over ls?
 
Old 09-09-2008, 08:21 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Quote:
Originally Posted by bioinformatics_guy View Post
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.
 
Old 09-09-2008, 08:45 AM   #7
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
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.
 
Old 09-09-2008, 08:48 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
If you have the infotext documentation installed, you might want to look at this command:
Code:
info coreutils
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.
 
Old 09-09-2008, 09:29 AM   #9
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
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.
 
Old 09-09-2008, 02:35 PM   #10
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
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.
 
Old 09-09-2008, 08:24 PM   #11
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,226

Rep: Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022
There's also
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://rute.2038bug.com/index.html.gz
 
  


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
Bash Shell Scripting Dynamic Variable naming question ZuG Programming 2 02-07-2007 02:39 PM
bash scripting : printing variable with the number within $i amdGTintel Programming 2 01-18-2007 01:30 AM
Bash scripting: how can I reference a variable? frankie_DJ Programming 4 10-06-2006 05:13 AM
simple bash variable command itz2000 Programming 6 06-19-2006 08:24 PM
Bash command scripting otisthegbs Linux - General 6 07-06-2004 12:06 AM


All times are GMT -5. The time now is 02:19 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration