Unwanted execution instead of saving to environment variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unwanted execution instead of saving to environment variable
# 1  
Old 10-30-2014
Unwanted execution instead of saving to environment variable

My .bash_profile file: source
Code:
$HOME/build.variables

My build.variables file:
Code:
export threads="$(${threads:-$(lscpu | egrep "CPU\(s\):|Thread" | $[$(sed -e:a -e "/$/N; s@CPU.*\([[:digit:]]\)\n.
*Thread.*\([[:digit:]]\)@\1*\2@;Ta")])})"

Code:
threads=$(lscpu | egrep "CPU\(s\):|Thread" | $[$(sed -e:a -e "/$/N; s@CPU.*\([[:digit:]]\)\n.*Thread.*\([[:digit:]]\)@\1*\2@;Ta")

The above codes when not comment out results in bash message, "-bash: 8: command not found" (because I have a 4 core cpu * 2 threads each). I tried other combination of brackets, backticks,echos, 2>&1 etc. with no success so far. I want to export the number, "8" not execute it. Thanks for your time.
# 2  
Old 10-30-2014
How about this:

Code:
export threads=$(lscpu | awk '/^CPU\(s\):/ {CPU+=$NF} /Thread/ {TH=$NF} END{ print CPU*TH}')

# 3  
Old 10-30-2014
Thanks for the effort. I should have mentioned I was not looking for another method for achieving the result but I am thinking maybe there was a bracket modification, quotes addition or pipe addition to solve the problem or an understanding of what is causing the fail and why a slight modification will never work.

I suspect that bash math brackets, "$[]" want to act as an executable not matter what in brackets, "$()" and I was wondering if it is possible to change that behaviour.
# 4  
Old 10-30-2014
Way to many levels of $() and $[]

This should "work"
Code:
export threads="${threads:-$(($(lscpu | egrep "^CPU\(s\):|Thread" |
    sed -e:a -e "/$/N; s@CPU.*\([[:digit:]]\)\n.*Thread.*\([[:digit:]]\)@\1*\2@;Ta")))}"

Edit: Note I added "^" (Beginning of line match) to the CPU grep RE, as on my system, lscpu was also outputting the following line, which the grep was (incorrectly) catching:

Code:
NUMA node0 CPU(s):     0,1


Last edited by Chubler_XL; 10-30-2014 at 11:10 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 11-01-2014
Impressive. I still have to study what different brackets do. The following might be the final revision, which you helped create, to be used in a new method of building xorg git:

Code:
export threads=$(($(lscpu | egrep "^CPU\(s\):|Thread" | sed -e:a -e "/$/N; s@C.*\([0-9*]\)\nT.*\([0-9*]\)@\1*\2@;Ta")))

