Why does this fish while loop terminate after a single iteration? - npm

This is a fish function used to upgrade JavaScript packages for a project. Strangely, it terminates after a single iteration with an exit status of 0. Why?
function yarn-upgrade-all --description "Upgrade JavaScript packages"
yarn outdated | sed '1,/^Package/d;/^Done/d' | awk '{print $1, $4}' | while read -l PACKAGE VERSION
echo
set_color brwhite
echo -n "==>"
set_color yellow
echo -n " "$PACKAGE
set_color brblue
echo -n " "$VERSION
set_color brwhite
echo -n " <=="
set_color normal
echo
echo
yarn upgrade --latest $PACKAGE
and yarn run test
and yarn run build
and git commit -am "Upgrade to "$PACKAGE" "$VERSION
or begin
set_color red
echo "last command exited with status $status" >&2
set_color normal
return 1
end
end
end
On the other hand, this second function, which contains only a stub body, runs through all packages piped into the loop.
function yarn-upgrade-all-debug --description "Upgrade JavaScript packages"
yarn outdated | sed '1,/^Package/d;/^Done/d' | awk '{print $1, $4}' | while read -l PACKAGE VERSION
echo $PACKAGE $VERSION
end
end
fish --version
fish, version 3.0.2

You're running fish 3.0.0, and hitting https://github.com/fish-shell/fish-shell/issues/5513 - return from a while doesn't actually set the status correctly.
However, the return still causes it to terminate the while-loop.
Upgrade to 3.0.2.

