LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 07-15-2012, 11:51 PM   #1
patrick295767
Member
 
Registered: Feb 2006
Distribution: FreeBSD, Linux, Slackware, LFS, Gparted
Posts: 664

Rep: Reputation: 138Reputation: 138
problem with $HOME and awk


Hi,

Suprinsingly the two codes differs in output (??)
echo '$HOME/fileb.txt' > "$HOME/file.ini"

Code:
	head -n 1 "$HOME/file.ini"  |    awk -v vk="$HOME" ' {  gsub("\$HOME",vk) ; print $0 }  '

Code:
	
		var=` head -n 1 "$HOME/file.ini" |    awk -v vk="$HOME" ' {  gsub("\$HOME",vk) ; print $0 }  ' `
echo $var
 
Old 07-16-2012, 01:36 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Well as you included no input or output, it will be difficult to test / see the difference?

I would however urge you to use $() over `` when issuing more complex substitutions.
 
Old 07-16-2012, 11:45 PM   #3
patrick295767
Member
 
Registered: Feb 2006
Distribution: FreeBSD, Linux, Slackware, LFS, Gparted
Posts: 664

Original Poster
Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by grail View Post
Well as you included no input or output, it will be difficult to test / see the difference?

I would however urge you to use $() over `` when issuing more complex substitutions.
with `` it returns $HOME/ bla bla

with $() it returns /home/peterba/file.ini

why so big difference? why is it like that?
 
Old 07-17-2012, 03:08 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
You still have not indicated what is in file.ini, or at least what the first line might actually be.
Currently the code looks pointless without anymore information. Maybe you could actually advise what it is you are trying to achieve?
 
Old 07-17-2012, 04:00 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,687

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
the problem is the quote evaluation, in the second case there is one more level of quotes (backtick) which will modify how the "program" will be seen during execution.
 
Old 07-17-2012, 04:59 AM   #6
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Quote:
Code:
var=` head -n 1 "$HOME/file.ini" | awk -v vk="$HOME" ' {  gsub("\$HOME",vk) ; print $0 }  ' `
The backslash could be causing the different output.

Why is $(...) preferred over `...` (backticks)?
http://mywiki.wooledge.org/BashFAQ/082
Quote:
`...` is the legacy syntax required by only the very oldest of non-POSIX-compatible bourne-shells. There are several reasons to always prefer the $(...) syntax:

Important differences
Backslashes (\) inside backticks are handled in a non-obvious manner:
Code:
  $ echo "`echo \\a`" "$(echo \\a)"
  a \a
  $ echo "`echo \\\\a`" "$(echo \\\\a)"
  \a \\a
  # Note that this is true for *single quotes* too!
  $ foo=`echo '\\'`; bar=$(echo '\\'); echo "foo is $foo, bar is $bar" 
  foo is \, bar is \\
Code:
echo  "`echo "\$HOME"`"
/home/kenhelm

echo "$(echo "\$HOME")"
$HOME

Last edited by Kenhelm; 07-17-2012 at 05:51 AM. Reason: Added an example: echo "`echo "\$HOME"`" etc
 
Old 07-17-2012, 05:12 AM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
What does awk return?
 
  


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] I need an awk statement that will look for the home directory and comment it out. Thaidog Programming 12 04-17-2012 06:48 AM
awk error awk: line 2: missing } near end of file boscop Linux - Networking 2 04-08-2012 10:49 AM
problem with home direcotry not under /home in CentOS gabriel.560 Linux - Server 6 08-11-2011 08:31 AM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
NFS problem: "Could not chdir to home directory /home/USER: Permission denied" sumanc Linux - Server 4 07-22-2010 04:12 PM

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

All times are GMT -5. The time now is 08:45 AM.

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