Linux Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsLinux Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 7th, 2011, 05:28 AM
reference2me reference2me is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 22 reference2me User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 5 m 10 sec
Reputation Power: 0
Syntax error in If loop

I have a requirement to check the following conditions..

If my folders are not A and B then list files in the directory else no listing the files

so my if loop is some thing like this.

Code:
if [ $dirName = "C" && $dirName \!= "A" && \!= "B" ] then
ls -la
else
echo "Inside else loop"
fi


But upon executing I am getting the below error..

Code:
./application/root/shellscript/Test.sh: line 5: [: missing `]'


Please help me..if there is any thing wrong in the syntax

Reply With Quote
  #2  
Old March 7th, 2011, 06:34 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Sep 2001
Location: Shanghai, An tSín
Posts: 6,894 ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 37 m 21 sec
Reputation Power: 3885
You have a few issues with this code (assuming you're using bash - you haven't said): firstly, an "if" statement is not a loop - it only executes once ("while" and "for" are loops because keep looping over the same code multiple times).

Here it is cleaned up:
Code:
#!/bin/bash
dirName='A';
if [ $dirName = "C" ] && [ $dirName != "A" ] && [ $dirName != "B" ]; then
   ls -la
else
   echo "Inside else statement"
fi

- You need to have square brackets around each of the conditions you're testing for
- You don't need to escape the !=
- You need to specify $dirName every time you do a comparison
- You need semicolon before "then"
- I haven't fixed the logical problem here: if $dirName = "C" then you're already guaranteed that it isn't equal to "A" or "B".

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsLinux Help > Syntax error in If loop

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap