[SOLVED] shell scripts: storing result of a command into a variable
Linux - NewbieThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
shell scripts: storing result of a command into a variable
hi there guys, I have a question to ask.
Let's say I typed in the command: find $1 -type d | wc -l
to find the number of directories. This command, from what I discovered in previous threads, gives 1 extra directory. That's not what I want. I want to do subtract 1 from this result and output it to my terminal.
Let's assume I have a shell script called "checkD."
An example would be:
$ checkD ~/direct/here
5
// I want it to output 4, but how would I do that?
I would assume I store that result into a variable, such as
num="find $1 -type d | wc -l" , or something along the lines.
What would be the correct way to modify that '5' into a 4 in my bash shell script?
Thank you for the prompt responses, divayashree and evo2.
evo2's code worked just fine. It was exactly what I needed.
However, divayashree's did not work. It looks a bit cleaner though. I got the error "expr: non-numeric argument"
Is there a way to make it work with expr?
Just out of curiosity, what is this extra directory you mentioned? Maybe there is a way to exclude it from the result using the proper predicates of the find command. For example, if you want to find the sub-directories contained in the main directory and exclude the main directory itself from the output:
The extra directory is the current directory. It includes itself in the count when I do:
Code:
find $i -type d | wc -l
colucix, that line of code definitely prints out all sub-directories, but I'm trying to get the number of folders, where the argument comes from command line arguments.
Another question: I want to iterate through each of the command line arguments. I understand that
Code:
argument=$1
will store my first argument in there. ((argument++)) won't work. I've tried googling for the answer, but nothing much has helped. An example would be:
Code:
$ checkD ~/Desktop ~/Music
How do I iterate 'argument' from $1 to $2, and from $2 to $3, and so on?
I'm expecting something like
Code:
argument=$1
count=1
end=$#
while( count != end )
do
echo $argument
((count++))
shift 1
done
a C++ equivalent would be
Code:
int main( int argc , char* argv[] ){
int i = 1;
while( i < argc )
cout << argv[i] << ' ';
i++;
}
Last edited by newbiecolonopenparens; 05-23-2012 at 05:02 PM.
colucix, that line of code definitely prints out all sub-directories, but I'm trying to get the number of folders, where the argument comes from command line arguments.
So pipe the output to wc -l as you did in your example. Or am I missing something?
Code:
n=$(find $1 -mindepth 1 -type d | wc -l)
My remark was that a workaround is not necessary (subtracting 1 from the result) if the source of the problem can be fixed.
Quote:
Originally Posted by newbiecolonopenparens
Another question: I want to iterate through each of the command line arguments.
Using shift as you already tried. However you must not increase the argument number, since arguments are moved to the left and the current one is always $1. Example:
The implementation of "$*" has always been a problem and realistically should have been replaced with the behavior of "$@". In almost every case where coders use "$*", they mean "$@". "$*" Can cause bugs and even security holes in your software.
Thank you for the prompt responses, divayashree and evo2.
evo2's code worked just fine. It was exactly what I needed.
However, divayashree's did not work. It looks a bit cleaner though. I got the error "expr: non-numeric argument"
Is there a way to make it work with expr?
Have you used the backtick as I mentioned , without which it will give error ...
marked in red bold,
Quote:
num=$(expr `find $1 -type d | wc -l` - 1)
Last edited by divyashree; 05-23-2012 at 10:41 PM.
Note that using wc to count the number of files is subject to some error. It's possible for file/directory names to contain newline characters themselves, and they would be counted as multiple files using this technique.
A more robust approach would be to output the files into an array using the -print0 option, and using that to get the count and to iterate over them.
bash v4+ has a new globstar option that, in simple cases, can replace find. (although I've found it to still be buggy when operating on certain large directory trees.)
#!/bin/bash
shopt -s globstar dotglob #set dotglob to count hidden directories as well
dirs=( "$1"/**/ )
#or using find
#while IFS= read -r -d '' ; do
# dirs+=( "$REPLY" )
#done < <( find "$1" -type d -print0 )
echo "The number of subdirectories in $1 is: $(( ${#dirs[@]} -1 ))"
for dir in "${dirs[@]}"; do
[[ $dir -ef "$1" ]] && continue #skip processing if it's the starting directory
echo "This is a command being applied to $dir"
done
exit 0
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.