LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2012, 12:15 AM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 37
Strange Error executing the script.


I have a script which is giving me error as below.
script test.sh

Code:
getInfo()
{
Total_Memory=$(ssh -ttq fmguser@$1 "sudo /usr/sbin/xm info" | grep total_mem |awk -F " " '{print $3}')
echo "$Total_Memory"
Count=`expr $Total_Memory + 1`
echo "$Count"
}
getInfo 10.180.85.65
output.
Code:
$ sh test.sh
1013
expr: non-numeric argument
I m not sure where is the issue. Please help.
 
Old 04-25-2012, 01:04 AM   #2
flamelord
Member
 
Registered: Jun 2011
Distribution: Arch Linux
Posts: 151

Rep: Reputation: 34
you might have some trailing space in $Total_memory, so expr would interpret it as non-numeric.
I think something like Count=$(expr $(echo $Total_memory) + 1) would work. (There is probably something more elegant though).
 
Old 04-25-2012, 01:25 AM   #3
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by flamelord View Post
you might have some trailing space in $Total_memory, so expr would interpret it as non-numeric.
I think something like Count=$(expr $(echo $Total_memory) + 1) would work. (There is probably something more elegant though).
Modified the script however new set of errors comming
Code:
# cat test.sh
getInfo()
{
Total_Memory=$(ssh -t -tqn  fmguser@$1 "sudo /usr/sbin/xm info" < /dev/null | grep total_mem |awk -F " " '{print $3}')
echo "$Total_Memory"
Count=`$(expr $(echo $Total_Memory) + 1)`
echo "$Count"
}
getInfo 10.180.45.13
output
Code:
$ sh test.sh
tcgetattr: Inappropriate ioctl for device
1013
test.sh: line 5: 1014: command not found
 
Old 04-25-2012, 02:10 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
What shell are you using? In bash you have the arithmetic operator, example:
Code:
Count=$((Total_Memory + 1))
or
Code:
Count=$((Total_Memory++))
The problem with your statement is that there are too many unnecessary commands substitutions. The outer one (with backticks) is responsible of the 1014: command not found error: the result of expr is 1014 and the script tries to execute it as a command.
 
Old 04-25-2012, 02:29 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
just a comment:
instead of
Code:
grep total_mem |awk -F " " '{print $3}'
you can write:
Code:
awk -F " " ' /total_mem/ {print $3}'
and also the whole function can be simplified:
Code:
ssh -ttq fmguser@$1 "sudo /usr/sbin/xm info" | awk -F " " ' /total_mem/ { print "Total memory = " $3 " , Count = " $3 + 1 }'
 
Old 04-25-2012, 02:51 AM   #6
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by pan64 View Post
just a comment:
instead of
Code:
grep total_mem |awk -F " " '{print $3}'
you can write:
Code:
awk -F " " ' /total_mem/ {print $3}'
and also the whole function can be simplified:
Code:
ssh -ttq fmguser@$1 "sudo /usr/sbin/xm info" | awk -F " " ' /total_mem/ { print "Total memory = " $3 " , Count = " $3 + 1 }'
Now most of the errors are solved but still one error is comming.

Code:
getInfo()
{
Total_Memory=$(ssh -t -tqn  fmguser@$1 "sudo /usr/sbin/xm info" < /dev/null |awk -F " " ' /total_mem/ {print $3}')
echo "$Total_Memory"
Count=$((Total_Memory + 1))
echo "$Count"
}
getInfo 10.180.45.13
output:
Code:
$ sh test.sh
tcgetattr: Inappropriate ioctl for device
1013
1014
what is tcgetattr: Inappropriate ioctl for device?
 
Old 04-25-2012, 03:01 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
http://pubs.opengroup.org/onlinepubs...tcgetattr.html
this error means you have no terminal associated with the "current" process.
I assume this is caused by "< /dev/null".
 
