March 7th, 2011, 05:28 AM
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.
But upon executing I am getting the below error..
if [ $dirName = "C" && $dirName \!= "A" && \!= "B" ] then
echo "Inside else loop"
Please help me..if there is any thing wrong in the syntax
./application/root/shellscript/Test.sh: line 5: [: missing `]'
March 7th, 2011, 06:34 AM
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:
- You need to have square brackets around each of the conditions you're testing for
if [ $dirName = "C" ] && [ $dirName != "A" ] && [ $dirName != "B" ]; then
echo "Inside else statement"
- 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".