Solved

Basic bash shell script question

Posted on 2014-10-17
8
346 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
Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

 
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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

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…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
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.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

630 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