LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   grep result for if clause (bash) (https://www.linuxquestions.org/questions/programming-9/grep-result-for-if-clause-bash-765797/)

CRUDE 10-31-2009 09:55 AM

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


ozanbaba 10-31-2009 10:24 AM

Quote:

Originally Posted by CRUDE (Post 3739168)
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.

gnashley 10-31-2009 11:08 AM

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

CRUDE 10-31-2009 11:51 AM

Quote:

Originally Posted by gnashley (Post 3739214)
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.


All times are GMT -5. The time now is 07:52 AM.