The loop terminates after a single iteration because the yarn run invocations in the loop body slurp up the rest of stdin. (Credits to #glenn-jackman.)
A possible workaround is to redirect stdin to /dev/null for these commands:
and yarn run test < /dev/null
and yarn run build < /dev/null
The culprit is run-s from the npm-run-all package, which is invoked by both yarn run commands.
https://github.com/mysticatea/npm-run-all/issues/166

Related

Modify zsh commands to forward errors

I would like to modify one of my recent Bash aliases to forward errors. Here is the alias:
alias makecclip=
"make |& tee >(sed \"s,\x1B\[[0-9;]*[a-zA-Z],,g\" |
egrep \":[0-9]+:[0-9]+: error\" | cut -d : -f1,2,3 |
head -n 1 | xargs -0 echo -n | xclip -selection clipboard &&
xclip -selection clipboard -o)
This code displays the results of a C++ compilation, and then removes formatting and displays and adds to the clipboard the first error location (if there is any).
However, I would like to use this code like this:
makecclip && bin/someexecutablecreated
This though ruins the && operator, since it always runs bin/someexecutablecreated even when there is a compilation error present. How can I add modifications to the code to set the error flag, when the error list (the things saved to clipboard and echoed) is not empty?
You can address your issue by using the PIPESTATUS internal variable (this variable has other names in non-bash shells). This allows to have an history of exit statuses of commands passed by pipe.
You precised in the comments that you didn't use bash, but used zsh instead. As such, some of the syntax of my solution has to be changed, as they handle the PIPESTATUS variable differently.
In bash, you use ${PIPESTATUS[0]}, whereas you'll use ${pipestatus[1]} in zsh.
A first approach, using your existing alias, could be as follow :
makecclip && [ "${pipestatus[1]}" -eq "0" ] && echo "ok"
This runs the echo command only if "${pipestatus[1]}" is equal to 0 (no errors during make)
A more convenient solution would be to use a function instead of an alias for makecclip. In your ~/.bashrc file, you could write :
makecclip () {
make |& tee >(sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | egrep ":[0-9]+:[0-9]+: error" | cut -d : -f1,2,3 | head -n 1 | xargs -0 echo -n | xclip -selection clipboard && xclip -selection clipboard -o)
return "${pipestatus[1]}"
}
Now, makecclip && echo "ok" will work as expected.
Test cases :
#!/bin/zsh
#do not run this test if there is an existing makefile in your current directory
rm -f makefile
makecclip () {
make |& tee >(sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | egrep ":[0-9]+:[0-9]+: error" | cut -d : -f1,2,3 | head -n 1 | xargs -0 echo -n | xclip -selection clipboard && xclip -selection clipboard -o)
# this part is only present to check the pipestatus values during the tests.
# In the real function, I wrote 'return ${pipestatus[1]}' instead.
a=(${pipestatus[#]})
echo ${a[#]}
return ${a[1]}
}
echo "# no makefile"
makecclip && echo "ok"
echo -e "\n# empty makefile"
touch makefile
makecclip && echo "ok"
echo -e "\n# dummy makefile entry"
echo -e 'a:\n\t#echo "inside makefile"' > makefile
makecclip && echo "ok"
echo -e "\n# program with error makefile"
echo -e "int main(){error; return 0;}" > target.cc
echo -e 'a:\n\tgcc target.cc' > makefile
makecclip && echo "ok"
Output :
$ ./test.sh
# no makefile
make: *** No targets specified and no makefile found. Stop.
2 0
# empty makefile
make: *** No targets. Stop.
2 0
# dummy makefile entry
inside makefile
0 0
ok
# program with error
gcc target.cc
target.cc: In function ‘int main()’:
target.cc:1:12: error: ‘error’ was not declared in this scope
int main(){error; return 0;}
^
makefile:2: recipe for target 'a' failed
make: *** [a] Error 1
target.cc:1:12
2 0

How to make nvm run from within a script influence the environment of the calling shell?

When I run nvm from within a shell script, it doesn't seem to impact the environment of the calling shell:
$ node -v
v4.1.1
$ env | grep -i node
MANPATH=/home/ubuntu/.nvm/versions/node/v4.1.1/share/man:/usr/local/rvm/rubies/ruby-2.2.1/share/man:/usr/local/man:/usr/local/share/man:/usr/share/man:/usr/local/rvm/man
NVM_PATH=/home/ubuntu/.nvm/versions/node/v4.1.1/lib/node
PATH=/home/ubuntu/.nvm/versions/node/v4.1.1/bin:/usr/local/rvm/gems/ruby-2.2.1/bin:/usr/local/rvm/gems/ruby-2.2.1#global/bin:/usr/local/rvm/rubies/ruby-2.2.1/bin:/mnt/shared/bin:/home/ubuntu/workspace/node_modules/.bin:/home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/mnt/shared/sbin:/opt/gitl:/opt/go/bin:/mnt/shared/c9/app.nw/bin:/usr/local/rvm/bin
NVM_NODEJS_ORG_MIRROR=https://nodejs.org/dist
NODE_PATH=/mnt/shared/lib/node_modules
NVM_BIN=/home/ubuntu/.nvm/versions/node/v4.1.1/bin
$
$ cat test
#!/bin/bash
. ~/.nvm/nvm.sh
nvm use 0.10.40
nvm alias default 0.10.40
echo NVM_PATH=$NVM_PATH
echo MANPATH=$MANPATH
echo PATH=$PATH
echo NVM_BIN=$NVM_BIN
$ ./test
Now using node v0.10.40 (npm v1.4.28)
default -> 0.10.40 (-> v0.10.40)
NVM_PATH=/home/ubuntu/.nvm/v0.10.40/lib/node
MANPATH=/home/ubuntu/.nvm/v0.10.40/share/man:/usr/local/rvm/rubies/ruby-2.2.1/share/man:/usr/local/man:/usr/local/share/man:/usr/share/man:/usr/local/rvm/man
PATH=/home/ubuntu/.nvm/v0.10.40/bin:/usr/local/rvm/gems/ruby-2.2.1/bin:/usr/local/rvm/gems/ruby-2.2.1#global/bin:/usr/local/rvm/rubies/ruby-2.2.1/bin:/mnt/shared/bin:/home/ubuntu/workspace/node_modules/.bin:/home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/mnt/shared/sbin:/opt/gitl:/opt/go/bin:/mnt/shared/c9/app.nw/bin:/usr/local/rvm/bin
NVM_BIN=/home/ubuntu/.nvm/v0.10.40/bin
$
$ node -v
v4.1.1
$ env | grep -i node
MANPATH=/home/ubuntu/.nvm/versions/node/v4.1.1/share/man:/usr/local/rvm/rubies/ruby-2.2.1/share/man:/usr/local/man:/usr/local/share/man:/usr/share/man:/usr/local/rvm/man
NVM_PATH=/home/ubuntu/.nvm/versions/node/v4.1.1/lib/node
PATH=/home/ubuntu/.nvm/versions/node/v4.1.1/bin:/usr/local/rvm/gems/ruby-2.2.1/bin:/usr/local/rvm/gems/ruby-2.2.1#global/bin:/usr/local/rvm/rubies/ruby-2.2.1/bin:/mnt/shared/bin:/home/ubuntu/workspace/node_modules/.bin:/home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/mnt/shared/sbin:/opt/gitl:/opt/go/bin:/mnt/shared/c9/app.nw/bin:/usr/local/rvm/bin
NVM_NODEJS_ORG_MIRROR=https://nodejs.org/dist
NODE_PATH=/mnt/shared/lib/node_modules
NVM_BIN=/home/ubuntu/.nvm/versions/node/v4.1.1/bin
$
What do I need to do inside script "test" so that "node -v" will give me 0.10.40 after I run "./test" ?
Note that if I open a new terminal, and type "node -v" I get 0.10.40. But for some reason, in the shell in which I executed the "test" script I seem to be stuck with 4.1.1.
Bash scripts run in their own process context that inherits its environment from the parent process. It's not possible to change the environment of the parent. See Can a shell script set environment variables of the calling shell?
But just as your script sources nvm with . ~/.nvm/nvm.sh, you could source your script, which will execute it in the context of the parent shell:
$ node -v
v4.1.1
$ ./test
Now using node v0.10.40 (npm v2.14.8)
default -> 0.10.40 (-> v0.10.40)
NVM_PATH=/Users/william/.nvm/v0.10.40/lib/node
MANPATH=/Users/william/.nvm/v0.10.40/share/man:/Users/william/.rvm/rubies/ruby-2.1.2/share/man:/usr/local/share/man:/usr/share/man:/opt/X11/share/man:/usr/local/MacGPG2/share/man:/Users/william/.rvm/share/man:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/share/man:/Applications/Xcode.app/Contents/Developer/usr/share/man:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/share/man
PATH=/Users/william/.nvm/v0.10.40/bin:/Users/william/.rvm/gems/ruby-2.1.2/bin:/Users/william/.rvm/gems/ruby-2.1.2#global/bin:/Users/william/.rvm/rubies/ruby-2.1.2/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin:~/local/bin:~/bin:/Users/william/.rvm/bin:/Users/william/.rvm/bin:./node_modules/.bin:/usr/local/heroku/bin
NVM_BIN=/Users/william/.nvm/v0.10.40/bin
$ node -v
v4.1.1
$ source ./test
Now using node v0.10.40 (npm v2.14.8)
default -> 0.10.40 (-> v0.10.40)
NVM_PATH=/Users/william/.nvm/v0.10.40/lib/node
MANPATH=/Users/william/.nvm/v0.10.40/share/man:/Users/william/.rvm/rubies/ruby-2.1.2/share/man:/usr/local/share/man:/usr/share/man:/opt/X11/share/man:/usr/local/MacGPG2/share/man:/Users/william/.rvm/share/man:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/share/man:/Applications/Xcode.app/Contents/Developer/usr/share/man:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/share/man
PATH=/Users/william/.nvm/v0.10.40/bin:/Users/william/.rvm/gems/ruby-2.1.2/bin:/Users/william/.rvm/gems/ruby-2.1.2#global/bin:/Users/william/.rvm/rubies/ruby-2.1.2/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin:~/local/bin:~/bin:/Users/william/.rvm/bin:/Users/william/.rvm/bin:./node_modules/.bin:/usr/local/heroku/bin
NVM_BIN=/Users/william/.nvm/v0.10.40/bin
$ node -v
v0.10.40

Apache Ace as a Service

I am trying to setup/configure Apache ACE for the first time. It seems like I got everything running correctly but I would like to have ACE startup automatically when the Linux box starts. I created the init.d script below which does starts up ACE however it drops me in an OSGI console and when I leave the console ACE shuts down.
Is there a way to start ACE without having the OSGI console so I do not have to keep my session open to keep ACE running?
#! /bin/bash
BIN_DIR=/opt/ace-current/server-allinone
PATH=/bin:/usr/bin:/sbin:/usr/sbin
JAVA_HOME=/usr/lib/jvm/default-java
if [ `id -u` -ne 0 ]; then
echo "You need root privileges to run this script"
exit 1
fi
. /lib/lsb/init-functions
if [ -r /etc/default/rcS ]; then
. /etc/default/rcS
fi
case $1 in
start)
cd $BIN_DIR
java -Dorg.apache.ace.server=localhost:11868 -Dorg.osgi.service.http.port=11868 -Dorg.apache.ace.obr=localhost:11868 -jar server-allinone.jar
;;
stop)
pid=`ps aux | grep server-allinone | awk '{print $2}'`
kill -9 $pid
;;
restart)
$0 stop
$0 start
;;
esac
exit 0
java -Dgosh.args=--nointeractive -jar server-allinone.jar
Will disable the local shell.

Mixed Mode RVM giving error when used in init script

I've installed RVM in Mixed Mode and have Phusion Passenger running in stand alone mode.
I've found this init script to start my Phusion Passenger standalone server on startup: http://memcloud.com/note/show/167
Modifying only the prescribed values, it was giving me the following error, but would still run
-su: /home/myuser/.rvm/bin/rvm: No such file or directory
I ran which rvm in myuser and found out that RVM is in /usr/local/rvm/bin/rvm. So I updated the RVM variable to reflect that, and changed RVM="$USER_HOME/.rvm/bin/rvm" to RVM="/usr/local/rvm/bin/rv". Now it's giving me the following message, but it still runs.
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
Not really sure if it's a problem if the system is running, but I'd just like to be sure.
I would say this script is wrong, you should use something more like this:
#!/usr/bin/env bash
### BEGIN INIT INFO
# Provides: my-app passenger in standalone
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/stop my-app web site
### END INIT INFO
# BEGIN MINIMAL CHANGES
USER=www-data
USER_HOME=/var/www
APP_PATH=/var/www/my-app/current
GEM_SET=ruby-1.8.7-p330#my-app
ADDRESS=127.0.0.1
PORT=3000
ENVIRONMENT=production
# END MINIMAL CHANGES
RVM="/usr/local/rvm/bin/rvm"
PASSENGER="$USER_HOME/.rvm/gems/$GEM_SET/bin/passenger"
PASSENGER="cd $APP_PATH; $RVM $GEM_SET do $PASSENGER"
CMD_START="$PASSENGER start -a $ADDRESS -p $PORT -e $ENVIRONMENT -d"
CMD_STOP="$PASSENGER stop -p $PORT"
. /lib/lsb/init-functions
case "$1" in
start)
echo "Starting myapp passenger"
echo $CMD_START
su - $USER -c "$CMD_START"
;;
stop)
echo "Stopping myapp passenger"
echo $CMD_STOP
su - $USER -c "$CMD_STOP"
;;
*)
echo "Usage: $0 start|stop" >&2
exit 3
;;
esac
you could also replace GEM_SET=. to make rvm use ruby stored in .rvmrc but this requires that $USER trusted that .rvmrc ... which could be also done in this script with:
su - $USER -c "rvm rvmrc trust $APP_PATH"
called as first line in start)

