On 2019-12-19 09:30, Johannes Altmanninger wrote: > On Thu, Dec 19, 2019 at 01:10:46AM +0100, Marcin Zajączkowski wrote: >> Hi, >> >> Due to [1] I use "|| exit -2" to break script on a command execution >> failure in my backup script. >> >> However, I also want to have output written to a file. The following >> solution writes logs to a file, but silently ignores any failure in >> "my-backup": >>> my-backup 2>&1 | tee -a /tmp/some-log || exit -2 >> >> I tried to keep the error in a variable (to check its state later on and >> fail), but it breaks output redirection at all: >>> my-backup 2>&1 || set ERR2 -2 | tee -a /tmp/some-log || exit -3 >> >> Maybe keeping the first two commands in (logical) brackes would help, >> but () are in Fish reserved for something else. >> >> Is it possible in Fish to have a construction that allows to: >> 1. Run command and have output redirected (e.g. with "tee") also to a >> file (at least) in a case of success. >> 2. Break further commands execution in a case of failure (with "|| exit >> -2" or anything else). > > Logical brackets in fish are "begin" and "end", so you can use > > begin > my-backup 2>&1 || exit -2 > end | > tee -a /tmp/some-log || exit -3
Thanks! I completely forgot about begin and end. However, while "exit -2" breaks the execution, the script return 0. Luckily, I can set a variable there and check its value in the next statement. The nice thing is that tee works also in a case of error. > Fish 3.1.0 will have a $pipestatus variable, so you will be able to do > this instead: > > my-backup 2>&1 | tee -a /tmp/some-log || exit -3 > if test $pipestatus[1] -ne 0 > exit -2 # my-backup failed > end Nice, looking at the number of open issues and recent discussion it doesn't seem to be far away :). > That being said, if you use `|| exit $status` a lot then it's probably > easier to use a POSIX shell with for this (non-interactive) script. Yes, that's somehow painful. Waiting for #2389 to be fixed/implemented one day ;). Marcin > >> >> ? >> >> >> [1] - https://github.com/fish-shell/fish-shell/issues/2389 >> >> Marcin >> >> -- >> https://blog.solidsoft.info/ - Working code is not enough _______________________________________________ Fish-users mailing list Fish-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/fish-users