TCSH Check if number is even


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting TCSH Check if number is even
# 1  
Old 08-10-2010
TCSH Check if number is even

Hey,
I am trying to check if an integer is even in a tcsh script

This is what I am running now

Code:
set lattest = ` echo $latmin "%2" | bc -l `
echo $lattest

if ( $lattest == 0 ) then
    echo "min is already even"
else if ( $lattest =! 0 ) then    
    set latmin = ` echo $latmin "+1" | bc -l `
endif

but it identifies both even and odd numbers as being even.

Last edited by Scott; 08-10-2010 at 01:01 PM.. Reason: Please use code tags
# 2  
Old 08-10-2010
Hi.

If I had to use tcsh, I would use something like:
Code:
#!/usr/bin/env tcsh

# @(#) s1	Demonstrate remainder operator in tcsh.

# Infrastructure details, environment, commands for forum posts. 
# Uncomment setenv command to run script as external user.
# setenv PATH "/usr/local/bin:/usr/bin:/bin"
echo
setenv LC_ALL C ; setenv LANG C
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility version)"
sh -c "version >/dev/null 2>&1 && version '=o' tcsh"
echo

@ a = 1 + 1
echo " a is $a"

echo
@ even = 16 % 2
echo " even is $even"

echo
@ odd = 17 % 2
echo " odd is $odd"

echo
set n = 22
if ( $n % 2 == 0 ) then
  echo " $n is even"
else
  echo " $n is odd"
endif

echo
set n = 23
if ( $n % 2 == 0 ) then
  echo " $n is even"
else
  echo " $n is odd"
endif

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility version)
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
tcsh 6.14.00

 a is 2

 even is 0

 odd is 1

 22 is even

 23 is odd

See man tcsh for details. However, if I had a choice, I would use one of the Bourne shell family: sh, bash, ksh, zsh for scripting. There are too many pitfalls and missing features in csh / tcsh. They are, however, very useful for interactive work.

Best wishes ... cheers, drl
# 3  
Old 08-10-2010
Thanks,


I adapted the if statement
Code:
if ( $n % 2 == 0 ) then
  echo " $n is even"
else
  echo " $n is odd"
endif

from your program and it worked like a charm

Last edited by Scott; 08-10-2010 at 02:18 PM.. Reason: Code tags, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Csh/tcsh : Check the file existance and run the script

Hi, I've to wait until a file generated and once its generated, source another script in Linux terminal. Please help me as this is very very urgent. The code should be something like if ( -e "/abc/xyz/a.txt ) source aaa.csh else sleep This should be repeated till the if... (4 Replies)
Discussion started by: kumar_eee
4 Replies

2. UNIX for Dummies Questions & Answers

How to check if a number exists?

Hello, May i please know how do i check if the given input argument is one of the listed numbers then success else failure. I am using bash shell. if then echo "success" else echo "failure" fi Thank you. (2 Replies)
Discussion started by: Ariean
2 Replies

3. Shell Programming and Scripting

number of instance check

Hi, We are getting a curios result in one of AIX script. Its executed using !/bin/ksh . After following line we get result of 3 in in the variable instance_count. instance_count=`ps -ef | grep "script_check_instances.sh" | grep -v "grep" | wc -l` But once we do a "ps -aef | grep... (2 Replies)
Discussion started by: niba
2 Replies

4. Shell Programming and Scripting

Check params for number

I have 2 and three params, both I should make sure thay numbers at one single line insted of checking for each one . Example I wroote the following way.. checking for 2 and three seperately but I shud be able to do it at on statement echo $2 | egrep '^+$' >/dev/null 2>&1 if ; then echo... (2 Replies)
Discussion started by: raopatwari
2 Replies

5. Shell Programming and Scripting

tcsh: not run script (if: Badly formed number)

Hello I have Linux Debian & tcsh shell. My mini script not run :(. Startup script displays a message: user@host:dir% ./test if: Badly formed number. script: #!/bin/tcsh -f #script check size files systems set x = `df -h /usr | tail -n1 | awk '{ print( $5 ); }'` set y = 60% if ($x... (5 Replies)
Discussion started by: moskovets
5 Replies

6. Shell Programming and Scripting

Check if the value is Number

Hi, I have a file with data as given $cat file1.txt 123 234 23e 234.456 234.876e 345.00 I am checking if the values are proper integers using the command. nawk -F'|' 'int($1)!=$1 {printf "Error in field 1|"$0"\n"}' file1.txt This is checking for only integers ( without... (10 Replies)
Discussion started by: ashwin3086
10 Replies

7. Shell Programming and Scripting

Check mail periodically with tcsh

A friend of mine is switching his email from one University server to a different one. He is using the tcsh, and on his current server, the shell will tell him (when the prompt is displayed) if there is new mail. The new server, where he also has the tcsh, does not do this by default. I got him... (0 Replies)
Discussion started by: kermit
0 Replies

8. Shell Programming and Scripting

To check a word is number or not

Hi, I have one file like 00123. And this file name is generated as a sequence. So how can I confirm the generated file name is a number, not a special character or alphabets. Can anybody help me out. Thanks in advance. (3 Replies)
Discussion started by: Kattoor
3 Replies

9. Shell Programming and Scripting

How to check whether a string is number or not

Hi , I am facing a problem .. which looks simple... but took 2 days of mine.. even now it is not solved completely.. I have one variable..., want to know whether that variable contains number... canbe +ve or -ve ... Values +35 --- number -43 --- number 45A -- non number... (12 Replies)
Discussion started by: shihabvk
12 Replies

10. UNIX for Dummies Questions & Answers

Check if variable is a number

If I have a variable $X, how do I check it is a number? Many thanks. (2 Replies)
Discussion started by: handak9
2 Replies
Login or Register to Ask a Question