Solved

Basic bash shell script question

Posted on 2014-10-17
8
344 Views
Last Modified: 2014-10-24
I am trying to search a string from file and checking the condition, but not working.

apple="fruit"

if [`grep $apple $log_dir/log1`]; then
    echo "$apple  exist"
else
    echo "$apple deosn't exist"
fi

any synax error here?
0
Comment
Question by:mokkan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 167 total points
ID: 40387003
Can try grep -q instead?

What error do you get?
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 167 total points
ID: 40387010
if [ `grep $apple $log_dir/log1` ] ;  then  # note the spaces
# or
if grep $apple $log_dir/log1 ;  then
0
 

Author Comment

by:mokkan
ID: 40387024
Sorry guys,  here is the exact script

#!/bin/bash -x

script_dir="/opt/scripts/dr"
log_dir="${script_dir}/logs"
echo $log_dir
status="not_completed"
fc_state="copying"

while [$status != "completed"]; do
if [`grep "$fc_state" $log_dir/log1`]; then
    echo "$fc_state exist on log"
else
    echo "$fc_state does not exist log"
    status="completed"
fi
done

And here is the error message

 script_dir=/opt/scripts/dr
+ log_dir=/opt/scripts/dr/logs
+ status=not_completed
+ fc_state=copying
+ '[not_completed' '!=' 'completed]'
./fl: line 8: [not_completed: command not found
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Assisted Solution

by:ozo
ozo earned 167 total points
ID: 40387032
while [ $status != "completed" ]; do # again, note the spaces
0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 83 total points
ID: 40387070
@ozo's given the answer, but I'd suggest moving your double quotes so that it is:

    while [ "$status" != "completed" ]; do

Without the quotes, if $status is empty or has a space in it, you get a syntax error.  You don't strictly need quotes round the "completed" because it has text, and has no spaces or special characters in it, but it's a common idiom, and indicates to the reader that you are dealing with strings rather than numbers.
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 83 total points
ID: 40387109
Simply do:

apple="fruit"

if grep -q $apple $log_dir/log1 ; then
    echo "$apple  exist"
else
    echo "$apple doesn't exist"
fi

i. e. no need for the square brackets nor for the backticks. Add the "-q" flag to "grep" to suppress its output.

The long version:

#!/bin/bash -x

script_dir="/opt/scripts/dr"
log_dir="${script_dir}/logs"
echo $log_dir
status="not_completed"
fc_state="copying"

while [ "$status" != "completed" ] ; do
if grep -q "$fc_state" $log_dir/log1 ; then
    echo "$fc_state exist on log"
else
    echo "$fc_state does not exist log"
    status="completed"
fi
done
0
 
LVL 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 167 total points
ID: 40390028
Since this is bash, I recommend you have a look at it's man page:

man bash

Search for 'conditional expression':

/conditional expression

When testing bash scripts, call them with verbosity:

bash -xv ./your_script

That way you get to see exactly how and where variables are assigned, tested etc.
0
 

Author Closing Comment

by:mokkan
ID: 40403465
Thank you  alllll.  I fixed the script.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question