integer comparasion troubles.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting integer comparasion troubles.
# 1  
Old 09-04-2007
integer comparasion troubles.

Hi, I am trying to store the real name of currently logged in users as a variable or in an array. I have been having troubly with this, below I have it set into a variable. But when it enters the loop to assign each to a variable it says:
Integer expression expected.

##!/bin/bash
## userprocesses
#Display actual users name

who | wc -l > nousers
set -- cut -f1 nousers
numberusers=$1
usercnt=1 # counter used in setup
pos=1 # position in array
outcount=1 # counter used in getname method
userout=1 # set for output

who -q > tempA
set -- $( cut -f "$pos" tempA);

while [ "$usercnt" -le "$numberusers" ]
do
eval user${pos}=$1
shift
usercnt='expr usercnt + 1'
pos='expr pos + 1'

done
echo "$user1 $user2 $user3"
# 2  
Old 09-04-2007
Line 2 sets $1 to "cut"
Line 3 sets $numberusers to $1, ie. "cut"
While loop fails on first iteration since "cut" is not an integer.

Try using set -x to debug your scripts.
# 3  
Old 09-06-2007
Quote:
Originally Posted by rorey_breaker
Hi, I am trying to store the real name of currently logged in users as a variable or in an array. I have been having troubly with this, below I have it set into a variable. But when it enters the loop to assign each to a variable it says:
Integer expression expected.

Code:
##!/bin/bash
## userprocesses 
#Display actual users name 

	who | wc -l > nousers
	set -- cut -f1 nousers
	numberusers=$1


Code:
numberusers=$( who | wc -l )

Quote:
Code:
	usercnt=1 # counter used in setup
	pos=1 # position in array
	outcount=1 # counter used in getname method
	userout=1 # set for output 

		who -q > tempA
		set -- $( cut -f "$pos" tempA); 

 while [ "$usercnt" -le "$numberusers" ]
do
		eval user${pos}=$1
		shift
		usercnt='expr usercnt + 1'
		pos='expr pos + 1'


You don't need expr in bash; expr is an external command (i.e., it's slow), and bash (like all POSIX shells) has integer arithmetic built in:
Code:
usercnt=$(( $usercnt + 1 ))
pos=$(( $pos + 1 ))

If you do use expr, you need to give it the contents of the variables, not their names (e.g., $usercnt, not usercnt).
Quote:
done
echo "$user1 $user2 $user3"[/CODE]

The whole operation can be done much more efficiently by reading the output of who directly:
Code:
who | while read name junk
do
   : do whatever
done

If you want to put the names into an array, use bash's built-in arrays:
Code:
names=( who | cut -d ' ' -f1 )
:


Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Troubles with OpenSSH

Hi, I am trying to login from one AIX server to another without using a password, a basic configuration, however it doesn't seem to work. All things are in place. I have both a public and private key in the ~/.ssh folder and also have an "authorized_keys" file on the target-server containing... (5 Replies)
Discussion started by: Hille
5 Replies

2. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

3. BSD

PF troubles on OpenBSD 5.0

I am setting up a system as an ADSL gateway. ADSL is working fine. PF is not forwarding for some reason. # ifconfig lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33196 priority: 0 groups: lo inet6... (0 Replies)
Discussion started by: John Tate
0 Replies

4. Shell Programming and Scripting

awk and tr troubles

I am trying to make all the fields containing lower case letters upper case and the third field of a file display ** instead. I have this: awk '{printf "%s %s ** %d %d\n", $1, $2, $4, $5}' database.txt | tr '' '' < database.txt And that only changes it to upper case, other... (4 Replies)
Discussion started by: Bungkai
4 Replies

5. Shell Programming and Scripting

Encoding troubles

Hello All I have a set of files, each one containing some lines that follows that regex: regex='disabled\,.*\,\".*\"'and here is what file says about each files: file <random file> <random file> ASCII text, with CRLF line terminatorsSo, as an example, here is what a file ("Daffy Duck - The... (3 Replies)
Discussion started by: tukuyomi
3 Replies

6. HP-UX

cron troubles

I have a cronjob that I need to run everyday and it needs to have todays date inputed, here is what I have, but is not working as expected.......... 23 02 * * * cd /path;./RequestSummaryReport.sh $(date +%Y-%m-%d) the output from mail gives me............. Date: Fri, 8 Feb 2008 02:12:07... (4 Replies)
Discussion started by: theninja
4 Replies

7. UNIX for Dummies Questions & Answers

ssh2 troubles

I'm trying to set up a secure and trusted connection between 2 boxes running solaris using ssh2. I've run ssh-keygen2 on the local box and on the remote box, created the identification file ( IdKey id_dsa_2048_a ) on the local machine and copied across the public key file from the local to... (5 Replies)
Discussion started by: PaulC
5 Replies

8. UNIX for Dummies Questions & Answers

Password Troubles

I'm very new to UNIX (I just started working with Terminal 2 days ago) and I don't know the system very well. I'm having trouble whenever I am asked for a password. I simply... can't type. I press keys on the keyboard but no characters appear on the screen. For example, when I log onto... (5 Replies)
Discussion started by: alexmiller
5 Replies

9. Programming

Troubles with HPUX

Hello I created an application in c language for HP-UX operative system,and it runs on a 32 bits PARISC processor. My problem is that I have to run this same application but now in a 64 bits Parisc processor. But I am not able to compile the application with the 64 bit server, and I only could use... (1 Reply)
Discussion started by: masterboy6666
1 Replies

10. UNIX for Dummies Questions & Answers

compariosn troubles...

Hi Guys, I am trying to compare using if, but keep getting some strange results. if ; then keeps creating the file 1 if ; then does not work at all if ; then does not work if ; then does not work if ; then does not work eihter. I am using a ksh, on Solaris (9 Replies)
Discussion started by: jagannatha
9 Replies
Login or Register to Ask a Question