Script has no result!! -gt -lt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script has no result!! -gt -lt
# 1  
Old 11-22-2015
Script has no result!! -gt -lt

Hello,

Result of scripts are emty. If statements are not working but why?

What is right line?
1-
Code:
#!/bin/ksh
up=`uptime |awk '{print$3}'`
hostname=`hostname`
if [[ $up -gt 365 ]] && [[$up -lt 730 ]]; then
echo "$hostname | $up |  Running over than one year."
fi
exit 0

2-
Code:
#!/bin/ksh
up=`uptime |awk '{print$3}'`
hostname=`hostname`
#if [[ $acik -gt 365 ]] && [[$acik -lt 730 ]]; then
if [[ "$(echo $up)" -gt "365" && "$(echo $up)" -lt "730" ]]; then
echo "$hostname | $up |  Running over than one year."
fi
exit 0

3-
Code:
#!/bin/ksh
up=`uptime |awk '{print$3}'`
hostname=`hostname`
if [[ $up -gt 365 && $up -lt 730 ]]; then
echo "$hostname | $up |  Running over than one year."
fi
exit 0


Last edited by Don Cragun; 11-23-2015 at 05:02 AM.. Reason: Change QUOTE tags back to CODE tags.
# 2  
Old 11-22-2015
There MUST be spaces between the brackets and condition. Right now there is not.
Code:
if [[ $up -gt 365 ]] && [[ $up -lt 730 ]]; then

Do not know what you are try to accomplish here:
Code:
echo "$hostname | $up |  Running over than one year."

This User Gave Thanks to Aia For This Post:
# 3  
Old 11-22-2015
Assuming that awk '{print$3}' doesn't produce a syntax error with the version of awk you're using, the variable up will be set to a number. For example, if the output of uptime is:
Code:
16:18  up 32 days, 47 mins, 10 users, load averages: 2.60 1.95 1.83

up will be set to 32.

How long has your system been up? None of your scripts should not produce any output unless your system has been up more than 365 days AND has been up less than 730 days. And, since acik is not set to any value in your 2nd script, that script will never produce any output unless acik is set to a value and exported from the shell execution environment used to invoke your script and it is set in that environment to a value x such that 365 < x < 730.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 11-22-2015
Quote:
Originally Posted by Aia
There MUST be spaces between the brackets and condition. Right now there is not.
Code:
if [[ $up -gt 365 ]] && [[ $up -lt 730 ]]; then

Do not know what you are try to accomplish here:
Code:
echo "$hostname | $up |  Running over than one year."

Hi Aia,
With test expression and with [ expression ], you are absolutely correct; the elements of expression and the command name must be separated by spaces. But since [[ and ]] are part of the grammar of the shell (not built-in utilities), spaces are not required in these scripts.

The echo seems pretty obvious to me (although extremely misleading). I would expect that a system that had been up for three years to print that line, but the code shown in scripts 1 and 3 will not print that line if the system has been up for 730 or more days. And, of course, script 2 might or might not print that line, but whether or not that line is printed has nothing to do with how long the system has been up.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 11-22-2015
Code:
[aia@ludus]$ cat test.sh
#!/usr/bin/ksh

set -x

number=3
if [[ $number -gt 1 ]] && [[$number -lt 4 ]]; then
    echo "$number"
