I am currently working on a Mac OS X software update check script/plugin for Nagios, so that I can monitor my Mac Mini and Mac Pro machines, whether they have any updates available or not.
I created a variable called swUpdateCheck and assigned the check command to it, including some grep and awk, so that I can work with the output at the end. Since I don't always want to call the os update command, I just exported the output to a text file update.txt and am currently using the text file for building the final script.
This is my variable:
swUpdateCheck=`cat update.txt | grep -B1 recommended | grep -v recommended | awk '{gsub(/^[[:cntrl:][:space:]]+|^\-+|\*\s/,"");}NF>0 1' | wc -l`
Content of the Text file:
* Security Update 2018-004-10.12.6
--
* Safari11.1.2Sierra-11.1.2
--
* iTunesX-12.8
Now my issue is, that whenever I call the variable it scans through the file and should give me a number of lines at the end when I echo it. It does give me a number when using a simple echo but as soon as I combine the variable with a string, it adds spaces in front of the number and I don't understand why.
Normal echo of swUpdateCheck:
$ echo $swUpdateCheck
3
Echo swUpdateCheck in a string:
$ echo "There are $swUpdateCheck Updates available."
There are 3 Updates available.
What am I missing or doing wrong?
swUpdateCheckcontains a number of spaces followed by3. What's the problem?wc -lis padding its output, but leaving the expansion unquoted inecho $swUpdateCheckeffectively strips the padding.wccommand. I suggest you collapse your pipeline and do everything in awk. It can keep a counter in a variable that you print in yourENDblock. Alternately, useprintfto format your output instead of displaying it withecho.wc -lwas causing it but was unsure about it. Thanks for the hint