0

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?

7
  • What do you mean by "call the variable"? Calling is something you do with functions, not variables. Commented Sep 11, 2018 at 13:50
  • From your output it looks like swUpdateCheck contains a number of spaces followed by 3. What's the problem? Commented Sep 11, 2018 at 13:51
  • 1
    You aren't doing anything wrong. wc -l is padding its output, but leaving the expansion unquoted in echo $swUpdateCheck effectively strips the padding. Commented Sep 11, 2018 at 13:52
  • The additional spaces are being added by the wc command. I suggest you collapse your pipeline and do everything in awk. It can keep a counter in a variable that you print in your END block. Alternately, use printf to format your output instead of displaying it with echo. Commented Sep 11, 2018 at 13:52
  • @melpomene The solution from @Kamil Cuk helped me with my problem. And sorry if I caused any confusion by saying "called", that was my German thinking... @chepner I assumed that wc -l was causing it but was unsure about it. Thanks for the hint Commented Sep 11, 2018 at 14:34

1 Answer 1

2

swUpdateCheck has spaces in it, not just 3. As you are not escaping the variable in echo $swUpdateCheck spaces get reinterpreted and 3 get's printed out. If you enclose the variable in ", as in echo ".... $swUpdateCheck ..." spaces will not be ignored. Observe the output with set -x:

$ set -x
$ echo
+ echo

$ swUpdateCheck="       3"  # assigment with spaces in it
+ swUpdateCheck='       3'
$ echo           3  # leading spaces get ignored
+ echo 3
3
$ echo $swUpdateCheck  # just printing '3', this is the same as above line
+ echo 3
3
$ echo "$swUpdateCheck"  # with "
+ echo '       3'
       3
$ swUpdateCheck=${swUpdateCheck// /}  # remove spaces using bash or use sed 's/ //g' or tr -d ' ' or other
+ swUpdateCheck=3
$ echo "$swUpdateCheck"   # now swUpdateCheck is without spaces, it will work
+ echo 3
3
$ echo "There are $swUpdateCheck Updates available."
+ echo 'There are 3 Updates available.'
There are 3 Updates available.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.