simple(?) if/else question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers simple(?) if/else question
# 8  
Old 10-27-2011
Quote:
Originally Posted by astropi
On line 29 I have:

read a b < temp_star1

I have a lot of comments before that, and a few simple shell commands that should not affect anything. I just noticed I am using

#!/bin/bash

perhaps that's the problem? I'll check. Thanks.
no, bash should be fine.
are you sure your 'temp_star1' file always has TWO numbers and no empty lines?
# 9  
Old 10-27-2011
Yup, that was it. Obviously I'm just a beginner here! A couple of questions if you have the time.

1)What's the difference between bash and ksh and is one better than the other?

2)if [ "$(echo "if (${a} < ${b}) 1" | bc)" -eq 1 ]
could you explain the above line to me? Smilie

Thanks!!

edit: in my case, when I change from bash to ksh I don't get the same error in ksh.

Last edited by astropi; 10-27-2011 at 06:07 PM.. Reason: clarification
# 10  
Old 10-27-2011
Quote:
Originally Posted by astropi
Yup, that was it. Obviously I'm just a beginner here! A couple of questions if you have the time.

1)What's the difference between bash and ksh and is one better than the other?
This is the stuff religious wars are made of. There's not a clear winner or everyone would be using one or the other.

KSH is older and more traditional. If you're on a non-Linux system it's likely to have ksh.

BASH and modern enough versions of KSH have most of their string operations in common. They differ in how they use arrays ( ksh uses set -a, BASH uses VAR=( stuff ) )

BASH does not support floating point numbers. Very new versions of ksh do.
# 11  
Old 10-27-2011
Quote:
Originally Posted by astropi
Yup, that was it. Obviously I'm just a beginner here! A couple of questions if you have the time.

1)What's the difference between bash and ksh and is one better than the other?
they're 2 diff implementations with diff capabilities. I prefer using ksh for scripting and bash for the interactive sessions as ksh has been more widely available.
Quote:
Originally Posted by astropi
2)if [ "$(echo "if (${a} < ${b}) 1" | bc)" -eq 1 ]
could you explain the above line to me? Smilie

Thanks!!
as ksh doesn't support the floating point arithmetic natively, we have to do the math with the external tool - this case 'bc' (BasicCalculator) that support floating point math. We pipe the boolean expression if (${a} < ${b}) 1 which says: if $a < $b, then return 1. The output from 'bc' is then being compared to '1' and the one of the then/else branches get executed.
Quote:
Originally Posted by astropi
edit: in my case, when I change from bash to ksh I don't get the same error in ksh.
strange - there must something else triggering this error in bash. Once again, examine the file you're reading in as previously suggested.
This User Gave Thanks to vgersh99 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

2. Shell Programming and Scripting

Simple if then else question

I am having trouble making this statement work. I am passing in a number value for the number of days to keep archive logs for and wanted to make sure that it is a number. I have a script that will return 1 for is a number and 0 for is not a number. I also want to make sure that the number is not... (2 Replies)
Discussion started by: gandolf989
2 Replies

3. UNIX for Dummies Questions & Answers

simple question

hi everybody; trying to c unix programming and ive stucked with a problem: simple program filedr=open("tempfile",O_RDWR|O_TRUNC,0); write(filedr,msg1,6); int i; i=read(filedr,msg3,4); it returns 0 bytes read ... why? well if i try to poll() before read , it doesnt indicate POLLHUP or... (4 Replies)
Discussion started by: IdleProc
4 Replies

4. UNIX for Dummies Questions & Answers

Simple question

I had a script in solaris wich i read data, for example: Number 1: _ and the cursor use to be in '_' place because in the code of the script i write: echo "Number 1:\c" but i copy the script to a linux and the cursor 'jump' to the begining of the next line like: Number 1:... (2 Replies)
Discussion started by: lestat_ecuador
2 Replies

5. Shell Programming and Scripting

Simple Question

Hi, Please don't berate me over the simplicity of these questions. I have recently gotten into bash shell scripting and enjoy it quite a bit. One thing I have not found the answer to though is when naming a shell script, what extension is normally used (ie myscript.?)? Also where is the standard... (5 Replies)
Discussion started by: msb65
5 Replies

6. Shell Programming and Scripting

Simple ls question

i am doing ls -la in the out put , first line is as total 41621 What is this total? (2 Replies)
Discussion started by: Saurabh78
2 Replies

7. UNIX for Dummies Questions & Answers

Simple Question

Hi Guys, I've been learning UNIX for the past couple of days and I came across this exercise, I can't get my head around it, so I would be ever so grateful if I could receive some sort of help or direction with this. Create a file with x amount of lines in it, the content of your choice. ... (3 Replies)
Discussion started by: aforball
3 Replies

8. Programming

Simple C question... Hopefully it's simple

Hello. I'm a complete newbie to C programming. I have a C program that wasn't written by me where I need to write some wrappers around it to automate and make it easier for a client to use. The problem is that the program accepts standard input to control the program... I'm hoping to find a simple... (6 Replies)
Discussion started by: Xeed
6 Replies

9. UNIX for Dummies Questions & Answers

Ok simple question for simple knowledge...

Ok what is BSD exactly? I know its a type of open source but what is it exactly? (1 Reply)
Discussion started by: Corrail
1 Replies

10. UNIX for Advanced & Expert Users

Simple Question

Friends, I did following exercise $ echo '' > test $ od -b test $ echo "">test $ od -b test $echo > test $od -b test Every time I got the following output 0000000 012 0000001 But 012 is octal value for new line character . Even though there is no apperent new line character... (6 Replies)
Discussion started by: j1yant
6 Replies
Login or Register to Ask a Question