E
E
Evgeny Oleinikov2016-02-19 18:38:26
bash
Evgeny Oleinikov, 2016-02-19 18:38:26

What's wrong with the Ubnutu system information gathering script?

There is a script for collecting information about the system taken from the network.

#!/bin/bash
 
telescripts_path=/root/
 
info_web="Web-servers
-------------
$(service apache2 status)
$(service nginx status)
 
"
 
info_mysql="MySQL
-------------
$(mysqladmin ping)
$(mysqladmin status)
 
"
 
info_cpu="CPU
-------------
top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1" of 100 percents"}'
 
"
 
info_ram="RAM
-------------
free: $(free -m | grep Mem | awk '{print $4}') MB of $(free -m | grep Mem | awk '{print $2}') MB total
 
"
 
info_space="HDD
-------------
$(df -h --output=source,size,used,avail | head -n2)
"
 
text=$(printf "$info_web$info_mysql$info_cpu$info_ram$info_space")
printf '%s\n' "$text" > ${telescripts_path}/status.txt

but on startup, an error occurs in the processor data collection line:
line 21: syntax error near unexpected token `('

I could not figure out what was wrong, therefore, in order to check the rest of the script, I commented out the non-working part. The script ran but the MySQL and RAM data came up empty.
MySQL
-------------
RAM
-------------
free: MB of MB total

What is wrong in the script?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2016-02-19
@ruJumi

$( top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1" of 100 percents"}' )

check if there is a binary "in the system" more precisely in PATH, you can do this
if hash mysql 2>/dev/null; then
   echo mysql exists
fi

The last lines can be written easier
cat << EOF > /path/to/file

$info_web

$info_mysql

$info_cpu

$info_ram

$info_space

EOF

A
abcd0x00, 2016-02-21
@abcd0x00

but on startup, an error occurs in the processor data collection line:

This error is due to parentheses being omitted from double quotes.
Similar option:
[[email protected] ~]$ s="abc"def(g)"hij"
bash: syntax error near unexpected token `('
[[email protected] ~]$

You can fix it like this:
[[email protected] ~]$ s="abc'def(g)'hij"
[[email protected] ~]$

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question