Kitz ADSL Broadband Information
adsl spacer  
Support this site
Home Broadband ISPs Tech Routers Wiki Forum
 
     
   Compare ISP   Rate your ISP
   Glossary   Glossary
 
Please login or register.

Login with username, password and session length
Advanced search  

News:

Author Topic: Code review - bash fragment  (Read 1150 times)

Weaver

  • Addicted Kitizen
  • *****
  • Posts: 9342
  • Retd sw dev; A&A; 4 ◊ 7km ADSL2; IPv6; Firebrick
Code review - bash fragment
« on: June 14, 2019, 10:54:01 PM »

I would like some criticism of the following bash fragment (for some odd reason the first hash line is not shown, I just now realise).

The purpose of the code is described later.

Code: [Select]
_t="/tmp/$$-remote-iPad-to-pi.XXXXXXXXXXXXX.bash"
printf "file_content" >"$_t"
chmod +x "$_t"
"$_t"  2>&1
rm "$_t" &

What I think I should be doing is adding the name (only) of the bash script, taken from argument 0 and Iíve seen an idiom for that, throwing away the path to just get the name but I forget. Is it basexxx something? So anyway I then also include that in the name of the temp file.

The real text of this bash script is generated at run time by an iOS Shortcuts program on the iPad which printf-escapes some text (with backslashes) and inserts that inline where I have put the placeholder file_content. It first removes all \ from the text, replacing each with a strange multi character escape which contains various extremely odd non-ascii characters, then takes all awkward characters and blackslash-prefixes them before undoing the first lot of damage replacing the weird escape with \\.

The idea of this is that the iPad generates a bash script sends it to the raspberry Pi (via ssh / IPv6)  and then runs it. On the pi as you see, a temp file is created, marked as executable and is then run. I donít want this temp file to name-clash with anything else hence the pid, but I donít think thatís enough, I would think including the script name makes things slightly better but more importantly what about including the time of tempfile creation ?
Logged

Weaver

  • Addicted Kitizen
  • *****
  • Posts: 9342
  • Retd sw dev; A&A; 4 ◊ 7km ADSL2; IPv6; Firebrick
Re: Code review - bash fragment
« Reply #1 on: June 14, 2019, 11:04:45 PM »

I note this following current_date_time="`date +s`"; but I would like a much higher res than seconds if such is even possible. I suspect that if this were ever even needed (paranoia is good) then it just possibly might let me down in one rare but significant important use case.

Am I just reinventing the wheel (badly?) about creating a unique temp file here?
Logged

petef

  • Reg Member
  • ***
  • Posts: 114
Re: Code review - bash fragment
« Reply #2 on: June 15, 2019, 05:51:03 PM »

Is the iPad-generated script particularly big? If it is not then consider avoiding a temp file. Either build a shell variable with the remote commands or pipe them.

remote_commands="$(frobnicate ...)"
ssh my_pi sh -c "$remote_commands"

frobnicate ... | ssh my_pi


Either way you still need to worry about escaping.
Logged

Weaver

  • Addicted Kitizen
  • *****
  • Posts: 9342
  • Retd sw dev; A&A; 4 ◊ 7km ADSL2; IPv6; Firebrick
Re: Code review - bash fragment
« Reply #3 on: June 15, 2019, 09:43:01 PM »

Hi petef the machine at the other end is not a *nix box but an iPad running an iOS Shortcuts framework program. I just canít remember now what it was that drove me to consider using the temp file, because itís ages since I wrote that code and I just came back to it the other day, for some reason.
Logged