LinuxQuestions.org
Help answer threads with 0 replies.
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-26-2012, 11:07 AM   #16
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037

Try adding the carriage return to awk's record separator to tell it to handle both kinds of line-ending.

Code:
awk -v RS='(\r\n|\n)' '...<expression>...'

Another hint: to check the output of a command for non-printing characters, pipe it through "cat -A". Any hidden stuff will show up as a caret-prefixed letter, with "^M" being the one for carriage return.
 
Old 04-27-2012, 12:02 AM   #17
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by David the H. View Post
Try adding the carriage return to awk's record separator to tell it to handle both kinds of line-ending.

Code:
awk -v RS='(\r\n|\n)' '...<expression>...'

Another hint: to check the output of a command for non-printing characters, pipe it through "cat -A". Any hidden stuff will show up as a caret-prefixed letter, with "^M" being the one for carriage return.
Sir you are right.
Here's what i did.

Code:
$ sh test.sh > sample.txt
")syntax error: invalid arithmetic operator (error token is "
$ cat -a sample.txt
cat: invalid option -- a
Try `cat --help' for more information.
$ cat -A sample.txt
1013^M$
I think $ is a hidden.

Here's the modified code. Thanks it worked.
Code:
getInfo()
{
Total_Memory=$(ssh -t -tq  fmgadmin@$1 "sudo /usr/sbin/xm info"  |awk -v RS='(\r\n|\n)' ' /total_mem/ {print $3}')
echo "$Total_Memory"
Count=$((Total_Memory + 1))
echo "$Count"
}
getInfo 10.180.45.13

Last edited by pinga123; 04-27-2012 at 12:05 AM.
 
Old 04-27-2012, 01:21 AM   #18
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
If you make the same adjustments to pan64's solution, you can, as he said, do away with all the extra assigning and echoes.
 
Old 04-27-2012, 05:53 AM   #19
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
The dollar sign is just the unix newline character.


Unix line ending format = the ascii line feed (LF, octal 012) character. Also called "newline". cat displays it as "$", and shell notation is "\n".

DOS line ending format = ascii carriage return plus line feed (CR+LF, octal 015+012). cat displays the CR with the caret notation "^M", and the shell notation is "\r"

So when your input has dos style line-endings, tools like sed and awk see (and remove) only the LF as the line delimiter, and the CR gets left behind at the end of the line, unseen but capable of causing all kinds of errors.



BTW, old style apple systems used to use CR only as the newline. But since OSX is unix-based, they now use LF too.

BTW2, caret notation is explained here, http://en.wikipedia.org/wiki/Caret_notation, (and see man ascii for reference). In a nutshell, add 100 to the non-printing character's octal value to get the character used to display it.

Last edited by David the H.; 04-27-2012 at 05:55 AM.
 
1 members found this post helpful.
Old 04-29-2012, 11:42 PM   #20
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by David the H. View Post
The dollar sign is just the unix newline character.


Unix line ending format = the ascii line feed (LF, octal 012) character. Also called "newline". cat displays it as "$", and shell notation is "\n".

DOS line ending format = ascii carriage return plus line feed (CR+LF, octal 015+012). cat displays the CR with the caret notation "^M", and the shell notation is "\r"

So when your input has dos style line-endings, tools like sed and awk see (and remove) only the LF as the line delimiter, and the CR gets left behind at the end of the line, unseen but capable of causing all kinds of errors.



BTW, old style apple systems used to use CR only as the newline. But since OSX is unix-based, they now use LF too.

BTW2, caret notation is explained here, http://en.wikipedia.org/wiki/Caret_notation, (and see man ascii for reference). In a nutshell, add 100 to the non-printing character's octal value to get the character used to display it.
That was very informative thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 07:56 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