how to ignore resolving $1 in awk as the positional parameter
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 #
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.