Shell command to sum integers, one per line?

Bit of awk should do it? awk ‘{s+=$1} END {print s}’ mydatafile Note: some versions of awk have some odd behaviours if you are going to be adding anything exceeding 2^31 (2147483647). See comments for more background. One suggestion is to use printf rather than print: awk ‘{s+=$1} END {printf “%.0f”, s}’ mydatafile

How to read a file into a variable in shell?

In cross-platform, lowest-common-denominator sh you use: #!/bin/sh value=`cat config.txt` echo “$value” In bash or zsh, to read a whole file into a variable without invoking cat: #!/bin/bash value=$(<config.txt) echo “$value” Invoking cat in bash or zsh to slurp a file would be considered a Useless Use of Cat. Note that it is not necessary to … Read more

How can I parse a YAML file from a Linux shell script?

Here is a bash-only parser that leverages sed and awk to parse simple yaml files: function parse_yaml { local prefix=$2 local s=”[[:space:]]*” w='[a-zA-Z0-9_]*’ fs=$(echo @|tr @ ‘\034’) sed -ne “s|^\($s\):|\1|” \ -e “s|^\($s\)\($w\)$s:$s[\”‘]\(.*\)[\”‘]$s\$|\1$fs\2$fs\3|p” \ -e “s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p” $1 | awk -F$fs ‘{ indent = length($1)/2; vname[indent] = $2; for (i in vname) {if (i > indent) … Read more

ExitCodes bigger than 255, possible?

Using wait() or waitpid() It is not possible on Unix and derivatives using POSIX functions like wait() and waitpid(). The exit status information returned consists of two 8-bit fields, one containing the exit status, and the other containing information about the cause of death (0 implying orderly exit under program control, other values indicating that … Read more