0
##!/bin/bash
set -e
cat << EOF > file1
1
2
3
EOF
cat << EOF > file2
1
2
3
4
EOF
a=$(grep -v -F -x -f file1 file2)
echo 'aa'
b=$(grep -v -F -x -f file2 file1)
echo 'bb not run'

When I run above bash script sudo bash ./***.sh,b=$(grep -v -F -x -f file2 file1) cause script stop.
How to fix this problem?

kittygirl
  • 2,255
  • 5
  • 24
  • 52
  • What is purpose of `set -e`? – anubhava Jul 07 '21 at 18:54
  • You are getting non-zero exit code from `grep` hence `set -e` is causing script to exit. – anubhava Jul 07 '21 at 18:55
  • `grep` returns a nonzero exit status if it doesn't match anything. `set -e` makes the script stop if any of the commands return a nonzero status. – Barmar Jul 07 '21 at 18:55
  • There should be a single `#` at the beginning of the shebang. I can see no reason to use `sudo` to run this. – tripleee Jul 07 '21 at 18:57
  • @tripleee,as cannot log in root,I always `sudo` – kittygirl Jul 07 '21 at 19:02
  • @anubhava,if some typo mistake,`set -e` can notice the error. – kittygirl Jul 07 '21 at 19:03
  • I must `set -e`,any solution to this question? – kittygirl Jul 07 '21 at 19:05
  • 1
    @kittygirl, `set -e` reduces reliability, it doesn't increase it. See [BashFAQ #105](http://mywiki.wooledge.org/BashFAQ/105) for a detailed discussion (below the parable at the top there's a set of exercises). Anyone who claims to be an "expert" and is telling you to use `set -e` is not. – Charles Duffy Jul 07 '21 at 19:12
  • 2
    @kittygirl, ...that said, for any command where you want `set -e` to not exit, put `||:` on the end; so, f/e, `a=$(grep -v -F -x -f file1 file2) ||:`. – Charles Duffy Jul 07 '21 at 19:13
  • https://www.in-ulm.de/~mascheck/various/set-e/ is also worth reading -- it goes into how different shells implement `set -e` in slightly different ways (meaning that you're getting reduced reliability and portability when depending on it). That's also true in that `set -e` behavior is different between different _versions_ of bash, so it exposes you to version-specific bugs. – Charles Duffy Jul 07 '21 at 19:15
  • @CharlesDuffy,thanks for your comments.If I want bash quit when error occur,any alternative to `set -e`? – kittygirl Jul 07 '21 at 19:19
  • Nothing here needs any privileges; you don't need either of `root` or `sudo` to run this script. – tripleee Jul 07 '21 at 19:32
  • Charles [already told you the alternative,](https://stackoverflow.com/questions/68291677/grep-cause-bash-script-stop?noredirect=1#comment120695715_68291677) as do the answers to the duplicate question. In case you missed it, if `grep` can fail but you don't want that to cause the script to terminate, use `grep || true` or equivalently `grep || :` or, in more longhand, `if ! grep then; true; fi` – tripleee Jul 07 '21 at 19:35
  • ...whereas if you _want_ the shell to exit after a nonzero exit status from a given command, put `|| exit` after that individual command. (That a "nonzero exit staus" and an "error" are not exactly the same thing is part of why `set -e` is unreliable: it assumes that all nonzero exit statuses are errors -- except where they're "checked" and it ignores them, a list of exceptions that varies between versions -- but what you have here is a `grep` that's emitting a nonzero status even when that doesn't really reflect an error as such). – Charles Duffy Jul 07 '21 at 19:40

0 Answers0