Solved

Basic bash shell script question

Posted on 2014-10-17
8
339 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
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 37

Accepted Solution

by:
Gerwin Jansen 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
 
LVL 84

Assisted Solution

by:ozo
ozo earned 167 total points
ID: 40387032
while [ $status != "completed" ]; do # again, note the spaces
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 37

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now