A
A
Arseniy_vka2021-05-16 16:26:12
linux
Arseniy_vka, 2021-05-16 16:26:12

Bash, head and tail: Unable to open for reading?

Good day everyone.
I would be very grateful if you help.
There is a bash script to analyze those. system states, code listing:

#! /bin/bash
touch logfile
while true
do
  echo $(date) >> logfile
  var=$(uptime | tail  -n –c17 | head -n –c4)
  cpuload=$(($var*1))
  echo current cpu load is $cpuload % >> logfile 
  echo $(df  –h  /  |  grep  /) >> logfile
  temp=$(cat  /sys/class/thermal/thermal_zone0/temp)
  echo “current temp is $((temp/1000)) c” >> logfile
  echo “----------------------------------------” >> logfile
  sleep 1
done


When running it in the terminal in head and tail it gives an error: the file cannot be opened for reading.

UPD: dealt with head and tail, one error remained in line 7: ./analiz.sh: line 7: *1: syntax error: operand expected (invalid marker "*1")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xotkot, 2021-05-16
@xotkot

one error left on line 7: ./analiz.sh: line 7: *1: syntax error: expected operand (invalid token "*1")

cpuload=$(($var*1))
firstly, it can be visually simplified a little
cpuload=$[var*1]
; secondly, it swears due to the fact that the var variable is empty
, you can set a value for var if it is empty
cpuload=$[${var:=999}*1]
here, if the var variable is not set, then its value for this expression will become equal to 999

S
Saboteur, 2021-05-16
@saboteur_kiev

cpuload=$(($var*1))
Why do you even need a construction with multiplication by 1? What's the point?
Make a numeric variable? So do it like this
declare -i var
And so, most likely $var was not set for you at that moment, so it tries to multiply the void by 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question