starting programmer


 
Thread Tools Search this Thread
# 1  
starting programmer

Hi to all,

I'm started to write some very simple loops in bash an i'm getting this error

Code:
-bash: [: x: integer expression expected

(the example is just to show when the error appeared)

the code was

Code:
x=o

while [ x -ne 2 ]
do 

echo "hello"

x++
done


Last edited by radoulov; 12-16-2011 at 05:25 PM.. Reason: Code tags, please!
# 2  
Hi gogodash,
welcome to the forums!

You should definitely read your shell manual pages. You're getting errors because your code is wrong ...

Try something like this:

Code:
x=0
while [ "$x" -lt 2 ]; do 
  echo "hello"
  x=$(( x + 1 ))
done

# 3  
A few problems there.

Code:
x=o

Did you mean zero?

Code:
while [ x -ne 2 ]

You've got the right idea, and the right syntax, but you're giving it the string "x", not the contents of the variable. Use "$x".

Code:
x++

Shell math doesn't work this way. If you have bash or ksh:
Code:
((x++))

otherwise do this:
Code:
x=`expr $x + 1`

# 4  
Make sure that "x=0" not "x=o".
Code:
#! /bin/bash

x=0

while [ ${x} -ne 2 ]
do
    echo "hello ${x}"
    x=$(( x + 1 ))
done

# 5  
In addition to what they said, since you're starting out and already mentioned you're using BASH: Don't use the classic shell builtin '[ expression ]' ; use the shell keyword '[[ expression ]]'. Trust me, it's all-around better, unless you're coding for old busted bourne shell systems.
One benefit: you won't have to worry about putting quotes around your variables, which was part of your problem.

The conditional expression [Bash Hackers Wiki]
# 6  
wow thanks guys I've got it , the loop was an example for commend that i want to put in side the loop to boot some long distance server...
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Mathematics
Difficulty: Easy
An equilateral triangle always has every angle measuring 60°.
True or False?

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

2. What is on Your Mind?

A Programmer is .....

https://www.unix.com/picture.php?albumid=112&pictureid=603 (7 Replies)
Discussion started by: Neo
7 Replies

3. Linux

Starting Linux for a Programmer

Hi all I am application Programmer. In my college(2 yrs back) i have learnt Unix i.e commads, shell scripts, Filesystem,I reffered to a book by "Sumitabha Das". I want to learn Linux. But i cant understand where should i start from and which book to refer to. Most of the books these days eg.... (2 Replies)
Discussion started by: FullMetal
2 Replies

4. Shell Programming and Scripting

help a newbie programmer tcl

Prompt please where the error occurred. text.txt obtained by ls-lah> text.txt proc file_to_mas {name_file} { set fileid seek $fileid 0 start global mas set mas(0) 1 for {set i 0} {!} {incr i} { gets... (2 Replies)
Discussion started by: augur_aen
2 Replies

5. Programming

do you think you are a real c programmer then lets see..

#include<stdio.h> #include<unistd.h> #include<fcntl.h> #include<termios.h> #include<string.h> #include<errno.h> #include<sys/types.h> #include<sys/select.h> #include<string.h> #include<sys/time.h> void *thread_function(void *arg); char quit='\0';/* this tends to terminate the read... (2 Replies)
Discussion started by: harsh_it
2 Replies

6. Programming

do you think you are a real c programmer then lets see..

here is a code which sends and receives sms through a serial port. the problem is that its givin segmentation fault. it first sets the file discriptor and the initialises the modem by using AT commands. there are two threads for reading and writing . rest the code is simple you'll get it. user has... (1 Reply)
Discussion started by: harsh_it
1 Replies

7. SCO

Need Sco Programmer

Need programmer to work on 8-12 month project near Dallas, Texas or can telecommute to make changes to SCO File Pro 32 UNIX software that was converted to DOS.....Can't find anything as good as what we had in any other OS. Brent Davis brentd@texasspecialty.com (0 Replies)
Discussion started by: brentpdavis
0 Replies

Featured Tech Videos