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 03-12-2015, 07:17 PM   #1
raj k yadav
LQ Newbie
 
Registered: Dec 2008
Posts: 27

Rep: Reputation: 2
how to ignore resolving $1 in awk as the positional parameter


i=$1
if [[ $# -eq 1 ]]
then
ssh -qt $i 'sudo dmidecode | grep -i Manufacturer | head -1 | awk -F"," '{print $1}'| awk -F":" '{print $2}' | tee -a test-raj'
else echo "Usage: Please give server name as 1st argument"
fi


Here is the result of execution:
dxxx-sa-unixmgmt@dxxxqxx01vsat01:~/raj # sh -x prepatch-script.sh CETQN1VTUTIxxx2
+ i=CETQN1VTUTIxxx2
+ [[ 1 -eq 1 ]]
+ ssh -qt CETQN1VTUTIxxx2 'sudo dmidecode | grep -i Manufacturer | head -1 | awk -F"," {print' 'CETQN1VTUTIxxx2}| awk -F":" {print' '} | tee -a test-raj'
awk: cmd. line:1: {print
awk: cmd. line:1: ^ unexpected newline or end of string
awk: cmd. line:1: {print
awk: cmd. line:1: ^ unexpected newline or end of string
dxxx-sa-unixmgmt@dexxqxx01vsat01:~/raj #
 
Old 03-12-2015, 11:52 PM   #2
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
ssh -qt $i 'sudo dmidecode | grep -i Manufacturer | head -1 | awk -F"," '{print $1}'| awk -F":" '{print $2}' | tee -a test-raj'
Wrong use of quotes. Try this:

Code:
ssh -qt $i 'sudo dmidecode | grep -i Manufacturer | head -1 | awk -F"," "{print $1}"| awk -F":" "{print $2}" | tee -a test-raj'
 
Old 03-13-2015, 12:10 AM   #3
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
Please use [code][/code] tags when displaying code/data.

I am not sure if veerain's suggestion will work? By using double quotes you open awk up to the shell parameters of $1 and $2, at least this is how it behaves inside a shell script.
In a shell script you could escape the dollar signs and still use single quotes, or perhaps see if switching the quotes around helps:
Code:
ssh -qt $i "sudo dmidecode | grep -i Manufacturer | head -1 | awk -F, '{print $1}'| awk -F: '{print $2}' | tee -a test-raj"
That being said, you could easily reduce the awk to a single call as well which eliminate some of then additional quotes.
 
Old 03-13-2015, 09:09 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,774

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by grail View Post
In a shell script you could escape the dollar signs and still use single quotes, or perhaps see if switching the quotes around helps:
Code:
ssh -qt $i "sudo dmidecode | grep -i Manufacturer | head -1 | awk -F, '{print $1}'| awk -F: '{print $2}' | tee -a test-raj"
You would also need to protect the $1 and $2 from being expanded by the outer shell.
 
Old 03-17-2015, 08:07 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
You have better luck by putting the command in a variable. That way all the quotes get evaluated on the assignment:

Code:
cmd='sudo dmidecode | grep -i Manufacturer | head -1 | awk -F"," \'{print $1}\'| awk -F":" \'{print $2}\' | tee -a test-raj'

ssh -qt $i "$cmd"
And note that the embeded apostrophe quotes for the awk commands are escaped. This allows the leading quote (in front of the sudo) to continue to the end of the line and the trailing apostrophe quote.
 
Old 03-17-2015, 10:32 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,774

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by jpollard View Post
And note that the embeded apostrophe quotes for the awk commands are escaped. This allows the leading quote (in front of the sudo) to continue to the end of the line and the trailing apostrophe quote.
This doesn't work, \ is not an escape character inside of single quotes.

Code:
$ foo='12\'
$ echo "$foo"
12\
 
Old 03-17-2015, 10:38 AM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by ntubski View Post
This doesn't work, \ is not an escape character inside of single quotes.

Code:
$ foo='12\'
$ echo "$foo"
12\
ahh pud. messed up the quotes yet again. It's why I try to avoid nested quoting as much as possible.

Last edited by jpollard; 03-17-2015 at 10:46 AM.
 
Old 03-17-2015, 08:54 PM   #8
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
So here is some simplification of the command:
Code:
sudo dmidecode | awk -F: 'tolower($0) ~ /manufacturer/{print $2;exit}' | tee -a test-raj
This can now have a single set of quotes around the outside.
 
  


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] what is "positional parameter" ? Ribo01 Linux - Newbie 6 03-22-2011 08:41 AM
Shifting positional parameter inside a function rylphs Linux - Newbie 9 10-19-2010 01:03 PM
trouble with positional parameter $0 on bash. linux distro: ubuntu breezy linrookie Linux From Scratch 2 05-07-2006 12:22 PM
Awk - get a parameter from the command line benjalien Programming 1 01-24-2006 09:06 AM
awk positional replacement qusan Programming 2 06-09-2004 10:08 AM

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

All times are GMT -5. The time now is 07:51 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