How to Use a Variable as Integer?


 
Thread Tools Search this Thread
Operating Systems Solaris How to Use a Variable as Integer?
# 1  
Old 08-11-2011
How to Use a Variable as Integer?

hello,
i am writing a script that takes the UID from the PASSWD and then i want
to increse the Number by one. for the Next user.
i cannot get this to work that a variable is as interger
example:
Code:
set i = 0
set $i = $+1

it's in tcsh if it's mather

Last edited by fpmurphy; 08-11-2011 at 09:44 AM..
# 2  
Old 08-11-2011
use expr

Code:
i=0
i=`expr $i + 1`

don't use blank space while assigning some value to a variable.
This User Gave Thanks to donadarsh For This Post:
# 3  
Old 08-11-2011
Thanks , can maybe tell me why this command does not work
Code:
Number = tail -1 /etc/passwd | awk -F":" '{ $3 }'

i want to put the UID of a user into a variable as a number andsd the increse by 1

Last edited by fpmurphy; 08-11-2011 at 09:45 AM.. Reason: Code tags please!
# 4  
Old 08-11-2011
Code:
 
Number=`tail -1 /etc/passwd | awk -F":" '{ $3 }'`
or
Number=$(tail -1 /etc/passwd | awk -F":" '{ $3 }')

Make sure there is no space between the = and Number
# 5  
Old 08-11-2011
it does not work take a look:
Code:
bash-3.00# i=$(tail -1 /etc/passwd | awk -F":" '{ $3 }')
awk: illegal statement 603350
 record number 1
bash-3.00# echo $i

bash-3.00#

# 6  
Old 08-11-2011
Code:
 
Number=`tail -1 /etc/passwd | awk -F":" '{ print $3 }'`

This User Gave Thanks to itkamaraj For This Post:
# 7  
Old 08-11-2011
Works Like a Charm now.Thanks alot

---------- Post updated at 07:09 AM ---------- Previous update was at 05:34 AM ----------

in regards on that subject i need to use the parameters for SSH Command:
Code:
ssh -t user@server2'/usr/loca/bin/sudo useradd -u $UsrID -g 1007 -s /bin/tcsh -d $hm$fl $fl'

OUTPUT:
UsrID: Undefined variable.

why isn't the syntax ok ?

Last edited by shatztal; 08-11-2011 at 09:20 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace integer string in a variable based on month?

Hi Folks - Linux Version = Linux 2.6.39-400.128.17.el5uek x86_64 I have a process that determines the start and end load periods for an Oracle data load process. The variables used are as follows follows: They are populated like such: However, the load requires the month to be the... (11 Replies)
Discussion started by: SIMMS7400
11 Replies

2. Shell Programming and Scripting

Date/Time parts to variable as integer

Hi Guys, i guess there is a several ways to grub the strings from date and time like THISMONTH='/bin/date +%m' but the hard part is to add or sub that string to a variable i tried to use let command TWOMONTHSAGO=$THISMONTH declare -i TWOMONTHSAGO let TWOMONTHSAGO-=2 but there... (1 Reply)
Discussion started by: CyR0iz4l1v3
1 Replies

3. 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

4. Shell Programming and Scripting

Read Variable From Fille And Convert it to Integer

I read 3 variables from from Inputfile.txt the third one "startnumber" is a number when i compare it with 9 ($startnumber -le 9) it give's me a "unary operator expected", i know that -le is for number comparison. What i need is to convert $startnumber to integer (i have try to do it with expr but... (8 Replies)
Discussion started by: marios
8 Replies

5. Shell Programming and Scripting

What's the max integer a variable can hold?

I would like to know the maximum integer that a variable can hold. Actually one of my variable holds value 2231599773 and hence the script fails to process it.Do we have any other data type or options available to handle this long integers? (9 Replies)
Discussion started by: michaelrozar17
9 Replies

6. Shell Programming and Scripting

Floating point to integer in variable length lines

Hi ! I'm looking for a way to transform certain floating point numbers in a one-line, variable length file to integers. I can do this in a crude way with sed : sed -e 's/0\.\(\):/\1:/g' -e 's/0\.0\(\):/\1:/g' -e 's/1\.000:/100:/g' myfile ... but this doesn't handle the rounding correctly. ... (3 Replies)
Discussion started by: jossojjos
3 Replies

7. UNIX for Dummies Questions & Answers

Subtracting an Integer from a Variable

Hello, I am in following situation.- COUNT=`ls -l | wc -l` echo $COUNT ---> 26 NO_OF_FILES=$COUNT-1 echo $NO_OF_FILES ---> 26-1 Here, I want the output to be 25. How could I do this. It seems simple, but I am not getting it. Please help me. (2 Replies)
Discussion started by: The Observer
2 Replies

8. UNIX for Dummies Questions & Answers

Converting a String variable into Integer

Hi, I am passing a variable to a unix function. However when I try to assign the value to another variable like typeset -i I_CACHE_VAL=$2 Is this because of String to Integer conversion? I get an error. Please help me with thsi. Thanks (2 Replies)
Discussion started by: neeto
2 Replies

9. Programming

Will we get SEGV if we try to “delete []” un-initialized integer pointer variable.

I have a class with an integer pointer, which I have not initialized to NULL in the constructor. For example: class myclass { private: char * name; int *site; } myclass:: myclass(....) : name(NULL) { ..... } other member function “delete “ the variable before... (2 Replies)
Discussion started by: sureshreddi_ps
2 Replies

10. UNIX for Dummies Questions & Answers

capturing the output of grep as integer variable

Hi, I have an expression using grep and nawk that captures the ID number of a given Unix process. It gets printed to screen but I don't know how to declare a variable to this returned value! For example, ps -ef|grep $project | grep -v grep | nawk '{print $2}' This returns my number. How... (2 Replies)
Discussion started by: babariba
2 Replies
Login or Register to Ask a Question