Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to run this bash command :

#!/bin/bash
rep="*"

for f in `ls -R`$rep; do
d='git log '$f'| wc -l'
c=$d
echo $c
done

how to excute a command git log myFile | wc -l from bash ?

ps : this command will return a number : git log myFile | wc -l

share|improve this question
2  
See I'm trying to put a command in a variable, but the complex cases always fail! regarding your $d variable. –  manatwork Oct 10 '12 at 9:34
2  
See Why you shouldn't parse the output of ls(1) regarding the use of ls. –  manatwork Oct 10 '12 at 9:38
    
Really? You seems to know the back tick already, why are you asking about it again? –  warl0ck Oct 10 '12 at 9:41
add comment

5 Answers

up vote 1 down vote accepted

If you want to execute a command and get the output use the line below

d=`git log`

In your script you have to change two two things. I have the correct script below

#!/bin/bash
rep="*"

for f in `ls -R $rep`; do
d=`git log $f| wc -l`
c=$d
echo $c
done

Edit: The original correction is changing the quotes to backticks to make the output reach the d variable. In addition, the $rep should be inside the backticks with the ls, otherwise it will add the * at the end of the last file name processed.

share|improve this answer
    
Yes it works, i have a same result when i run eval commande :). Thanks –  user15992 Oct 10 '12 at 9:42
add comment

I've already answered your other question Syntax error in a bash script that calls find, so I won't repeat what I already wrote in my answer there.

Do not parse the output of ls. You don't need to run ls to enumerate files in a directory. Use find if you need to recurse into subdirectories.

To get the output of wc -l, use it in a command substitution: $(git log "$f" | wc -l)

Note that you need double quotes around the variable name. Always use double quotes around variable and command substitutions: "$foo", "$(foo)". Double quotes turn off the shell's further processing of the value of the variable or command output: without quotes, the result is split into words which are interpreted as file wildcard patterns. With single quotes, the $ sign is not treated specially, so git would see $f instead of the value.

find . -type f -exec sh -c '
  for x in "$@"; do
    c=$(git log "$f" | wc -l)
  done
' _ {} +

The use of -exec … {} + instead of -exec … {} \; allows the intermediate shell to be invoked only once per large batch of files, which is a bit faster.

Since bash ≥4, you don't need to use find, you can use the ** syntax to recurse into subdirectories.

shopt -s globstar
for x in **/*; do
  [ -f "$x" ] || continue  # skip directories and other non-regular files
  git log "$x" | wc -l
done
share|improve this answer
add comment

As far as i know the command :

     git log

Will show all the revision histories. If that is true, i guess you are trying to store the contents inside a file called myfile and then you want to count the no of lines in the file.

If this is what you want, first you can save the history in this way:

     git log>myFile.txt

Now you need to find the no of lines using wc -l and store it a file called nOfLines.txt

So you have to do this :

    wc -l<myFile.txt>nOfLines.txt

Then store the content of this file inside a variable called gitVar.

     gitVar=$(cat nOfLines.txt)

You can see whether this is working properly using this command :

     echo "$gitVar"

Just bundle the whole thing inside a script called "myCommand.sh", give it permssions using chmod a+x and run the script from the terminal like a command.

Hope this helps you .

share|improve this answer
    
You may prefer wc -l < myFile.txt > nOfLines.txt instead to avoid wc mentioning the input file's name in its output. –  manatwork Oct 10 '12 at 10:02
    
@manatwork : yes, tx for pointing it out. –  The Dark Knight Oct 10 '12 at 10:05
    
You can not have spaces around = in the assignment: gitVar=$(cat nOfLines.txt). –  manatwork Oct 10 '12 at 10:07
    
@manatwork: Ahhh, another blunder by me. Okay edited the ans as per all your suggestions. Thanks a lot. –  The Dark Knight Oct 10 '12 at 10:09
add comment

I have a solution, i think with a commande eval :D

share|improve this answer
add comment

I think you want to do:

#!/bin/bash
rep="*"

for f in `ls -R`$rep; do
d='git log "$f"| wc -l'
c=$d
echo $c
done

Notice the double quotes instead of the single ones around git command.

A smaller solution could be to use:

echo `git log "$f"| wc -l`
share|improve this answer
    
No with your commande i have the same result as my script. In addition you have a mistake in your script :) –  user15992 Oct 10 '12 at 9:31
    
I am really sorry. I had an extra quote. Please try now and upvote it if it works for you. –  Aditya Patawari Oct 10 '12 at 9:32
    
i corrected it and i have the same result ! –  user15992 Oct 10 '12 at 9:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.