LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   help using var in script (https://www.linuxquestions.org/questions/linux-newbie-8/help-using-var-in-script-4175440335/)

sniper8752 12-06-2012 03:34 PM

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


David the H. 12-06-2012 03:50 PM

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

sniper8752 12-06-2012 04:15 PM

i don't think my teacher will let me use these :p we never learned them. is there any way to do it the way that I am doing it?

cbtshare 12-06-2012 10:08 PM

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

David the H. 12-07-2012 04:45 AM

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).


All times are GMT -5. The time now is 04:15 AM.