Solved

Basic bash shell script question

Posted on 2014-10-17
8
335 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

758 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

22 Experts available now in Live!

Get 1:1 Help Now