LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-31-2009, 09:55 AM   #1
CRUDE
LQ Newbie
 
Registered: Oct 2009
Distribution: Arch
Posts: 3

Rep: Reputation: 0
grep result for if clause (bash)


I want part of my bash script to only be executable if there's a certain grep match.

Here's what I want in the if clause:
Code:
install -m644 -D ${srcdir}/${pkgname}.preset ${pkgdir}/etc/mkinitcpio.d/${pkgname}.preset || return 1
install -m644 -D ${srcdir}/mkinitcpio-${pkgname}.conf ${pkgdir}/etc/mkinitcpio-zen.conf || return 1
Disregard the variables.

Here's the grep line:
Code:
grep -F CONFIG_BLK_DEV_INITRD=y /path/to/file
I haven't added the correct path.

I want it to look like this, but I don't know how to state it:
Code:
if grep -F CONFIG_BLK_DEV_INITRD=y /path/to/file
    install -m644 -D ${srcdir}/${pkgname}.preset ${pkgdir}/etc/mkinitcpio.d/${pkgname}.preset || return 1
    install -m644 -D ${srcdir}/mkinitcpio-${pkgname}.conf ${pkgdir}/etc/mkinitcpio-zen.conf || return 1
fi
 
Old 10-31-2009, 10:24 AM   #2
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 671

Rep: Reputation: 94
Quote:
Originally Posted by CRUDE View Post
I want part of my bash script to only be executable if there's a certain grep match.

Here's what I want in the if clause:
Code:
install -m644 -D ${srcdir}/${pkgname}.preset ${pkgdir}/etc/mkinitcpio.d/${pkgname}.preset || return 1
install -m644 -D ${srcdir}/mkinitcpio-${pkgname}.conf ${pkgdir}/etc/mkinitcpio-zen.conf || return 1
Disregard the variables.

Here's the grep line:
Code:
grep -F CONFIG_BLK_DEV_INITRD=y /path/to/file
I haven't added the correct path.

I want it to look like this, but I don't know how to state it:
Code:
if grep -F CONFIG_BLK_DEV_INITRD=y /path/to/file
    install -m644 -D ${srcdir}/${pkgname}.preset ${pkgdir}/etc/mkinitcpio.d/${pkgname}.preset || return 1
    install -m644 -D ${srcdir}/mkinitcpio-${pkgname}.conf ${pkgdir}/etc/mkinitcpio-zen.conf || return 1
fi
when you run the grep -F CONFIG_BLK_DEV_INITRD=y /path/to/file, how will the output be? if empty, ´ grep -F CONFIG_BLK_DEV_INITRD=y /path/to/file´ == "" would do the jop? (not ', it's ´). forever you can try to use grep return codes if available, man grep would list them if exists.

if there's a usable return code you can check the return code in $ in if. http://www.technologyquestions.com/t...sh-script.html this address gives some examples.
 
Old 10-31-2009, 11:08 AM   #3
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,755

Rep: Reputation: 466Reputation: 466Reputation: 466Reputation: 466Reputation: 466
if [[ $(grep -F CONFIG_BLK_DEV_INITRD=y /path/to/file 2> /dev/null) ]] ; then

I haven't checked for sure, but you meay need to esacpe the '=':
if [[ $(grep -F 'CONFIG_BLK_DEV_INITRD\=y' /path/to/file 2> /dev/null) ]] ; then
 
Old 10-31-2009, 11:51 AM   #4
CRUDE
LQ Newbie
 
Registered: Oct 2009
Distribution: Arch
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by gnashley View Post
if [[ $(grep -F CONFIG_BLK_DEV_INITRD=y /path/to/file 2> /dev/null) ]] ; then
This works perfectly and the -F flag keeps me from having to escape '='.

Thank you.
 
  


Reply

Tags
bash, grep, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using find and grep - how to exclude a result ? graziano1968 Linux - General 13 12-04-2010 10:02 PM
Delete first line from a grep result mbjunior99 Linux - Desktop 5 07-02-2009 01:07 AM
[Bash] If clause netpumber Programming 2 04-10-2009 06:06 AM
ps -ef | grep iptables gives no result ? markraem Linux - Networking 1 07-07-2004 05:28 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM


All times are GMT -5. The time now is 11:21 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration