On Thu, Mar 23, 2017 at 07:27:19PM +0000, Jay Freeman (saurik) wrote: > > Errexit (a.k.a. set -e) is horrible, > > and you should not be using it in any new shell scripts you write. > > It exists solely for support of legacy scripts. > > Wow. For those of us who don't know this, what should we be using instead? Is > using a trap on ERR any better? Is your suggestion that || exit 1 be added to > the end of every command?
That will work, as long as you know which commands *not* to put it on. In practice, you should know which commands are critical to the script, and just put it on those. > (Potentially of mild interest is this thread on Hacker News from earlier > today, where multiple people are suggesting the usage of "set -e" along with > "set -u" and "set -o pipefile".) > > https://news.ycombinator.com/item?id=13940322 I understand why people want set -e. I really, truly do. If it did what people thought it did, I'd use it too! I use "use strict" in Perl, and in Tcl, you get behavior similar to that of "set -u" (except not broken) by default. The problem is, set -e does NOT do what people think it does. http://mywiki.wooledge.org/BashFAQ/105 has more details. set -u is not quite as bad, but it can also break scripts, just like set -e can. http://mywiki.wooledge.org/BashFAQ/112 has details on that. Let me try a story. Maybe that will help convey how things stand. Once upon a time, a man with a dirty lab coat and long, uncombed hair showed up at the town police station, demanding to see the chief of police. "I've done it!" he exclaimed. "I've built the perfect criminal-catching robot!" The police chief was skeptical, but decided that it might be worth the time to see what the man had invented. Also, he secretly thought, it might be a somewhat unwise move to completely alienate the mad scientist and his army of hunter robots. So, the man explained to the police chief how his invention could tell the difference between a criminal and law-abiding citizen using a series of heuristics. "It's especially good at spotting recently escaped prisoners!" he said. "Guaranteed non-lethal restraints!" Frowning and increasingly skeptical, the police chief nevertheless allowed the man to demonstrate one robot for a week. They decided that the robot should patrol around the jail. Sure enough, there was a jailbreak a few days later, and an inmate digging up through the ground outside of the prison facility was grabbed by the robot and carried back inside the prison. The surprised police chief allowed the robot to patrol a wider area. The next day, the chief received an angry call from the zookeeper. It seems the robot had cut through the bars of one of the animal cages, grabbed the animal, and delivered it to the prison. The chief confronted the robot's inventor, who asked what animal it was. "A zebra," replied the police chief. The man slapped his head and exclaimed, "Curses! It was fooled by the black and white stripes! I shall have to recalibrate!" And so the man set about rewriting the robot's code. Black and white stripes would indicate an escaped inmate UNLESS the inmate had more than two legs. Then it should be left alone. The robot was redeployed with the updated code, and seemed to be operating well enough for a few days. Then on Saturday, a mob of children in soccer clothing, followed by their parents, descended on the police station. After the chaos subsided, the chief was told that the robot had absconded with the referee right in the middle of a soccer game. Scowling, the chief reported this to the scientist, who performed a second calibration. Black and white stripes would indicate an escaped inmate UNLESS the inmate had more than two legs OR had a whistle on a necklace. Despite the second calibration, the police chief declared that the robot would no longer be allowed to operate in his town. However, the news of the robot had spread, and requests from many larger cities were pouring in. The inventor made dozens more robots, and shipped them off to eager police stations around the nation. Every time a robot grabbed something that wasn't an escaped inmate, the scientist was consulted, and the robot was recalibrated. Unfortunately, the inventor was just one man, and he didn't have the time or the resources to recalibrate EVERY robot whenever one of them went awry. The robot in Shangri-La was recalibrated not to grab a grave-digger working on a cold winter night while wearing a ski mask, and the robot in Xanadu was recalibrated not to capture a black and white television set that showed a movie about a prison break, and so on. But the robot in Xanadu would still grab grave-diggers with ski masks (which it turns out was not common due to Xanadu's warmer climate), and the robot in Shangri-La was still a menace to old televisions (of which there were very few, the people of Shangri-La being on the average more wealthy than those of Xanadu). So, after a few years, there were different revisions of the criminal-catching robot in most of the major cities. In some places, a clever criminal could avoid capture by wearing a whistle on a string around the neck. In others, one would be well-advised not to wear orange clothing in certain rural areas, no matter how close to the Harvest Festival it was, unless one also wore the traditional black triangular eye-paint of the Pumpkin King. Many people thought, "This is lunacy!" But others thought the robots did more good than harm, all things considered, and so in some places the robots are used, while in other places they are shunned. The end. And that is what set -e is: a set of hacks and rube-goldberg contraptions bolted on to the shell over time, as completely legitimate commands caused undesired aborts in strange edge cases. No two shells implement exactly the same set of hacks. Two different versions of the *same shell* may implement different sets of hacks. Shell syntax dates back to the early days of Unix. Error checking in the shell is just like error checking in C -- optional. The C or shell programmer must implement error checking by hand, because there is no *working* language feature that will do it for you. Not correctly, at any rate. But, if you're a certain type of person, maybe you think it's OK to use set -e as long as you remember that you have to put "|| true" after every single arithmetic command, and so on, and so on. Also remember to outlaw all whistles, and to paint black triangles around your eyes. The next robot you see might be different from the last one, and you never can tell.