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 03-31-2013, 10:15 AM   #1
SpaceBoB
LQ Newbie
 
Registered: Mar 2013
Posts: 10

Rep: Reputation: Disabled
What is wrong with my shell script?


Hi guys!
I am trying to write a shell script which will write out the number of files in the given directory.
I have done this:

Code:
cat >nf
echo "There are `ls -l $1|grep ^-|wc -l` files in this directory."
CTRL+D
Now when I use this:
Code:
sh <nf
It writes out the number of files in the active directory. But when I try to give him a new directory to look at ($1), like:
Code:
sh <nf Files/
It won't write anything out, not even errors.
What am I doing wrong? Thank you in advance!
 
Old 03-31-2013, 10:27 AM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
Try to run your script without using redirection to start it:
Code:
sh nf Files/
 
Old 03-31-2013, 10:28 AM   #3
SpaceBoB
LQ Newbie
 
Registered: Mar 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks it works that way. Is there a way to leave out the sh and just write this?

Quote:
nf Files/
 
Old 03-31-2013, 10:40 AM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
Yes, just give your script a proper shebang, so that the first line looks like this
Code:
#!/bin/sh
You can use other interpreters instead of sh in that line, for example /usr/bin/zsh, /bin/bash or even constructs like
Code:
/usr/bin/env python
 
Old 03-31-2013, 10:46 AM   #5
SpaceBoB
LQ Newbie
 
Registered: Mar 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks for your help, I appreciate it.
 
Old 03-31-2013, 01:18 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
You're really going about this the wrong way. parsing ls for files or metadata is not wise.

To count how many "files" are in the directory, all you really need is a bash array.

Code:
#!/bin/bash

if [[ -d $1 ]]; then
    files=( "$1/"* )
else
    echo "Argument is not a directory"
    exit 1
fi


echo "There are ${#files} files in directory $1"

exit 0
Note though that this actually counts all entries in the directory, including subdirectories and special files. To count regular files only, a testing loop should be added.

Code:
#!/bin/bash

if [[ ! -d $1 ]]; then
    echo "Argument is not a directory"
    exit 1
fi

shopt -s dotglob        #turn on hidden file globbing

for fname in "$1/"* ; do
    if [[ -f "$fname" ]]; then
        files=( "$fname" )
    fi
done

echo "There are ${#files} files in directory $1"
Actually, there's no need for the array either since all you're doing is counting entries. A simple "files=$(( files + 1 ))" counter variable would be a fully portable alternative.


And if you need recursive counting, then you should switch to find. See here for more:

How can I check whether a directory is empty or not? How do I check for any *.mpg files, or count how many there are?
http://mywiki.wooledge.org/BashFAQ/004

Last edited by David the H.; 03-31-2013 at 01:19 PM. Reason: minor text corrections
 
Old 04-10-2013, 04:33 PM   #7
SpaceBoB
LQ Newbie
 
Registered: Mar 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thank you! We are studying this at school at the way I have written it. Are there any good websites or e-books where I could learn the "proper" way which you have written down?
 
Old 04-10-2013, 07:50 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,254

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
The wiki http://mywiki.wooledge.org/ as referenced by David the H. contains bash guides.
See also
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
1 members found this post helpful.
  


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
What is wrong with my 3 line shell script? QueenZ Linux - Newbie 4 08-12-2011 09:00 AM
what is wrong with my shell script ??? HuMan-BiEnG Programming 11 06-15-2010 05:08 AM
What is wrong with this basic shell script? Black.Sands Linux - Newbie 3 03-29-2010 04:13 AM
Could someone tell me what's wrong with my shell script please? RowanB Programming 4 11-12-2004 12:17 AM


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