1

I want to modify the value of a System Variable of a remote server .

The IP of server is 172.16.22.53.

The server runs Windows OS,and Cygwin instlled in it.In /cygdrive/c/cygwind/home/sikaiwei/.bashrc file(in Windows),I created a new System Variables and definded it.And I want the Variable to be a number,so I can change its value freely using ssh in my bash script . I try it ,but the system variable seem be a string instead of a number.

[sikaiwei@login-1-2 ~]$ $ORANGE
-bash: 1: command not found
[sikaiwei@login-1-2 ~]$ export ORANGE=1+$ORANGE
[sikaiwei@login-1-2 ~]$ $ORANGE
-bash: 1+1: command not found
[sikaiwei@login-1-2 ~]$

Like that,I defined a system variable $ORANGE to 1.Then 1 plus it to get 2,but I got '1+1' instead of '2'!

@skwllsp @Thorsten Staerk ,thanks! Your ways work well both. But if I ues sshpass like this :

[sikaiwei@login-1-2 v1.4]$ sshpass -p 'CHUN~19900405' ssh 172.16.22.53 -l sikaiwei     "getId=$ORANGE;echo $ORANGE;let ORANGE=$ORANGE+1;echo $ORANGE;echo 'getId is'$getId;"
1
1
getId is

But the result should like this:

1
2
getId is 1

What happened?

I try this:

[sikaiwei@login-1-2 v1.4]$ sshpass -p 'CHUN~19900405' ssh 172.16.22.53 -l sikaiwei "getId=$ORANGE;echo $ORANGE;ORANGE=$((ORANGE+1));echo $ORANGE;echo 'getId is '+$getId;"
1
1
getId is +

and

[sikaiwei@login-1-2 v1.4]$ sshpass -p 'CHUN~19900405' ssh 172.16.22.53 -l sikaiwei "getId=$ORANGE;echo $ORANGE;((ORANGE=ORANGE+1));echo $ORANGE;echo 'getId is '+$getId;"
1
1
getId is +

I find a big error. The value of $ORANGE in

sshpass -p 'CHUN~19900405' ssh 172.16.22.53 -l sikaiwei "getId=$ORANGE;echo $ORANGE;    ((ORANGE=ORANGE+1));echo $ORANGE;echo 'getId is '+$getId;"

is not the value of the System Variable of the Server host.

It is the value of the System Variable of my Client host.

2
  • 2
    tldp.org/LDP/abs/html/untyped.html: let ORANGE=$ORANGE+1
    – user17530
    Commented Dec 18, 2013 at 12:29
  • Thanks! But if I change the variable and exit and log in again,the variable don't changed!? Can I permantly change the value of variable regardless of rebooting the computer machine just using bash command line?
    – orange
    Commented Dec 18, 2013 at 12:47

2 Answers 2

1

arithmetic operator is $(()):

tstaerk@euve31070:~$ export orange=$((1+$orange))
tstaerk@euve31070:~$ echo $orange
2 
0
1

The value of a variable is a string. Even if it was a number, 1+$ORANGE is not a number, and neither is 1+1: both are strings which, when evaluated as arithmetic expressions, yield a number.

You can use the arithmetic expression syntax to tell the shell that you want to perform arithmetic.

ORANGE=$((1+ORANGE))

Bash has an alternate syntax to perform arithmetic operations:

((ORANGE=ORANGE+1))
((ORANGE+=1))

In bash, you can also declare a variable as integer-valued. If you do that, assignments to that variable perform arithmetic expansion on the value that you assign, and signal an error if the value isn't a well-formed arithmetic expression.

declare -i ORANGE
ORANGE=1
ORANGE=1+ORANGE
1
  • Thanks!But I try them I writed just now.Because I want to modify the value of a System Variable of a remote server .The server runs Windows OS,and Cygwin instlled in it.
    – orange
    Commented Dec 19, 2013 at 1:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .