If inside If loop


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If inside If loop
# 1  
If inside If loop

Hi All,

Below is the very simple code snippet but it si giving me syntax error

Code:
 
#!/bin/bash
#To ensure If JMS directory exists or not
ServerName=$(hostname)
#To ensure If JMS directory exists or not
echo $ServerName
if [ "$ServerName" = "usei01" || "$ServerName" = "usei02" || "$ServerName" = "usei03" ]; 
then
echo "Inside First If"
if [ -d /opt/shared -a -w /opt/shared/data/jms ];
then 
echo 'JMS mounts................exist'>>OutputResult.txt
else 
echo "JMS mounts................not exist">>OutputResult.txt
fi
echo "Outside IF"
fi

It is not reading the else and syntax error at line 7 which is my first If clause.
# 2  
Quote:
Originally Posted by sharsour
Hi All,

Below is the very simple code snippet but it si giving me syntax error

Code:
 
#!/bin/bash
#To ensure If JMS directory exists or not
ServerName=$(hostname)
#To ensure If JMS directory exists or not
echo $ServerName
if [[ "$ServerName" = "usei01" || "$ServerName" = "usei02" || "$ServerName" = "usei03" ]]; 
then
echo "Inside First If"
if [ -d /opt/shared -a -w /opt/shared/data/jms ];
then 
echo 'JMS mounts................exist'>>OutputResult.txt
else 
echo "JMS mounts................not exist">>OutputResult.txt
fi
echo "Outside IF"
fi

It is not reading the else and syntax error at line 7 which is my first If clause.
Just try the above code Smilie

Cheers!!!
-R
This User Gave Thanks to rangarasan For This Post:
# 3  
You have to write the first if clause as below.

Code:
 
if ([ "$ServerName" = "usei01" ]|| ["$ServerName" = "usei02" ]|| ["$ServerName" = "usei03" ]);then

;then should be in one line

Last edited by millan; 07-01-2013 at 06:22 AM.. Reason: added brackets
This User Gave Thanks to millan For This Post:
# 4  
Or:
Code:
 if [ "$ServerName" = "usei01" -o "$ServerName" = "usei02" -o "$ServerName" = "usei03" ]

This User Gave Thanks to Klashxx For This Post:
# 5  
Thanks Milan,

It is working for me
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #585
Difficulty: Easy
Most object oriented programming languages such as C++ and Java are class-based languages.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

2. Shell Programming and Scripting

AWK inside For loop

Hi, awk -F"|" 'BEGIN{sum=0}{sum+=$2}END{printf("%d\n", sum)}' css.txt awk -F"|" 'BEGIN{sum=0}{sum+=$3}END{printf("%d\n", sum)}' css.txt awk -F"|" 'BEGIN{sum=0}{sum+=$4}END{printf("%d\n", sum)}' css.txt awk -F"|" 'BEGIN{sum=0}{sum+=$5}END{printf("%d\n", sum)}' css.txt awk -F"|"... (3 Replies)
Discussion started by: thulasidharan2k
3 Replies

3. Shell Programming and Scripting

Help with grep inside an if loop

Hello All, I have been reading posts on here for a while, but this is my first post. I have a document in which many sentences appear, and I am piping it through an exterior script which will tag each word in the document with its part of speech (not part of my script, just background). The... (3 Replies)
Discussion started by: daf189
3 Replies

4. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

5. Shell Programming and Scripting

Using 'su' inside a loop

Hi, I am using su within a for loop. As you might expect, it prompts for password during each loop execution. Here is my piece of code: for i in $LIST do if then DATABASE=`echo $i | awk -F "|" '{ print $1 }'` USER_ID=`echo $i | awk -F "|" '{ print $2 }'` su - apstage -c... (1 Reply)
Discussion started by: sugan
1 Replies

6. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

7. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

8. Shell Programming and Scripting

condition inside a for loop

I have a for loop in my script as shown below. for file_path in $file_list ; do ........my code .......... ...... done Can i restrict the number of files parsing to the variable file_path as 50? That is, even if I have some 100 files in file_list, I need to take only 50 files for... (7 Replies)
Discussion started by: Vijay06
7 Replies

9. Shell Programming and Scripting

Assigning inside for loop

If I have 3 variables and I want to check if any of these is null. If one of them is null then it should be assigned a value of 0.I have the following code below. The output should be 0 is A, 11 is B, 33 is C $a= $b=11 $c=33 $echo $a $b $c $11 33 for i in "${a}" "${b}" "${c}"; do ... (2 Replies)
Discussion started by: thana
2 Replies

10. UNIX for Advanced & Expert Users

remsh inside of while loop

I have a space delimited file containing: hostname OracleSID connectstring I want to loop through the file and execute remsh to check the database processes. cat $filename | while read HOST SID CONNECT do { result=`remsh $HOST "ps -ef | grep pmon_${SID}$| grep -v grep"` if ........ (1 Reply)
Discussion started by: joettacm
1 Replies

Featured Tech Videos