Maven + Mercurial for Build Numbers

I can't figure out how to get a Mercurial revision id put into my Maven build (ideally I would like it in the MANIFEST of my jars and war).
The closest solution I could find is:
mvn -DbuildNumber=`hg id -i`
Which won't really work for Windows or my Hudson server.
Luckily Hudson tags my builds but I would like some more assurance if the builds were also tagged with the Mercurial changset id.
Have a look at this previous question and the link from the accepted answer. Basically, you want to do the same thing except that you'll want to use the buildnumber:hgchangeset goal with Mercurial to get a changeset property with the content of hg id -i.
Unfortunately, hg id -i is too long for use. I created a script that will calculate an accurate build number. However, there are two exceptions. If there was not previous release on the branch, then it cannot be valid. If there are changes in the local repo, then it cannot be valid. In my build script I mark the build as "x.x.UNSTABLE" whenever that happens.
I use a REL_PATTERN to pick up the last tag in the current branch that was marked as an actual release. Then I calculate the build number by tracking the commit log count from that release + all commits to the branch since that release.
#!/bin/bash
REL_PATTERN="release-[0-9]*\.[0-9]*\.[0-9]*"
BRANCH=$( hg branch )
CURR_REV=$( hg id -n )
if [ "${CURR_REV: -1}" = "+" ] ; then
echo "ERROR: This workspace contains uncommitted code. Cannot calculate build number" >&2
echo "UNSTABLE"
exit 1
fi
RELEASE=$( hg log --rev="branch($BRANCH) and tag() and 1:$CURR_REV" -T "{tags} {rev}\n"|grep "${REL_PATTERN} "|tail -1 )
if [ "$RELEASE" = "" ] ; then
echo "ERROR: Unable to locate version tag" >&2
echo "UNSTABLE"
exit 1
fi
RELEASE_REV=$( echo $RELEASE|cut -f 2 -d ' ' )
RELEASE_TAG=$( echo $RELEASE|cut -f 1 -d ' ' )
REVS=$( hg log -P $RELEASE_REV -b $BRANCH -T "{rev}\n"|wc -l )
BUILD=$( hg log -r1:$CURR_REV -P $RELEASE_REV -b $BRANCH -T "{rev}\n"|wc -l )
echo "BRANCH=$BRANCH" >&2
echo "CURR_REV=$CURR_REV" >&2
echo "RELEASE_REV=$RELEASE_REV" >&2
echo "RELEASE_TAG=$RELEASE_TAG" >&2
echo "BUILD=$BUILD" >&2
echo $BUILD