LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to run a command in another command? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-run-a-command-in-another-command-4175537172/)

AcousticBruce 03-18-2015 05:59 PM

How to run a command in another command?
 
I am not sure how to ask this, sorry.

if I had a code like this

Code:

# grep -a ": " md5list.txt | cut -f2,3 -d

How can I run the command basename for each line of the output?

basename {(grep -a ": " md5list.txt | cut -f2,3 -d )}


EDIT: A little more clarity on what im doing:
I didn't realize that 'md5sums' was a link to a nice formatted page. So I copied all packages here and put them into a text file. I decided to write a script that put all of these in that format.

So basically, even though I have already ran the md5sum -c 'md5sum-list' I still want to finish this small project because I am learning a ton.

Philip Lacroix 03-18-2015 06:17 PM

Hi

This should work:

Code:

for filename in "$( grep -a ": " md5list.txt | cut -f2,3 -d " " )" ; do basename "$filename" ; done
<edit> In your example option "-d" has no argument: that will not work.

Philip

AcousticBruce 03-18-2015 06:46 PM

Quote:

Originally Posted by Philip Lacroix (Post 5334352)
Hi

This should work:

Code:

for filename in "$( grep -a ": " md5list.txt | cut -f2,3 )" ; do basename "$filename" ; done
<edit> In your example option "-d" has no argument: that will not work.

Philip

Actually, the -d had " " as an argument. Its hard to see though.

This is similar to your code but using {} instead of $()...

Code:

for i in {1..5}; do echo $i; done
The code ran but stopped on the second output. There is 79 to go. Not sure why it stopped.



What about eval?? Would my code need that? I do not understand $() in your example. what does this do? what is it called?
Code:

for i in $(eval echo "{$START..$END}")

AcousticBruce 03-18-2015 07:02 PM

I worked with your code and came up with a way to iterate through the file, but still I can not figure how to run baseline.

Code:

# for i in {1..200}; do sed '$i,0!d' md5list.txt | grep -a ": "; done
How can I run baseline for each of these iterations?

Philip Lacroix 03-18-2015 07:32 PM

Quote:

This is similar to your code but using {} instead of $()...
They have a completely different meaning, being similar only graphically. In one case you run the for loop for each line which is grepped and cut from your file: basename is run on the resulting string. This is called command substitution. Your example, on the other hand, uses extended brace expansion.

Quote:

The code ran but stopped on the second output. There is 79 to go. Not sure why it stopped.
Any error messages? Make sure that grep is actually capturing what you want, and that cut is actually finding something in the specified fields. Posting here a few lines from your file (or from a similar one with the same structure) may be helpful.

Quote:

What about eval?
Not sure if it is useful here.

This is a great reference guide for bash scripting: Advanced Bash-Scripting Guide

Miati 03-18-2015 07:51 PM

Wish I knew what the md5list.txt file contained. Hard to help when that's not available. I am assuming it's the normal output of md5sum.

This may be a better method:

Code:

while read i
  do
    basename "$(echo "$i" | grep -a ": " | cut -f2,3 -d" ")"
done < md5list.txt

Each line of output is run through the while loop until done.
I added a " " to the -d cause I wasn't sure what you were defining so I figured whitespace.

AcousticBruce 03-19-2015 05:58 AM

Quote:

Originally Posted by Miati (Post 5334380)
Wish I knew what the md5list.txt file contained. Hard to help when that's not available. I am assuming it's the normal output of md5sum.

This may be a better method:

Code:

while read i
  do
    basename "$(echo "$i" | grep -a ": " | cut -f2,3 -d" ")"
done < md5list.txt

Each line of output is run through the while loop until done.
I added a " " to the -d cause I wasn't sure what you were defining so I figured whitespace.


Yes, whitespace is correct. Because I was using the PDF document, I didn't realize that 'md5sums' was a link to a nice formatted page. So I copied all packages and md5 text here and put them into a text file. I decided to write a script that put all of these in that format.

So basically, even though I have already ran the md5sum -c 'md5sum-list' I still want to finish this small project because I am learning a ton.

I ran your script and got a syntax error near unexpected token 'done'


All times are GMT -5. The time now is 01:00 PM.