LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools
Old 10-31-2009, 10:55 AM   #1
CRUDE
LQ Newbie
 
Registered: Oct 2009
Distribution: Arch
Posts: 3
Thanked: 0
grep result for if clause (bash)


[Log in to get rid of this advertisement]
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
linux CRUDE is offline  
Tag This Post , ,
Reply With Quote
Old 10-31-2009, 11:24 AM   #2
ozanbaba
Member
 
Registered: May 2003
Location: istanbul
Distribution: Slackware
Posts: 38
Thanked: 3
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.
linux ozanbaba is offline     Reply With Quote
Old 10-31-2009, 12:08 PM   #3
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 3,692
Thanked: 101
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
linux gnashley is offline     Reply With Quote
Thanked by:
Old 10-31-2009, 12:51 PM   #4
CRUDE
LQ Newbie
 
Registered: Oct 2009
Distribution: Arch
Posts: 3
Thanked: 0

Original Poster
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.
linux CRUDE is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
Delete first line from a grep result mbjunior99 Linux - Desktop 5 07-02-2009 02:07 AM
[Bash] If clause netpumber Programming 2 04-10-2009 07:06 AM
Using find and grep - how to exclude a result ? graziano1968 Linux - General 12 06-29-2005 10:47 AM
ps -ef | grep iptables gives no result ? markraem Linux - Networking 1 07-07-2004 06:28 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM


All times are GMT -5. The time now is 08:29 AM.

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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration