LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to ignore resolving $1 in awk as the positional parameter (https://www.linuxquestions.org/questions/programming-9/how-to-ignore-resolving-%241-in-awk-as-the-positional-parameter-4175536583/)

raj k yadav 03-12-2015 07:17 PM

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 #

veerain 03-12-2015 11:52 PM

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'

grail 03-13-2015 12:10 AM

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.

ntubski 03-13-2015 09:09 AM

Quote:

Originally Posted by grail (Post 5331378)
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.

jpollard 03-17-2015 08:07 AM

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.

ntubski 03-17-2015 10:32 AM

Quote:

Originally Posted by jpollard (Post 5333398)
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\


jpollard 03-17-2015 10:38 AM

Quote:

Originally Posted by ntubski (Post 5333479)
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.

grail 03-17-2015 08:54 PM

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.


All times are GMT -5. The time now is 12:02 AM.