Embed commands into an expr command? (so I only store result)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Embed commands into an expr command? (so I only store result)
# 1  
Old 10-17-2010
Embed commands into an expr command? (so I only store result)

Hi

Can I thank the advice I've received on here previously, working examples are helping me teach myself BASH fairly quickly and I've written some pretty complex scripts now (even if I do say so myself).

Anyhooo.....

I'm writing a script that handles lots of text files and does various calculations based on the count of those files and that sort of gumph.

At the moment for example I have sets of lines similar to this:-

Code:
VALUE=`find /path/to/find -iname *.txt | sed -e s'\/\path\/to\/find\///' | wc -l`
VALUE=`expr $VALUE - 1`

I realise the sed is redundant however it's for the example.

I then use this value for just on calculation so the second VALUE= line is a bit of overkill really.

is there some way of coding something like

Code:
VALUE=`expr (find /path/to/find -iname *.txt | sed -e s'\/\path\/to\/find\///' | wc -l) - 1`

So that the line is all self contained and more readable and logical? Is there a general rule/syntax on how you can structure this?

Thanks in advance

Last edited by Bashingaway; 10-17-2010 at 09:34 AM.. Reason: added note
# 2  
Old 10-17-2010
Try this,

Code:
VALUE=$(expr $(find /path/to/find -iname *.txt | sed -e s'\/\path\/to\/find\///' | wc -l) - 1)

# 3  
Old 10-17-2010
Or something like this:

Code:
VALUE=$(( $( find /path/to/find -iname *.txt | sed -e s'\/\path\/to\/find\///' | wc -l ) - 1 ))

# 4  
Old 10-17-2010
Thanks guys, it was the use of the $ I wasn't aware of, googling for bash embedded command throws up a LOT of answers that aren't relevant.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to store result in variable for other lines in script to use

I can not figure out how to capture the $filename variable store by the bash. #!/bin/bash # oldest folder stored as variable for analysis, version log created, and quality indicators matched to run dir=/home/cmccabe/Desktop/NGS/test find "$dir" -maxdepth 1 -mindepth 1 -type d -printf... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Store result variable

Friends have the following problem: cat $PATH_DAT/mr.txt | nawk 'BEGIN { CantPnt=0; NumReg=0; FS="|" } { NumReg++ CantPnt=CantPnt+int($2) } END{ printf... (5 Replies)
Discussion started by: tricampeon81
5 Replies

3. Shell Programming and Scripting

How to store result of grep into a variable?

In a directory i have a file *output* whose contents are changing for every simulation. zgrep "trace file is" *output* | zgrep g o/p: trace file is Int_01.1352176388z4f56ec33.0.trace.gz I want to extract "Int_01.1352176388z4f56ec33.0.trace.gz" from the above result into a variable. i... (2 Replies)
Discussion started by: twistedpair
2 Replies

4. Shell Programming and Scripting

How to embed commands in awk search

Hello; When I try: awk '/$(date "+%y\/%m\/%d")/,0' It errors with: awk: There is a regular expression error. Invalid pattern. Is there anyway to make this work ?? Thank you (2 Replies)
Discussion started by: delphys
2 Replies

5. Shell Programming and Scripting

Assign zero to strings that don't appear in block, store result in AWK array

Hi to all, I have this input: <group> <x "2">Group D</x> <x "3">Group B</x> <x "1">Group A</x> </group> <group> <x "1">Group E</x> <x "0">Group B</x> <x "1">Group C</x> </group> <group> ... (11 Replies)
Discussion started by: Ophiuchus
11 Replies

6. Shell Programming and Scripting

Search file for string and store last result to variable

Hi, I'm trying to search a text file for a string: "energy(sigma->0)=". To do so, I executed the grep command, which returned many matches, for example: energy without entropy = -112.16486170 energy(sigma->0) = -112.16520778 energy without entropy = -112.16488936 ... (5 Replies)
Discussion started by: gwr
5 Replies

7. Shell Programming and Scripting

Find diff bet 2 files and store result in another file

Hi I want to compare 2 files. The files have the same amount of rows and columns. So each line must be compare against the other and if one differs from the other, the result of both must be stored in a seperate file. I am doing this in awk. Here is my file1: Blocks... (2 Replies)
Discussion started by: ladyAnne
2 Replies

8. Shell Programming and Scripting

Why can't embed commands like fg or bg in a shell script ?

Hi Can someone explain in an easy way that why can't embed commands like fg or bg in a shell script ? (4 Replies)
Discussion started by: qiulang
4 Replies

9. Shell Programming and Scripting

Prase a file and store and result to an array

Dear all, I have a file having the following formats: ThreadFail=Web1=1234 ThreadFail=Web2=2345 ThreadFail=Web3=12 ConnectionFail=DB1=11 ConnectionFail=DB2=22 The number of lines will be different from every time . How can I parse the file and store the result to an a array inside... (6 Replies)
Discussion started by: youareapkman
6 Replies

10. Shell Programming and Scripting

How to store query multiple result in shell script variable(Array)

:) Suppose,I have one table A. Table A have one column. Table A have 10 rows. I want this 10 rows store into shell script variable. like #!/bin/ksh v_shell_var=Hi here in call oracle , through loop How can I store table A's 10 rows into v_shell_var (Shell Script Array). Regards, Div (4 Replies)
Discussion started by: div_Neev
4 Replies
Login or Register to Ask a Question