LinuxQuestions.org
Help answer threads with 0 replies.
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 12-06-2012, 03:34 PM   #1
sniper8752
Member
 
Registered: Oct 2012
Posts: 564

Rep: Reputation: Disabled
help using var in script


I am trying to write a script. I have to list items in a var then count each var, and list them out, indented. what would be the best way of accomplishing this?

here is what i have:

Code:
#For ordinary files
count=$(ls -l $1 | grep '^-' | wc -l)
echo "You have $count ordinary files: "
echo "$(ls -l | grep ^- | awk '{print $9}' | sed 's/./   &/')"

echo

#For directories
count=$(ls -l $1 | grep '^d' | wc -l)
if [ $count != 0 ]
then
	echo "You have $count directorie(s): "
	echo "$(ls -l $1 | grep ^d | awk '{print $4}' | sed 's/./   &/')"
else
	echo "You have no directories."
fi
 
Old 12-06-2012, 03:50 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
First, Do not parse ls for files. Either use simple globbing patterns, or find.

http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html

Next, don't try to store lists of things like files in a single variable. That's what arrays are for.

When processing the output of find, be sure to use null separators for the greatest safety.

What I would do instead is simply run over all the files in turn using a for loop, and test them for the categories you need. To count them, either increment a simple variable, or save each name into a separate array for final processing.

See this page too:
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
 
Old 12-06-2012, 04:15 PM   #3
sniper8752
Member
 
Registered: Oct 2012
Posts: 564

Original Poster
Rep: Reputation: Disabled
i don't think my teacher will let me use these we never learned them. is there any way to do it the way that I am doing it?
 
Old 12-06-2012, 10:08 PM   #4
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Rep: Reputation: 42
well if by better you mean faster and more efficient then, you certainly use the find command to

Quote:
find . \( -type f -maxdepth 1 \)|wc -l
and to get the files in the directory

Quote:
for i in *
do

if [ -f "$i" ]
then

echo "$i"
fi
done

Last edited by cbtshare; 12-06-2012 at 10:22 PM.
 
Old 12-07-2012, 04:45 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
The links I gave, and other related ones at that site, go into a lot of detail about your various options. Reading them should give you some clues about ways to proceed.

The main problem with storing lists in a scalar variable is in reliably reading them back out again. Arrays make this easy by storing each value in a separate memory space, but regular variables don't.

The only way to do it semi-safely (without going into major gymnastic contortions, at least) is to select a separator character that is unlikely to ever be part of a filename, and set your IFS variable to word-split only on it. The null character would be perfect, but unfortunately it's the one character that variables cannot store, and they will be silently discarded if you attempt to do so (actually, it's the fact that they can never appear in a variable or filename that makes them the perfect separator). But tools like find can generate lists using them, and those can be processed dynamically with a while+read loop.

The easiest separator to use is actually the simple newline, but it, like all other characters, depends on the assumption that none of the filenames will ever contain one. If you don't have arrays, that's about all you can do.

How can I find and deal with file names containing newlines, spaces or both?
http://mywiki.wooledge.org/BashFAQ/020

[ Edit: Come to think of it, there is one "array" available to even POSIX shells, and that's the set of input parameters ("$@"). Section 4 of the array link I gave above describes how to use it. ]

See here too for more on how the shell does word-splitting and argument processing. This is a vital concept in scripting, so learn it well!

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


Actually, nothing you've posted so far seems to need storing the filenames themselves in a variable anyway. As I said before, all it requires is looping over the files and testing each one in turn, then printing out the final tallies. Unless there's something to the assignment you haven't mentioned yet.

It would also help to clarify what shell(s) you are using/are allowed to use, as ones like bash and ksh have features not available to POSIX shells that make these things easier and safer to do (arrays are just one example).

Last edited by David the H.; 12-07-2012 at 04:55 AM.
 
  


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
initialize var in shell script dningale Solaris / OpenSolaris 3 08-12-2008 10:51 PM
what does ${var:-:0} mean...in shell script sean04 Programming 2 11-27-2007 09:48 PM
bash shell env var script ejbest Linux - Newbie 4 09-29-2004 10:23 AM
tcsh script: how to see if an env var is defined? BrianK Programming 1 06-07-2004 10:12 PM
bash script+ftp+ls->var g00ral Programming 1 05-05-2004 06:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:32 AM.

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
Open Source Consulting | Domain Registration