Variable increment (of some sort)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable increment (of some sort)
# 1  
Old 05-24-2012
Variable increment (of some sort)

i have a variable that has more than one value. i am declaring another variable, which will have the old variable data one by one. i want to use the second variable to hold the track of all the variable it has parsed from the first one. can somebody help me how do i declare and use the second variable?

e.g.
Code:
 LIST="A:B C:D E:F"
for VAR in `print $LIST`
do
NAME=`print $VAR | cut -d ":" -f1`
RETURN_VALUE1=`some action`
echo $RETURN_VALUE1
done

i want another variable to keep track of what all LIST variable it has processed and need to display that these values were used
Moderator's Comments:
Mod Comment Please use code tags
# 2  
Old 05-24-2012
Hi

Something like this?


Code:
do
NAME=`echo $VAR | cut -d ":" -f1`
OLDLIST=$OLDLIST" "$VAR
RETURN_VALUE1=`some action`
......

OLDLIST will contain the items parsed.
# 3  
Old 05-24-2012
OLDLIST=$OLDLIST" "$VAR
are both OLDLIST and $OLDLIST going to be that variable?
# 4  
Old 05-24-2012
I don't understand the question.

OLDLIST is the name of the variable.

$OLDLIST is its contents.
# 5  
Old 05-24-2012
Code:
SERVERLIST="1:Server_1a 1:Server_1b 2:Server_2a 2:Server_2b 3:Server_3a 3:Server_3b 4:Server_4a 4:Server_4b"
RETURN_VALUE1=0


for VAR in `print $SERVERLIST`
do
SERVERNAME=`print $VAR | cut -d ":" -f1`
SERVER_NAME=`print $VAR | cut -d ":" -f2`

print "<br>BOUNCING $SERVER_NAME on $SERVERNAME<br>"
print "*******************************************<br><br>"

RETURN_VALUE1=`cmd $SERVERNAME ".././ServerBounce.ksh $SERVER_NAME"`
echo $RETURN_VALUE1
done

if [ $RETURN_VALUE1 -eq 0 ] ; then
        print "\nThe $SERVER_NAME was bounced successfully\n"
elif [ $RETURN_VALUE1 -eq 1 ] ; then
        print "\nThe $SERVER_NAME couldn't be stopped\n"
elif [ $RETURN_VALUE1 -eq 2 ] ; then
        print "\nThe $SERVER_NAME couldn't be started\n"
else
        print "\nThe $SERVER_NAME was not bounced\n"
fi
done

this is the code i am using. the called script will bounce the servers from the list.
the idea is to update this script in a webpage and those if-else can't be run and will only display the last server.
hence i want a variable that will hold the information about what all servers were parsed and increment it once the previous server was bounce and at last will print that the following servers (its content) were bounced.

Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 05-24-2012 at 02:20 PM.. Reason: na
# 6  
Old 05-24-2012
Code:
for VAR in `print $SERVERLIST`

useless use of backticks. for VAR in $SERVERLIST will do.

But you don't need to run print and cut in backticks 10 times to process one line of text either. It is extremely slow to run awk/cut/sed 10 times to process 10 single bits of text -- that's like making 10 phone calls to say 10 words. I'd just split in the shell itself:

Code:
OLDIFS="$IFS" # Save the default value of the special IFS variable for later.  It controls splitting.

SERVERLIST="1:Server_1a 1:Server_1b 2:Server_2a 2:Server_2b 3:Server_3a 3:Server_3b 4:Server_4a 4:Server_4b"

IFS=": " # Split on space, or :
        set -- $SERVERLIST # $1=1, $2=Server_1a, $3=1, $4=Server_1b, etc.
IFS="$OLDIFS" # Set splitting back to normal

VAR=""

while [ "$#" -gt 0 ] # Loop until $1, $2, ... are empty.  We remove two arguments per loop.
do
        # Append new result to VAR.  If VAR was "1" before, it'll be "1 2", etc.
        VAR="$VAR $(cmd $1 ".././ServerBounce.ksh $2")"
        shift ; shift # Get rid of $1, $2, and shift down so the list starts at $1 again.
done

# $1 will be the result from the first process, $2 is the result from the second, etc.
set -- $VAR
echo "$# results in list"
echo "First is $1"
echo "Second is $2"
...


Last edited by Corona688; 05-24-2012 at 02:37 PM..
# 7  
Old 05-24-2012
thanks for the insight. but i am still new to unix scripting.
NEWLIST=$NEWLIST" "SERVER_NAME
the above statement will be inside the for loop and will get append with new SERVER_NAME every time.
my question is how do we determine how many values NEWLIST is holding? is there any wc -l stuff we can do inside a variable?
value of NEWLIST="Server_1a Server_1b Server_2a Server_2b Server_3a Server_3b Server_4a Server_4b""
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Script to find a date variable and increment it

Hi, I have parameter file wo_location.prm which has a date variable $last_upd_date= 02032016. I need to write a unix shell script to find that variable and increment it by 1 day. The path to the file is root/dir_lc/shared/param/wo_location.prm and the variable is $last_upd_date. Any... (2 Replies)
Discussion started by: isenhiem
2 Replies

2. Shell Programming and Scripting

Increment date variable

hey guys, I need to incerement the date variable for instance echo `date '+%F %H:%M:00'` this produces 2014-08-02 20:05:00 -I will grant this to : $Datehour and need to assign 1 hr from now to $Datelasthour -the script time will be used to talk to DB system information. however... (4 Replies)
Discussion started by: mo_VERTICASQL
4 Replies

3. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

4. Shell Programming and Scripting

How to increment a string variable?

Hi All, I am new to this forum and a novice at shell script. I am trying to write a script to determine each of the NIC configured on a linux system and its speed and Duplex. I came up with the following piece of code: echo `ifconfig -a | grep eth > /home/a/nic.txt` i=`awk -F, '{print... (4 Replies)
Discussion started by: pravin883
4 Replies

5. Shell Programming and Scripting

Bash 4.0 increment variable

Hi there everyone! This is my first post so be gentle. I have a small bash script that is extracting 3 line every 3 lines. I got the AWK part but i cant do the loop part. #!/bin/bash export line=`awk 'END { print NR }' btnew` echo $line for i in {1..$line..3} #increment do echo... (2 Replies)
Discussion started by: theodorosGreece
2 Replies

6. Shell Programming and Scripting

Increment a variable in unix bash

Hello There, I have been trying to increment the value of variable to 1, 2, 3 etc. but, it displays 1 1+1 1+1+1 ..... :wall: Could anyone help out with this? for i in *.* do s=`expr $s+1` echo $s j=$i$j mv $i $j done Any help is appreciated? (24 Replies)
Discussion started by: amrutha0303
24 Replies

7. Shell Programming and Scripting

Increment of a variable

Hi All, I have a variable n that stores a number. Eg. echo $n comes out to be 120. I need to print 121 using echo command on n. Please advice. Thanks in advance !! (4 Replies)
Discussion started by: learning_skills
4 Replies

8. Shell Programming and Scripting

Increment variable stored in a file

Hi, I want to write a perl script, which will increment number stored in file. I want to do this without any file handles. I think we have to use some UNIX commands. I am not sure how to do this with file handles. Thanks, (1 Reply)
Discussion started by: solitare123
1 Replies

9. Shell Programming and Scripting

increment a Variable

hi, i want to increment a Variable but it doesnt work. here my codé COUNT=1 COUNT= 'expr $COUNT + 1' i've tried it in the prompt but it print me: expr: syntaxerror What does I make wrong? (4 Replies)
Discussion started by: cengiz
4 Replies
Login or Register to Ask a Question