fi
[aia@ludus]$ ./test.sh
+ number=3
+ [[ 3 -gt 1 ]]
+ '[[3' -lt 4 ']]'
./test.sh[6]: [[3: not found [No such file or directory]


Last edited by Aia; 11-22-2015 at 09:48 PM..
This User Gave Thanks to Aia For This Post:
# 6  
Old 11-22-2015
Quote:
Originally Posted by Aia
Code:
[aia@ludus]$ cat test.sh
#!/usr/bin/ksh

set -x

number=3
if [[ $number -gt 1 ]] && [[$number -lt 4 ]]; then
    echo "$number"
fi
[aia@ludus]$ ./test.sh
+ number=3
+ [[ 3 -gt 1 ]]
+ '[[3' -lt 4 ']]'
./test.sh[6]: [[3: not found [No such file or directory]

Right... Sorry about that.

So, why is the OP reporting that the results are empty instead of reporting that there are diagnostics from ksh?
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 11-23-2015
Thank you for your responce. I changed $number instead of $up. I don't know why but $up or $up1 is not working. $number is working.

Code:
#!/bin/ksh

number=`uptime |awk '{print$3}'`

if [[ $number -gt 1 ]] && [[ $number -lt 365 ]]; then
    echo "`hostname` $number running about 1 year."
fi

if [[ $number -gt 365 ]] && [[ $number -lt 730 ]]; then
    echo "`hostname` $number running over than 1 year."
fi

if [[ $number -gt 730 ]] && [[ $number -lt 1095 ]]; then
    echo "`hostname` $number running over than 2 years."
fi

if [[ $number -gt 1095 ]] && [[ $number -lt 1460 ]]; then
    echo "`hostname` $number running over than 3 years."
fi

if [[ $number -gt 1460 ]] && [[ $number -lt 1825 ]]; then
    echo "`hostname` $number running over than 4 years."
fi

exit 0


Last edited by Don Cragun; 11-23-2015 at 05:04 AM.. Reason: Change QUOTE tags to CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to compare the current result with previous line result.?

Hi Gurus, I have requirement to compare current result with previous reuslt. The sample case is below. 1 job1 1 1 job2 2 1 job3 3 2 job_a1 1 2 job_a2 2 2 job_a3 3 3 job_b1 1 3 job_b2 2 for above sample file, GID is group ID, for input line, the job run... (1 Reply)
Discussion started by: ken6503
1 Replies

2. Programming

Query result from shell script

Hi, I wrote the below script to get the query result from a Syabase DB. isql -s -U **** -P **** SYBASE SERVERNAME USE ***(Database name in Sybase) @command.sql But im not getting the output. Can anyone help me on this (2 Replies)
Discussion started by: rohan G
2 Replies

3. Shell Programming and Scripting

Script to write result to a file

Hello, How can I run this script every 1 hour and save its result to result.txt ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' Regards Shaan (5 Replies)
Discussion started by: Shaan_Shaan
5 Replies

4. Shell Programming and Scripting

Script not spooling in result file

Hi everyone and nice to meet you :) I'm having some issues with a script I'm writing. It's probably most chaotic, I'm no ksh guru, but the idea is to extract an ID with that query, spool it into a file, and read that file making the ID a variable. This has to be done for every row extracted by... (10 Replies)
Discussion started by: Arkadia
10 Replies

5. Shell Programming and Scripting

Use the print result of a script as an argument

Hi all, Say I have a script named script.sh. What it does is to print a line like "abc" #! /usr/bin/ksh print "abc" I would like to pass this value to an external variable, var1="script.sh" However when I echo $var1 I got "script.sh" itself instead of the result? Thanks (3 Replies)
Discussion started by: isaacniu
3 Replies

6. Shell Programming and Scripting

shell script - unexpected result

I hv a file --am executing a script which is giving me unexpected results COntents of file: f1 CMT_AP1_CONT:/opt/sybase/syboc125:150:ASE12_5::Y:UX: CMT_AP1:/opt/sybase/syboc125:150:ASE12_5::Y:UX f1.tmp CMT_AP1_CONT:/opt/sybase/syboc125:150:ASE12_5::Y:UX:... (2 Replies)
Discussion started by: rajashekar.y
2 Replies

7. Shell Programming and Scripting

shell script result to file

Since I'm not an expert in shell scripting, I have question on sending script result to file. I have script like this... if condition=0: then echo "service is not running" | mail -s "Server Status" uname@companyname fi sleep 10 if configtion=1: then echo "service is not running" | mail -s... (3 Replies)
Discussion started by: s_linux
3 Replies

8. Shell Programming and Scripting

get result from database into shell script

hi, I have a script that will logon to a database siebel and do the select query and then get the result in command prompt of unix.Below the script. #!/bin/ksh . $HOME/conf/systemProperties/EnvSetup.properties #set -x while read i do echo $i connect1=`sqlplus -silent... (1 Reply)
Discussion started by: ali560045
1 Replies

9. Shell Programming and Scripting

Could any one tell me how to do get result in fraction in shell script

Could any one tell me how to do get result in fraction in shell script. I am using expr for division of 2 number but it is giving Quitent. I need it in full fraction value. please helpp. how to do it using "dc" or "bc" (5 Replies)
Discussion started by: Neerajjaiswal
5 Replies

10. UNIX for Dummies Questions & Answers

Parameter from result of a script

Hi, I have a situation where I need a parameter value to pick from a file can I use it in this way File_name.txt contains names of files a.jpg b.jpg c.jpg now I want to assign the file name to parameter FTP_INPUT_FILE=a.jpg for first run once it is processed we can delete onve they are... (2 Replies)
Discussion started by: mgirinath
2 Replies
Login or Register to Ask a Question