Also, could I infer that by the word, "incorrectly" that I should report to the grep maintainer the unexpected behavior of catching the following line?
# 6  
Old 11-01-2014
Quote:
Originally Posted by charlieandlinux
[...]Also, could I infer that by the word, "incorrectly" that I should report to the grep maintainer the unexpected behavior of catching the following line?
What you should infer is that an unforeseen result was produced because the original regular expression `"CPU\(s\):|Thread"' was able to match the following line, outputted by lscpu:
Code:
NUMA node0 CPU(s):     0,1

This is correct since it satisfies the regex by matching the part CPU(s):
In order to exclude that unwanted match, the beginning of line anchor was added later on.
Code:
"^CPU\(s\):|Thread"

Letting only match like these be the acceptable result:
Code:
CPU(s):                x
Thread(s) per core:    x

Even when, maybe, just in case, it should be
Code:
"^CPU\(s\):|^Thread"

if the lesson was learned.

Last edited by Aia; 11-01-2014 at 02:50 AM.. Reason: Adding matching part
This User Gave Thanks to Aia For This Post:
# 7  
Old 11-01-2014
No lesson on my part can be learned. Only a misunderstanding of language corrected. I incorrectly understood that, "lscpu was also outputting the following line, which the grep was (incorrectly) catching" meant that grep was returning the next line after "CPU(s):" (two different lines total) which would be "On-line CPU(s) list" on my system but some unknown line after "Numa node CPU(s):" on his computer which I don't have on my computer. Therefore I thought egrep had a bug of returning two lines on his machine and only one on mine.

But the difference between your clarification and chubler is that yours adds that the unforeseen result was due to the regular expression and omits the former statement of 'grep being incorrect'.

Last edited by charlieandlinux; 11-01-2014 at 03:22 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Saving Mod in a variable

Hello Experts, In one of my shell script, I've been trying to calculate mod and saving it in a variable, below is what I have tried but it isn't working. Any help appreciated!!! #!/bin/bash num1=4 num2=3 echo "Number one is $num1" echo "Number two is $num2" mod_final=$(( echo "num1%num2"... (7 Replies)
Discussion started by: mukulverma2408
7 Replies

2. Shell Programming and Scripting

awk - saving results of external script to variable.

So, I've been playing with speeding up some analysis we do by using multiple threads of awk (actually, mawk, but code-compatible as far as I use it) on multiple CPU cores. So, I have a big data file and I have several copies of exactly the same processor script, written in mawk. I also have a... (8 Replies)
Discussion started by: treesloth
8 Replies

3. Shell Programming and Scripting

Non trivial file splitting, saving with variable filename

Hello, Although I have found similar questions, I could not find advice that could help with our problem. The issue: We have a few thousands text files (books). Each book has many chapters. Each chapter is identified by a cite-key. We need to split each of those book files by... (4 Replies)
Discussion started by: samask
4 Replies

4. Shell Programming and Scripting

Expand an environment variable in sed, when the variable contains a slash

I'm trying to make a sed substitution where the substitution pattern is an environment variable to be expanded, but the variable contains a "slash". sed -e 's/<HOME_DIRECTORY>/'$HOME'/'This gives me the following error: sed: -e expression #1, char 21: unknown option to `s'Obviously this is... (2 Replies)
Discussion started by: Ilja
2 Replies

5. Shell Programming and Scripting

Trouble saving variable

Hi, I have problems when you save a variable of a command. I have put the following line: CONEXION_BAGDAD = $ (grep-c "Please login with USER and PASS" $ LOG_FILE_BAGDAD) But I returned the following error: syntax error at line 67: `CONEXION_BAGDAD = $ 'unexpected Because it can happen?... (2 Replies)
Discussion started by: danietepa
2 Replies

6. Shell Programming and Scripting

Help with capturing homedir via ssh and saving to variable

I need to capture the homedir using the ssh command and then saving it to a variable. The results from the following command is what I need to capture to a variable: NOTE: the value I'm getting back is also incorrect. as it seems to be getting the home dir from the local server and not the... (2 Replies)
Discussion started by: reneuend
2 Replies

7. Shell Programming and Scripting

Execution issue with shell script - works in a different environment

Hi I get the following error while executing the shell script. I did not get an error when I ran the script in a different environment (unix server). str-token.ksh: 0403-057 Syntax error at line 20 : `(' is not expected. This is the line which gives error string=(${pos_array}) Please find... (3 Replies)
Discussion started by: hidnana
3 Replies

8. UNIX for Dummies Questions & Answers

saving command output to a variable

Hello, I have a shell script containing a command string in the following format: command1 | command2 | cut -c9-16 The output from this is a record number (using characters 9-16 of the original output string) e.g. ORD-1234 I wish to save this value to a variable for use in later commands... (4 Replies)
Discussion started by: philjo
4 Replies

9. Shell Programming and Scripting

saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash: total=`awk '{sum+=$3} END {print sum}' "$count".txt"` ((count++)) the above statement is in a while loop.. $count is to keep track of file numbers (1.txt,2.txt,3.txt,etc.) i get the following error: ./lines1:... (1 Reply)
Discussion started by: npatwardhan
1 Replies

10. Shell Programming and Scripting

Saving output from awk into a perl variable

How would I pass awk output to a perl variable? For example, I want to save the value in the 4th column into the variable called test. My best guess is something as follow, but I am sure this isn't correct. $test = system("awk '/NUMBER/{print \$4}' $_"); (8 Replies)
Discussion started by: userix
8 Replies
Login or Register to Ask a Question