24

How can I copy the numeric value in a variable to another variable in bash script. If this were C, I would do

int a=0;
int b;
a=b;

I am trying to do this:

if [ $countip -gt $totalip ]; 
then
    $countip -eq $srctip # <-- My problem is here!
    echo $srctip
fi
0

1 Answer 1

32

Just say

countip=$srctip

This is how assignment works in bash. This will set countip to the value of srctip. If you want to assign srctip then just write

srctip=$countip

Based on the comments below this looks like the one you want.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for replying, i`m obtaining allways 0 in $srctip.
@gomesg are you trying to assign to srctip? Reverse the variables: srctip=$countip.
I`m trying to get the highest value of countip and copy it to $srctip. This is what iàm trying to do do. I am sory i am new to bash script. I was for the last 15 years a telecom tec and now i have to admin an entire linux based lan!
for srcip in cat /var/log/messages | grep "WACSLAW1 CRITICAL INCOMING" | awk '{ print $14 }'|grep -v 192.168.1. |grep -v IN=eth1 |grep -v MAC;do if (! grep "$srcip" /var/wacstemp/ids.tmp > /dev/null) ; then countip=0 echo $srcip >> /var/wacstemp/ids.tmp else countip=expr $countip + 1 if [ $countip -gt $totalip ]; then countip=$srctip echo $srctip fi fi
Might be better to update your original post if the problem hasn't been solved yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.