Old 04-25-2012, 03:55 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
As the issues have been answered, I would just add that the -F switch is not required at all seeing whitespace is the default separator used.
 
Old 04-25-2012, 05:14 AM   #9
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by pan64 View Post
http://pubs.opengroup.org/onlinepubs...tcgetattr.html
this error means you have no terminal associated with the "current" process.
I assume this is caused by "< /dev/null".
Error after removing /dev/null
Code:
getInfo()
{
Total_Memory=$(ssh -t -tq  fmguser@$1 "sudo /usr/sbin/xm info"  |awk -F " " ' /total_mem/ {print $3}')
echo "$Total_Memory"
Count=$((Total_Memory + 1))
echo "$Count"
}
getInfo 10.180.45.13
Code:
$ sh test.sh
1013
")syntax error: invalid arithmetic operator (error token is "


---------- Post added 04-25-12 at 05:15 AM ----------

Quote:
Originally Posted by grail View Post
As the issues have been answered, I would just add that the -F switch is not required at all seeing whitespace is the default separator used.
The issue is not yet resolved. agree on -F switch but the problem is somewhat different .
 
Old 04-25-2012, 05:31 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
just try my awk, here it is again:
Code:
getInfo()
{
    ssh -ttq fmguser@$1 "sudo /usr/sbin/xm info" | awk ' /total_mem/ { print "Total memory = " $3 " , Count = " $3 + 1 }'
}
you can change the print at the and as you like....
 
Old 04-25-2012, 10:20 AM   #11
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
The confused error message

")syntax error: invalid arithmetic operator (error token is "

is caused by carriage return characters in the script.
The message was meant to be seen like this

test.sh: 1013\r: syntax error: invalid arithmetic operator (error token is "\r")

where \r is a carriage return character.
Carriage returns take printing back to the beginning of the line so that whatever has been printed starts to get overprinted. Here it happens twice:-
Code:
#!/bin/bash
echo -e 'test.sh: 1013\r: syntax error: invalid arithmetic operator (error token is "\r")'

")syntax error: invalid arithmetic operator (error token is "
 
Old 04-25-2012, 11:17 PM   #12
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by Kenhelm View Post
The confused error message

")syntax error: invalid arithmetic operator (error token is "

is caused by carriage return characters in the script.
The message was meant to be seen like this

test.sh: 1013\r: syntax error: invalid arithmetic operator (error token is "\r")

where \r is a carriage return character.
Carriage returns take printing back to the beginning of the line so that whatever has been printed starts to get overprinted. Here it happens twice:-
Code:
#!/bin/bash
echo -e 'test.sh: 1013\r: syntax error: invalid arithmetic operator (error token is "\r")'

")syntax error: invalid arithmetic operator (error token is "
What is the solution?
 
Old 04-26-2012, 01:03 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
Quote:
Originally Posted by pinga123 View Post
What is the solution?
what is the problem with my awk? that should work.
Where do you execute this script? which os, which shell...?
 
Old 04-26-2012, 04:37 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
The awk will not work any better without tweaking the RS to accommodate for the alternate line endings.

Last edited by grail; 04-26-2012 at 04:39 AM.
 
Old 04-26-2012, 08:46 AM   #15
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Various ways of converting a DOS/Windows text file to Unix format are given here
http://en.wikipedia.org/wiki/Newline...sion_utilities
For example, tr can be used to remove all of the carriage returns
Code:
tr -d '\r' < test.sh > newtest.sh
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Error executing the script. pinga123 Linux - Newbie 1 06-16-2011 10:11 AM
[SOLVED] Error Executing the script. pinga123 Linux - Newbie 6 07-20-2010 01:32 PM
[SOLVED] Error Executing a script(SSH problem) pinga123 Linux - Newbie 5 07-14-2010 12:54 PM
[SOLVED] Error Executing the script. pinga123 Linux - Newbie 3 07-08-2010 10:34 PM
Executing PHP script- error message vfulco Linux - Newbie 3 07-13-2004 10:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration