LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-17-2017, 10:54 AM   #1
UBuilditIBreakit
LQ Newbie
 
Registered: Oct 2017
Posts: 3

Rep: Reputation: Disabled
Question Unknown Operand error


Hi everyone,

I am new to this forum and to Linux scripting.

I have this piece of code in stats.sh file:

#! /bin/sh
printf "Memory\t\tDisk\t\tCPU\n"
time=3600
end=$((SECONDS+$time))
while [ $SECONDS -lt $end ];
do
MEMORY=$(free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }')
DISK=$(df -h | awk '$NF=="/"{printf "%s\t\t", $5}')
CPU=$(top -bn1 | grep load | awk '{printf "%.2f%%\t\t\n", $(NF-2)}')
echo "$MEMORY$DISK$CPU"
sleep 5
done

Running the command # sh ./stats.sh >> stats.log returns the following message: sh: 3600: unknown operand

If I run:

# time=3600
# end=$((SECONDS+$time))
# echo $end
I get 3600

Any idea as to why when ran from the script it gives me that message?

Thanks in advance for your help.
 
Old 10-17-2017, 08:01 PM   #2
ShadowCat8
Member
 
Registered: Nov 2004
Location: San Bernardino, CA
Distribution: Gentoo, Arch, (RedHat4.x-9.x, FedoraCore 1.x-4.x, Debian Potato-Sarge, LFS 6.0, etc.)
Posts: 258

Rep: Reputation: 52
Greetings,

How about defining (READ: initializing) the time variable like this?:
Code:
time="3600"
HTH. Let us know.

Last edited by ShadowCat8; 10-17-2017 at 08:02 PM.
 
Old 10-17-2017, 09:19 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,507

Rep: Reputation: 4114Reputation: 4114Reputation: 4114Reputation: 4114Reputation: 4114Reputation: 4114Reputation: 4114Reputation: 4114Reputation: 4114Reputation: 4114Reputation: 4114
Welcome to LinuxQuestions.

What linux distribution/version are you running?

The reason for error is dependent on the shell. While the code you posted should work (although may not be as expected) in bash that isn't necessarily true using using /bin/sh.

What is the output of the command
ls -l /bin/sh
 
Old 10-18-2017, 07:52 AM   #4
UBuilditIBreakit
LQ Newbie
 
Registered: Oct 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
This is the outcome of the ls -l /bi/sh: /bin/sh -> busybox
 
Old 10-18-2017, 08:06 AM   #5
UBuilditIBreakit
LQ Newbie
 
Registered: Oct 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ShadowCat8 View Post
Greetings,

How about defining (READ: initializing) the time variable like this?:
Code:
time="3600"
HTH. Let us know.
Change made in bold but I still get message: sh: 3600: unknown operand

#! /bin/sh
printf "Memory\t\tDisk\t\tCPU\n"
time="3600"
end=$((SECONDS+$time))
while [ $SECONDS -lt $end ];
do
MEMORY=$(free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }')
DISK=$(df -h | awk '$NF=="/"{printf "%s\t\t", $5}')
CPU=$(top -bn1 | grep load | awk '{printf "%.2f%%\t\t\n", $(NF-2)}')
echo "$MEMORY$DISK$CPU"
sleep 5
done
 
Old 10-18-2017, 09:57 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,507

Rep: Reputation: 4114Reputation: 4114Reputation: 4114Reputation: 4114Reputation: 4114Reputation: 4114Reputation: 4114Reputation: 4114Reputation: 4114Reputation: 4114Reputation: 4114
Is this a single board computer? Make/model?

Without knowing how busybox was compiled will have to just make some assumptions.
time is a command so it is a reserved word and can not be used as a variable.

Code:
count=0
while [ "$count" -le 3600 ]
do
  echo "$count"
  sleep 5
  let count="count+5"
done
 
Old 10-18-2017, 10:51 AM   #7
urbanwks
Member
 
Registered: Sep 2003
Distribution: Slackware64-Current, FreeBSD 12.1, Alpine 5.4, Manjaro 19, Alpine on WSL [Win10]
Posts: 194

Rep: Reputation: 213Reputation: 213Reputation: 213
Try removing the ‘$’ from before the ‘time’ var on line 4. (And remove the quotes you edited in if they’re still there)
 
Old 10-18-2017, 10:54 AM   #8
urbanwks
Member
 
Registered: Sep 2003
Distribution: Slackware64-Current, FreeBSD 12.1, Alpine 5.4, Manjaro 19, Alpine on WSL [Win10]
Posts: 194

Rep: Reputation: 213Reputation: 213Reputation: 213
Quote:
Originally Posted by urbanwks View Post
Try removing the ‘$’ from before the ‘time’ var on line 4. (And remove the quotes you edited in if they’re still there)


Also probably change the var name to something else, as michaelk suggested.
 
Old 10-18-2017, 09:44 PM   #9
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 355

Rep: Reputation: 165Reputation: 165
The $SECONDS special variable is not in the ash shell used by Busybox sh.
(It is in bash, ksh and zsh.)
Code:
while [ $SECONDS -lt $end ]          # If $SECONDS is unset becomes
while [ -lt 3600 ]               # Causes an error.

# Try /proc/uptime
cat /proc/uptime
2468962.08 10404217.63      # The first number is the system uptime in seconds

seconds=$(cut -d '.' -f1 /proc/uptime)
echo $seconds
2468962
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
yasm assembler error: invalid size for operand 1 BombaD Programming 5 09-08-2015 09:55 AM
[SOLVED] Error in program, lvalue required as left operand of assignment srinietrx Programming 1 11-13-2014 06:10 AM
chown giving operand missing error Jhinukk Linux - General 1 06-25-2012 04:45 AM
Error: operand out of range anhvuitinh Red Hat 1 04-02-2010 08:12 AM
Error in building world: warning: operand 1 missing mode? henkelarsson12 *BSD 0 04-14-2006 02:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration