[sf-lug] shell quoting
Alex Kleider
akleider at sonic.net
Thu Nov 9 10:35:26 PST 2017
On 2017-11-08 19:24, Michael Paoli wrote:
> Various ways, but, e.g.:
> shell variable / named parameter is set in the outer/surrounding
> shell, then, minimally, something like this (but possibly a bit
> hazardous):
>
> sudo sh -c 'echo "'\''$ap_ip'\'' library library.lan rachel
> rachel.lan" >> /etc/hosts'
> But that could be potentially rather hazardous, depending upon what
> ap_ip is set to.
> Safer would be something more like:
> sudo sh -c 'echo "'\''"$ap_ip"'\'' library library.lan rachel
> rachel.lan" >> /etc/hosts'
I just tried the above- the line added to /etc/hosts was:
'' library library.lan rachel rachel.lan
The first version above does the same.
>
> Complete explanations left as an exercise. :-)
> Hints:
> ' - shields from shell (quotes) everything, until matching '
> " - quotes, but subject to variable/parameter/command substitution (and
> perhaps a wee bit more, depending upon what shell)
> \ - when not itself otherwise quoted, quotes just the immediately
> following
> character
> '\'' within a pair of '' ends up as a literal ' - take that one
> character
> at a time parsing it through slowly to understand, e.g.:
> ''\''' - take it character by character to see what happens.
> with variable/parameter substitution, if not otherwise quoted, "words"
> are split at whitespace - sometimes that's exactly what one
> wants/needs,
> most (but not all) of the time that's not something one wants.
> And remember, "$@" is your friend (usually what one wants, among
> $*, $@, and "$@", and for completeness, "$*").
>
> Remember to take things layer-by-layer, figuring out what things "look"
> like to the shell, and what it does with them, each time shell parses
> such. Use of +x and/or -v can also be useful/informative.
>
>> From: "Alex Kleider" <akleider at sonic.net>
>> Subject: [sf-lug] sudo
>> Date: Wed, 08 Nov 2017 18:30:28 -0800
>
>>
>> I am working with a headless Raspberry Pi and would like to be able
>> to add a line to a file owned by root but would like to do this from
>> within a bash script that itself is not run with root privileges.[1]
>>
>> Here is what I have come up with so far:
>>
>>
>> #!/bin/bash
>> # File: t.sh
>>
>> export ap_ip=10.10.10.10
>>
>> sudo sh -c 'echo "$ap_ip library library.lan rachel rachel.lan" >>
>> /etc/hosts'
>>
>>
>>
>> This seems to work except for the fact that the variable ap_ip
>> does not get inserted, only the other part of the line.
>> I think it's because the quoting has not allowed the variable to
>> be passed.
>>
>> Can anyone advise how this should be done?
>> Thanks in advance.
>>
>>
>>
>> [1] It so happens that the Raspberry Pi does not ask for a password
>> when an sudo command is issued (not sure how that is- not seen it
>> on any other Linux system.)
>>
>>
>>
>> -- Alex Kleider
>> (sent from my current gizmo)
>
>
> _______________________________________________
> sf-lug mailing list
> sf-lug at linuxmafia.com
> http://linuxmafia.com/mailman/listinfo/sf-lug
> Information about SF-LUG is at http://www.sf-lug.org/<br>
> Related Information <br>
> http://www.shallowsky.com/blog/<br>
> http://explainshell.com/ <br>
More information about the sf-lug
mailing list