-ne 0 doesn't work -le does

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting -ne 0 doesn't work -le does
# 1  
-ne 0 doesn't work -le does


I am using korn shell.

until [[ $# -ne 0 ]]
echo "\$# = " $#
echo "$1"

To the above script, I passed 2 parameters and the program control doesn't enter inside "until" loop. If I change it to until [[ $# -le 0 ]] then it does work.

Why numeric comparison is not working with -ne and works with -le?

thank you

Last edited by Scrutinizer; 06-04-2015 at 02:42 PM.. Reason: code tags
# 2  
-ne means not equal to
-le means less than or equal to

If you run the script and do not pass any parameters, it will enter the loop as it currently it is waiting 'until' $# is not equal to 0, which it will be set to if you do not pass any parameters (it will then fail on the shift command).

If you run the script and do pass some parameters, it will not enter the loop because $# is already not equal to 0, so it has nothing to wait 'until' for.

I hope that has made some sense.
# 3  
It is a matter of logic. Until the number of parameters is not equal to 0. If the number of parameters is 2 then it will not enter the loop, since the condition is satisfied ( 2 is unequal to 0 ).
# 4  
You probably want a while loop rather than an until loop:

while [[ $# -ne 0 ]]
  echo "\$# = " $#
  echo "$1"

It can also be written: while (($#))

until (( $# == 0 ))

Last edited by cfajohnson; 06-04-2015 at 07:11 PM..
Login or Register for Dates, Times and to Reply

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

Test Your Knowledge in Computers #275
Difficulty: Easy
OpenSolaris is the only version of the System V variant of UNIX available as open source.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Why doesn't this work?

find . -name "05_scripts" -type d -exec mv -f {}/'*.aep\ Logs' {}/.LogFiles \; Returns this failure: mv: rename ./019_0120_WS_WH_gate_insideTEST/05_scripts/*.aep\ Logs to ./019_0120_WS_WH_gate_insideTEST/05_scripts/.LogFiles/*.aep\ Logs: No such file or directory I don't know why it's trying... (4 Replies)
Discussion started by: scribling
4 Replies

2. Shell Programming and Scripting

my script doesn't work :(

i have this script and when i ejecute it, the console tell me this " sintax error line 41 unexpected element "}" " is the sintaxis ok? #!/bin/bash if ;then { exit 0; } if ; then { sudo /etc/init.d/apache2 start; sudo /etc/init.d/mysql start; php5 & nautilus... (3 Replies)
Discussion started by: keiserx
3 Replies

3. Solaris

shutdown -y -i5 -g0 DOESN'T work

hello, The command above seems not working on my solaris 8/9 sparc machines. a. resulted to the ff below when I instead use "shutdown" only. Broadcast Message from root (pts/1) on "hostname" date.. The system "hostname" will be shut down in 30 seconds THE SYSTEM bdosg IS BEING SHUT... (4 Replies)
Discussion started by: lhareigh890
4 Replies

4. Shell Programming and Scripting

HELP: If Doesn't Work in AWK

Hi! I have a somehow big file (almost 3000 lines long and thirteen columns). Some lines have no value at all or, at least, are incomplete. The columns' values that have no data are marked with a "-" and the corresponding line (the line that owns that value) should be discarded and not used. ... (5 Replies)
Discussion started by: Marcelo de Brit
5 Replies

5. UNIX for Dummies Questions & Answers

useradd doesn't work! why??

# uname -a Linux localhost.localdomain 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386 GNU/Linux # useradd 4141421248 useradd: invalid user name '4141421248' why??? (6 Replies)
Discussion started by: cqlouis
6 Replies

6. UNIX for Dummies Questions & Answers

FTP doesn't work

Hi! I have 2 servers. The firts has vsftpd server with this configuration: # Example config file /etc/vsftpd.conf # # The default compiled in settings are fairly paranoid. This sample file # loosens things up a bit, to make the ftp daemon more usable. # Please see vsftpd.conf.5 for all... (2 Replies)
Discussion started by: Torquemada
2 Replies

7. UNIX for Dummies Questions & Answers

my make doesn't work

hi I wrote the following makefile, I have just one problem, when i type make clean I get the message make 'clean' is up to date and any obj file is removed from my folder, what's wrong? Thank you CC = cc all: es.o elaboration.o $(CC) -o es es.o elaboration.o elaboration.o:... (0 Replies)
Discussion started by: Puntino
0 Replies

8. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

9. Shell Programming and Scripting

sed doesn't work

Hello I' m confused a bit. I want to replace string "&amp" with "&" using this command. sed 's/&amp/&/g' and it doesn't work. Nothing happens. On the other side this works: sed 's/&amp/@/g' or sed 's/&amp/^/g' !!! Can somebody help please? Thanks (3 Replies)
Discussion started by: billy5
3 Replies

10. Shell Programming and Scripting

Why doesn't this work?

cat .servers | while read LINE; do ssh jason@$LINE $1 done exit 1 ./command.ksh "ls -l ~jason" Why does this ONLY iterate on the first server in the list? It's not doing the command on all the servers in the list, what am I missing? Thanks! JP (2 Replies)
Discussion started by: jpeery
2 Replies

Featured Tech Videos