Reputation: 1263
I am new to coding in the shell and recently made the switch to zsh
(not sure if there are syntax differences between bash
& zsh
) and I am trying to write a function that will print out the versions of packages & tooling on my machine. The below is what I have thus far with the output of foo --version
commented out.
get_versions() {
local brew_version=$(brew --version 2>/dev/null)
# Homebrew 1.3.6
# Homebrew/homebrew-core (git revision 8b18; last commit 2017-11-10)
local create_react_app_version=$(create-react-app --version 2>/dev/null) # 1.4.3
local elixir_version=$(elixir --version 2>/dev/null)
# Erlang/OTP 20 [erts-9.1.3] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
#
# Elixir 1.5.2
local git_version=$(git --version 2>/dev/null) # git version 2.15.0
local go_version=$(go version 2>/dev/null) # go version go1.9.2 darwin/amd64
local hugo_version=$(hugo version 2>/dev/null) # Hugo Static Site Generator v0.30.2 darwin/amd64 BuildDate: 2017-11-10T10:06:36-06:00
local mongo_version=$(mongo --version 2>/dev/null)
# MongoDB shell version v3.4.10
# git version: 078f28920cb24de0dd479b5ea6c66c644f6326e9
# OpenSSL version: OpenSSL 1.0.2m 2 Nov 2017
# allocator: system
# modules: none
# build environment:
# distarch: x86_64
# target_arch: x86_64
local node_version=$(node --version 2>/dev/null) # v9.1.0
local now_version=$(now --version 2>/dev/null) # 8.4.0
local npm_version=$(npm --version 2>/dev/null) # 5.5.1
local nvm_version=$(nvm --version 2>/dev/null) # 0.33.6
local psql_version=$(psql --version 2>/dev/null) # psql (PostgreSQL) 10.1
local yarn_version=$(yarn --version 2>/dev/null) # 1.3.2
local zsh_version=$(zsh --version 2>/dev/null) # zsh 5.4.2 (x86_64-apple-darwin17.0.0)
# echo "brew:----------------- ${brew_version:9}"
echo "create-react-app:----- ${create_react_app_version}"
echo "elixir:--------------- ${elixir_version:130}"
echo "git:------------------ ${git_version:12}"
# echo "go:------------------- ${go_version:13}"
# echo "hugo:----------------- ${hugo_version:28}"
# echo "mongo:---------------- ${mongo_version:23}"
echo "node:----------------- ${node_version:1}"
echo "now:------------------ ${now_version}"
echo "npm:------------------ ${npm_version}"
echo "nvm:------------------ ${nvm_version}"
echo "psql:----------------- ${psql_version:18}"
echo "yarn:----------------- ${yarn_version}"
# echo "zsh:------------------ ${zsh_version:4}"
}
My question is in regards to how to deal with trailing strings I do not want in the output. I figured out that using :
with an integer acts like substring()
from JavaScript yielding the result starting at the specified index of the string.
For example mongo
is quite verbose in its output. Currently calling mongo_version:23
will get me to the version number, but I am still being left with a lot of strings I don't want in my customized output.
The attached images show what the current output looks like when the variables that have the trailing strings.
Upvotes: 0
Views: 536
Reputation: 285
To deal with the multi-line output from mongo, you could take advantage of the builtin read
:
Read one line and break it into fields using the characters in $IFS as separators, except as noted below. The first field is assigned to the first name, the second field to the second name, etc., with leftover fields assigned to the last name. If name is omitted then REPLY is used for scalars and reply for arrays.
function show_mongo_version() {
read -r
print "mongo version: $REPLY"
}
Since we just want to store the line read as is, this code leverages read
s behavior of storing the line read into a single scalar REPLY
when read
is not given any parameter names. Also, while not strictly necessary for your example, the code further reduces the potential post processing zsh will perform on the data that is read with -r
(raw).
Test it out with:
show_mongo_version <<-'EOF'
MongoDB shell version v3.4.10
git version: 078f28920cb24de0dd479b5ea6c66c644f6326e9
OpenSSL version: OpenSSL 1.0.2m 2 Nov 2017
allocator: system
modules: none
build environment:
distarch: x86_64
target_arch: x86_64
EOF
to get:
mongo version: MongoDB shell version v3.4.10
Or, if you just want the actual version string, leverage the usual split on whitespace behavior, and store the line in an array, then pick out the bits you want:
function show_mongo_array() {
read -rA
print "mongo version: $reply[4]"
}
to get:
mongo version: v3.4.10
Upvotes: 0
Reputation: 17169
If you are ok with using the ${var:offset}
syntax, you can use the ${var:offset:length}
syntax, e.g.
echo ${mongo_version:23:6}
should print
3.4.10
Upvotes: 2