Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Basic bash shell script question

Posted on 2014-10-17
8
Medium Priority
?
349 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 668 total points
ID: 40387003
Can try grep -q instead?

What error do you get?
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 668 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 84

Assisted Solution

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

Assisted Solution

by:simon3270
simon3270 earned 332 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 332 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 668 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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

715 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