Conditional IF Question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional IF Question
# 1  
Old 10-19-2017
Conditional IF Question

I trying to get a simple script to see if a directory contains any files in it. I am failing on the conditional IF statement. I am not sure if it because the command I am using is creating the variable as a string or if it is numeric or if I just have the syntax wrong.

Code:
#!/usr/bin/ksh
files=$(ls | wc -l)
echo $files
if [[$files -gt 0]];
then
print "There are files";
else
print "There are NO files";
fi
exit

Command Output:
Code:
1034
./test2.ksh[7]: [[: not found
There are NO files

Any assistance would be appreciated

Thank you
# 2  
Old 10-19-2017
Hi,

You need spaces inside both sides of [[ and ]].

Code:
if [[ $files -gt 0 ]];
...

This User Gave Thanks to Scott For This Post:
# 3  
Old 10-19-2017
Thank you Scott, that fixed it. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Conditional Script

Hi, I have a script file which has some simple commands. I want these commands to be executed based on the input. Ia m good with IF statement also. At the end it has to be based on incoming value. Example CASE 1 : Execute some commands where Input value as 1 CASE 2 : Execute... (5 Replies)
Discussion started by: vrupatel
5 Replies

2. Shell Programming and Scripting

Conditional execution

Hi All, I want to echo a message in case a system is reachable by ping or echo a different message in case it's not reachable. Sample code i wrote is ping localhost -n 2 | grep 'ttl' > ping_op; ls ping_op > /dev/null && drReachable=Alive; echo -e `date`: \\t "DR server is reachable" >>... (5 Replies)
Discussion started by: Mr. Zer0
5 Replies

3. Shell Programming and Scripting

Easy if conditional question.

I'm just starting this whole scripting thing, and I'm trying to write a simple script which will ask the user to press a key between 3-7. If they press a key that's not between 3-7, it will tell the user such. Here's my script: #!/bin/bash blah=1 echo -n "Press a key from 3-7." read... (3 Replies)
Discussion started by: SlickStretch
3 Replies

4. Shell Programming and Scripting

Conditional Splitting.

hi, I have file with some data delimited by #. For e.g. : RHMS0001#1#ABCD RHMS0002#1#ABCD RHMS0003#1#ABCD RHMS0004#1#ABCD RHMS0005#1#ABCD RHMS0006#1#ABCD RHMS0007#1#ABCD RHMS0001#2#ABCD RHMS0002#2#ABCD RHMS0001#3#ABCD RHMS0004#3#ABCD RHMS0006#3#ABCD (7 Replies)
Discussion started by: pparthiv
7 Replies

5. Shell Programming and Scripting

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (12 Replies)
Discussion started by: abhinavsinha
12 Replies

6. UNIX for Dummies Questions & Answers

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (1 Reply)
Discussion started by: abhinavsinha
1 Replies

7. UNIX for Dummies Questions & Answers

conditional

conditional is not wworking can any one figure out what goes wrong xx1=`$ORACLE_HOME/bin/sqlplus -s apps/ostgapps1 2>/dev/null << EOF WHENEVER SQLERROR EXIT 1 set head off feedback off ; WHENEVER SQLERROR EXIT SQL.SQLCODE; select count(*) from CMS_INVOICE_ALL... (2 Replies)
Discussion started by: u263066
2 Replies

8. UNIX for Dummies Questions & Answers

conditional ftp

There are two servers X and Y.We have some files in server X created in the month of may,08(other months file is also there). We want to FTP the files to server Y checking the condition on server X.We cant run script on server X.please suggest wat is the command to do it (3 Replies)
Discussion started by: dr46014
3 Replies

9. Shell Programming and Scripting

AWK - conditional cause

Hello guys, I want to make a conditional cause in the following file using awk: awk '{ if ($2 != 0) print $1, $2, $3}' test.csv > test2.csv FILE EXAMPLE = test.csv string,number,date abc,0,20050101 def,1,20060101 ghi,2,20040101 jkl,12,20090101 mno,123,20020101 ... (2 Replies)
Discussion started by: Rafael.Buria
2 Replies

10. Programming

question on conditional compilation

Hey, can I #define something outside the source file ? I have a C program which uses #ifdef.. #ifdef ABC ... do this.. #else ... that ... #endif The usual way that I know of defining ABC is in the source/header file #define ABC But is there any other way to do that ? Maybe as... (2 Replies)
Discussion started by: the_learner
2 Replies
Login or Register to Ask a Question