Csh arithmetic not producing the expected value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Csh arithmetic not producing the expected value
# 1  
Old 01-03-2014
Csh arithmetic not producing the expected value

My original post did not show up properly. I am trying again.
I have a simple tsch script that does some basic arithmetic. The calculated value was not producing the result I was expecting. I wrote a sample script to illustrate the things that I tried.

Code:
#!/bin/tcsh
@ count = 43
@ total_count = 61
@ failing = 4
@ no_finish = 0
echo "Total Count:$total_count  Finished:$count  Failed:$failing  $total_count-$count-$failing"
@ no_finish   = $total_count - $count - $failing 
echo $no_finish
echo "Total Count:$total_count  Finished:$count  Failed:$failing  $total_count-$count-$failing"
@ no_finish  = $total_count - ( $count + $failing )
echo $no_finish
echo "Total Count:$total_count  Finished:$count  Failed:$failing  $total_count-$count-$failing"
@ no_finish  = ( $total_count - $count - $failing )
echo $no_finish
echo "Total Count:$total_count  Finished:$count  Failed:$failing  $total_count-$count-$failing"
@ no_finish  = ( $total_count - $count ) - $failing 
echo $no_finish

script output

Total Count:61 Finished:43 Failed:4 61-43-4
22
Total Count:61 Finished:43 Failed:4 61-43-4
14
Total Count:61 Finished:43 Failed:4 61-43-4
22
Total Count:61 Finished:43 Failed:4 61-43-4
14



Last edited by sgualandri; 01-03-2014 at 04:52 PM.. Reason: original text did not show up
# 2  
Old 01-03-2014
Send send me a PM with the code you just posted.

We are trying to debug some issue with some new software from DragonByte.
# 3  
Old 01-03-2014
I cannot send a PM. I am a new user and do not have enough posts to qualify to send personal messages.

Steve
# 4  
Old 01-03-2014
It seems to evaluate right to left, a - b - c is push c, negate, add b, negate, add a. Yes, it is ugly. Use parens. I installed tcsh just to try:
Code:
$ tcsh -c '@ a = 61
@ b = 43
@ c = 4
@ tot = $a - $b - $c
echo $tot'
14
$ tcsh -c '
@ count = 43
@ total_count = 61
@ failing = 4
@ no_finish = 0
echo "Total Count:$total_count  Finished:$count  Failed:$failing  $total_count-$count-$failing"
@ no_finish   = $total_count - $count - $failing
echo $no_finish
echo "Total Count:$total_count  Finished:$count  Failed:$failing  $total_count-$count-$failing"
@ no_finish  = $total_count - ( $count + $failing )
echo $no_finish
echo "Total Count:$total_count  Finished:$count  Failed:$failing  $total_count-$count-$failing"
@ no_finish  = ( $total_count - $count - $failing )
echo $no_finish
echo "Total Count:$total_count  Finished:$count  Failed:$failing  $total_count-$count-$failing"
@ no_finish  = ( $total_count - $count ) - $failing
echo $no_finish
'
Total Count:61  Finished:43  Failed:4  61-43-4
14
Total Count:61  Finished:43  Failed:4  61-43-4
14
Total Count:61  Finished:43  Failed:4  61-43-4
14
Total Count:61  Finished:43  Failed:4  61-43-4
14
$ tcsh --version
tcsh 6.17.06 (Astron) 2011-04-15 (x86_64-unknown-linux) options wide,nls,dl,al,kan,rh,nd,color,filec
$

Some called it a documented feature, but I think that is a stretch: http://www.linuxmisc.com/12-unix-she...bdc36b511c.htm

Last edited by DGPickett; 01-03-2014 at 06:07 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script not producing output

Hi, I have a text file with some thousands of rows of the following kind (this will be referred to as the inputFileWithColorsAndNumbers.txt): Blue 6 Red 4 Blue 3 Yellow 4 Red 7 Colors in the left column and a number in the right one for each line. I want to run an awk... (5 Replies)
Discussion started by: Zooma
5 Replies

2. Shell Programming and Scripting

awk producing too many fields

Hey guys, awk is putting out too many results, two of each specifically. See below. Code: read CPU_VENDOR_ID <<< $(cat /proc/cpuinfo | grep "vendor_id" | awk -F: '{print $2}') echo $CPU_VENDOR_ID echo Output: AuthenticAMD AuthenticAMD I only want it to print out "AuthenticAMD" once. ... (7 Replies)
Discussion started by: 3therk1ll
7 Replies

3. IP Networking

Tcpdump -i any producing duplicate packages?

Hi, Can anyone explain why do I see same request twice in tcpdump package when I use "tcpdump -i any"? So I'm tracing packets on eth1.800 interface, but using "tcpdump -i any" to capture them. What I see is that the requests/responses from my side seem to appear as duplicate in the pcap file,... (1 Reply)
Discussion started by: Juha
1 Replies

4. Shell Programming and Scripting

arithmetic from csh variable passed to awk

I have the following code in a csh script I want to pass the value of the variable sigmasq to the awk script so that I can divide $0 by the value of sigmasq grep "Rms Value" $f.log \ | awk '{ sub(/*:*\.*/,x); \ print... (2 Replies)
Discussion started by: kristinu
2 Replies

5. Shell Programming and Scripting

Help, while loop and sed not producing desired output

Hello everyone, I need some assistance with what I thought would have been a very simple script. Purpose of Script: Script will parse through a source file and modify (search/replace) certain patterns and output to stdout or a file. Script will utilize a "control file" which will contain... (12 Replies)
Discussion started by: packetjockey
12 Replies

6. Shell Programming and Scripting

csh arithmetic ?

Hello, Could someone explain how this one is possible: # @ x = 10 - 11 + 3 # echo $x -4 I know that writing script using csh is bad idea, but I need to write few lines. thanks Vilius (2 Replies)
Discussion started by: vilius
2 Replies

7. Shell Programming and Scripting

Script not working...producing 0's and not number

Below is my script: #!/bin/sh #echo "Please type oracle-lower case please:" #read X #if ] #then # echo "Sorry that is not oracle, try again" # exit 1 #else # echo Thank you #fi find / -name oracle 2>/dev/null | while read line do bdf 2>/dev/null |... (6 Replies)
Discussion started by: bigben1220
6 Replies

8. Shell Programming and Scripting

sed in while loop producing arithmetic output

Hi all Just wondering if someone can help me with this. I'm trying to write a script that processes the output of another file and prints only the lines I want from it. This is only the second script I have written so please bare with me here. I have referred to the literature and some of the... (3 Replies)
Discussion started by: javathecat
3 Replies

9. Shell Programming and Scripting

csh failing to call an 2 embedded csh script

I have an extraordinary problem with a csh script.....(feel free to berate the use of this but I'm modifying an existing bunch of them) Anyway, I have a master csh script which in turn calls a second csh script. This second csh script is below. Within this second script are two compiled C++... (1 Reply)
Discussion started by: pollsizer
1 Replies

10. Shell Programming and Scripting

#/usr/bin/csh -f not working as expected?

Hey everyone, A coworker of mine has written a csh script that starts with #!/usr/bin/csh -f. It's my understanding that the -f should skip the .cshrc and .login files, but here's the problem: In the script "line" is used, and I happen to have a "line" in my ~/bin. When the script is ran my... (4 Replies)
Discussion started by: effigy
4 Replies
Login or Register to Ask a Question