LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-13-2009, 09:38 PM   #1
jackie1
LQ Newbie
 
Registered: Jul 2009
Posts: 7

Rep: Reputation: 0
system script


Hi,
Is the script below correct to use in linux environment?
It always report:

line 8: print: command not found
line 9: [: -gt: unary operator expected
line 8: print: command not found
line 9: [: -gt: unary operator expected
line 8: print: command not found
line 9: [: -gt: unary operator expected

What's the root cause of the error report? Thanks!

____________________________________________

#!/bin/bash

users='ls -l /home/'
limit=100000

for user in $users
do
diskuse='du -s /home/$user | awk '{ print $1 } '';
if [ $diskuse -gt $limit ]
then
/usr/bin/mail $user <<!
Dear $user,

It is expected that users on this system keep their disk usage below $limit blocks. You are currently using $diskuse.

!
fi
done

____________________________________________
 
Old 08-13-2009, 09:45 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
You want to use back-ticks as a wrapper around the du | awk construct.
Or, better yet, use $( ) ... makes it clearer. The single quotes wouldn't
work in the first place, and it confuses the shell, which thinks that you
finish with awk, and then attempt some expansion in the {} ....
;}

Code:
diskuse=$(du -s /home/$user | awk '{ print $1 } ')
Cheers,
Tink
 
Old 08-14-2009, 01:18 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,412

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
You also need that around the definition of $users, otherwise you're just storing that string 'ls -l /home/' not the cmd results.

http://tldp.org/LDP/Bash-Beginners-G...ect_03_04.html

Last edited by chrism01; 08-14-2009 at 01:19 AM.
 
Old 08-19-2009, 12:52 AM   #4
jackie1
LQ Newbie
 
Registered: Jul 2009
Posts: 7

Original Poster
Rep: Reputation: 0
THANKS for your kindly help! By the way, I also change "-gt" to ">" to make it works.
 
Old 08-19-2009, 02:33 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by jackie1 View Post
THANKS for your kindly help! By the way, I also change "-gt" to ">" to make it works.
">" is a string comparison while "-gt" is a numeric comparison. Looking at your original post you want the numeric comparison. If you have to change from "-gt" to ">" then your script is not working as intended.

It is good practice to use the [[ <test expression> ]] form of test than the [ <test expression> ]. The [ <test expression> ] form is the original and has some confusing shortcomings; that is why the [[ <test expression> ]] form was introduced.

It is good practice to double quote all your variables. If you do so and the variable is unset or empty then an empty string results. If you do not then it simply disappears and this can lead to confusing syntax errors. This is what happened in your first script. The line diskuse='du -s /home/$user | awk '{ print $1 } ''; tries to run a command called print and does not find it. Hence print: command not found. Because of this error, $diskuse is not set. Because $diskuse is not set the test [ $diskuse -gt $limit ] becomes [ -gt 100000 ].

The shell tries to be helpful. Because of the [ it knows it is doing a test; it recognises -gt as a comparison operator but knows -gt compares two terms. It has been given only one (100000), so tells you that you need to use a single term (unary) test operator.

If you had followed good practice and double quoted the terms, as in [ "$diskuse" -gt "$limit" ] then it would have seen "$diskuse" as an empty string and given the more useful error message "integer expression expected".

Given that you script is probably not working as intended, please post what you have now, enclosing it in code tags (that's CODE and /CODE in [] brackets). If you click on the Advanced button you will find a # icon that automates code tags.

Last edited by catkin; 08-19-2009 at 02:34 AM.
 
Old 08-20-2009, 08:36 PM   #6
jackie1
LQ Newbie
 
Registered: Jul 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Smile system script

Hi Catkin,

Thank you so much for your kindly help! I double check my test code and find out it doesn't work properly as you said after I change "-gt" to ">". Could you help me to understand how to fix that?

Thanks,
Jack



Code:
#######################################################################
#!/bin/bash

users=$(ls /home/)
limit=1000000

for user in $users
do
   diskuse=$(du -s /home/$user | awk '{ print $1 } ');
   if  [ $diskuse -gt $limit ]

   then
         /usr/bin/mail $user <<!
Dear $user,

        It is expected that users on this system keep their disk usage below $limit blocks. You are currently using $diskuse. Please delete any unnecessary files and directories.
        I can help you archive old files if you wish. If you don't reduce your disk usage, your access to the system may be reduced. You Friendly System Administrator.

Thanks,
Admin
!
   fi
done
#######################################################################
 
Old 08-20-2009, 09:45 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,412

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
'doesn't work' isn't very informative; please give details.
Better still, add

set -xv

as the 2nd line of the file (ie after #!/bin/bash) and it'll show you exactly what its doing.

I recommend you bookmark & read these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 08-21-2009, 01:52 AM   #8
jackie1
LQ Newbie
 
Registered: Jul 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Thank you, Chris!

I'll read those documentation to understand more on shell programming.

After adding in "set -xv" into test script, the reports is as below:

[root@System trn1]# ./script.sh > 1

users=$(ls /home/)
ls /home/
++ ls /home/
+ users=trn1
trn2
trn3
limit=1000000
+ limit=1000000

for user in $users
do
diskuse=$(du -s /home/$user | awk '{ print $1 } ');
if [ $diskuse -gt $limit ]

then
/usr/bin/mail $user <<!
fi
done
du -s /home/$user | awk '{ print $1 } '
++ du -s /home/trn1
++ awk '{ print $1 } '
+ diskuse=16668832
+ '[' 16668832 -gt 1000000 ']'
+ /usr/bin/mail trn1
du -s /home/$user | awk '{ print $1 } '
++ du -s /home/trn2
++ awk '{ print $1 } '
+ diskuse=4519284
+ '[' 4519284 -gt 1000000 ']'
+ /usr/bin/mail trn2
du -s /home/$user | awk '{ print $1 } '
++ du -s /home/trn3
++ awk '{ print $1 } '
+ diskuse=9072
+ '[' 9072 -gt 1000000 ']'
+ /usr/bin/mail trn3
 
  


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
system monitoring script nkd Linux - Software 3 10-30-2008 11:15 AM
Killing my script when system down. ziyunxiao Linux - General 3 10-21-2008 12:28 PM
Script in system startup usa1234 Linux - General 2 09-21-2008 02:56 AM
How to call a script from a system startup script? jonatito Linux - Newbie 7 11-11-2005 10:40 PM
System command script RHrulz Mandriva 7 03-26-2005 12:33 PM

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

All times are GMT -5. The time now is 01:17 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration