TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

"So that a truncated partial download doesn't end up executing half a script"

78 pointsby obi1kenobiabout 1 year ago

12 comments

obi1kenobiabout 1 year ago
The shell script starts with the following comment:<p><pre><code> # All the code is wrapped in a main function that gets called at the # bottom of the file, so that a truncated partial download doesn&#x27;t end # up executing half a script.</code></pre>
评论 #40202654 未加载
TrianguloYabout 1 year ago
Another trick I&#x27;ve also seen is to enclose the whole script in a code block<p><pre><code> { code } </code></pre> That way, if the file is not fully loaded, the block will not end and the script will not parse
noisy_boyabout 1 year ago
A simple (not perfect) approach could be to have a comment containing a &quot;unique&quot; string on the last line and grep for it as the first check to ensure that the entire script has downloaded.<p><pre><code> #!&#x2F;usr&#x2F;bin&#x2F;env bash set -u grep -wq &#x27;^# asfewdq42d3@asd$&#x27; $0 [ $? -ne 0 ] \ &amp;&amp; echo &quot;script is not complete - re-download&quot; \ &amp;&amp; exit 1 echo &quot;script is complete&quot; # asfewdq42d3@asd</code></pre>
评论 #40206116 未加载
bombcarabout 1 year ago
I&#x27;ve seen scripts (self-extracting archives for Linux, for example) that <i>checksum themselves</i> either by some trickery, or just ignoring the first line after the shebang (which itself is the computed checksum of the rest of the file).
评论 #40202391 未加载
评论 #40203060 未加载
评论 #40202721 未加载
JonChesterfieldabout 1 year ago
This probably means you can edit the script while it&#x27;s running without it falling over confusingly. Might cargo cult this pattern - I&#x27;m very prone to editing a build.sh while it runs.
评论 #40202833 未加载
kseifriedabout 1 year ago
<p><pre><code> #!&#x2F;bin&#x2F;bash SHA512=&quot;485fe3502978ad95e99f865756fd64c729820d15884fc735039b76de1b5459d32f8fadd050b66daf80d929d1082ad8729620925fb434bb09455304a639c9bc87&quot; # This line and everything later gets SHA512&#x27;ed and put in the above line. # To generate the sha512 simply: tail -n +3 [SCRIPTNAME].sh | sha512sum check_sha512() { # Compute the SHA512 hash of the script excluding the first two lines local current_sha=$(tail -n +3 &quot;$0&quot; | sha512sum | awk &#x27;{print $1}&#x27;) # Compare the computed SHA512 hash with the predefined one if [[ &quot;$current_sha&quot; != &quot;$SHA512&quot; ]]; then echo &quot;Error: SHA512 hash does not match!&quot; exit 1 fi } # Call the function to perform the hash check check_sha512 # Rest of your script starts here echo &quot;Script execution continues...&quot; </code></pre> The idea is simple: if the first line get&#x27;s mangled (#!&#x2F;bin&#x2F;bash) the script probably won&#x27;t execute at all. If the second line gets mangled than obviously the SHA512 comparison won&#x27;t work (variable name or value).<p>Finally if the rest of the script gets mangled or truncated it won&#x27;t SDHA512 the same and it&#x27;ll cause the function to exit.<p>For bonus points you can add a check if first line of script is exactly &quot;#!&#x2F;bin&#x2F;bash&quot; as well.
评论 #40204131 未加载
deanCommieabout 1 year ago
A serious question for any Linux-heads here, no insult intended.<p>How is it possible that there are ELEVEN different possible package managers that need to be supported by an installation script like this?<p>I can understand that some divergences in philosophical or concrete requirements could lead to two, three, or four opinionated varieties, but ELEVEN?<p>Does that mean that if I want to write an app that runs on Linux I should also be seeking to support 11 package managers? Or is there something unique about tailscale that would necessitate it?<p>edit: Thank you for the responses so far, but noone has yet answered the core question: WHY are there eleven of them?
评论 #40202907 未加载
评论 #40202588 未加载
评论 #40203159 未加载
评论 #40202524 未加载
评论 #40203348 未加载
评论 #40202537 未加载
评论 #40202714 未加载
评论 #40202581 未加载
oneepicabout 1 year ago
I read TFA. Why would a truncated partial download happen and still run the script?
评论 #40202499 未加载
评论 #40202516 未加载
评论 #40202530 未加载
评论 #40202527 未加载
kazinatorabout 1 year ago
I mean, could you put the main function at the <i>top</i> of the script, so that it calls later definitions?<p>The problem is that the script could be truncated in such a way that it executes successfully. It defines a bunch of functions and then quits.<p>If you&#x27;re not checking for the success or failure of the download, you&#x27;re probably not checking for the success or failure of the script; something is just going to assume the script worked.
latchkeyabout 1 year ago
If only there was a way to transactionally run shell scripts such that if they don&#x27;t complete fully, the changes are automatically reverted.<p>Edit: cue the HN responses to use nix, and other solutions
评论 #40202375 未加载
评论 #40202374 未加载
评论 #40202482 未加载
Arcuruabout 1 year ago
...isn&#x27;t that a common pattern? I&#x27;m pretty sure most install scripts I download and run already do that, though I don&#x27;t run those very often.
yau8edq12iabout 1 year ago
Don&#x27;t pipe curl&#x2F;wget a script to a shell without reading what you&#x27;ve downloaded. This should be common sense. Do `wget $url; most install.sh` and <i>only if you&#x27;re satisfied with what you read</i>, execute `sh install.sh`.
评论 #40202472 未加载
评论 #40202266 未加载
评论 #40202229 未加载
评论 #40202217 未加载
评论 #40202575 未加载
评论 #40202410 未加载
评论 #40202608 未加载
评论 #40202421 未加载
评论 #40203058 未加载