1
#!/bin/bash
echo 'Want Task 1'
read r1 ;
if [ echo "${r1}" | grep -iq "^y" ] ; then
    echo 'Task One Done'

    echo 'Want Task 2 ??'
    read r101
    if [ echo "${r101}" | grep -iq "^y" ] ; then 
        echo 'Task 2 Done'
    fi
fi

When I run the above code, it gives me error saying

[: missing `]' grep: ]: No such file or directory

But when I simply omit out the brackets [ and ] the code runs perfect. I know this [] is a test command, but I don't know when to use it and when not to.

And also please clarify if I should call this [] an "operator" or test "command "

Side note, if necessary, I am using Bash in GNOME terminal.

2

The usage of the pipe lets the commands run in different subshells, so you get the error message as both commands ([ and grep) are then incomplete (respective do not work as expected).

If you just want to test the result code of a program you do not need to use [ … ] or test, you will need those only for more distinct comparison like [ 1 -gt 2 ] or [ "$1" == "$2" ]. If you want even more advanced comparisons, like using regular expressions, you would use the command [[ … ]], see Test Constructs and A Brief Introduction to Regular Expressions in the 'Advanced Bash-Scripting Guide'

  • Also, should I call these [] operators or test command by naming convention? – GypsyCosmonaut Jul 6 '17 at 17:49
  • 1
    I personally prefer to use [] because of the enhanced readability, especially when using several tests chained one after another with && or ||, but it's up to you. (if I understood your question correctly) – Jaleks Jul 6 '17 at 17:57
1

Here is another suggestion to a script which will do the same:

#! /bin/bash
read -p 'Want Task 1 ' 
if [ "${REPLY^}" = Y ] ; then
    echo 'Task One Done'

    read -p 'Want Task 2 ?? '
    if [ "${REPLY,}" = y ] ; then 
        echo 'Task 2 Done'
    fi
fi

The default variable for read is $REPLY. When using ${REPLY^} you will get the content in uppercase, and with ${REPLY,} it is in lowercase. As the variable can be empty you need the " quotes around it.

If you really want to call an external program like grep you can do it like this:

read -p 'Want Task 1 ' 
if echo $REPLY | grep -icq ^y ; then
        echo 'Task 1 Done'
fi

What actually happen above is that we run a command and if the exit value is not 0, it is true. This example will show it:

if /bin/true ; then
    echo We have always the yes hat on
fi

And [ is just a program called /usr/bin/test. Look it up under man test.

  • Thanks for letting me know about ${REPLY} but why is there a ^ at the end of it as you mentioned ${REPLY^}? Doesn't ^ mean the starting of string? and also what is , in ${REPLY,}? – GypsyCosmonaut Jul 6 '17 at 19:32
  • 1
    @MultiverseTrip No, in a bash string ${REPLY^} means uppercase. Most of the time $REPLY and ${REPLY} is just the same, but $REPLY7 is not the same as ${REPLY}7. – hschou Jul 6 '17 at 19:36
  • @hschou It may be worth editing your answer to include Bash 4.0 as a minimum requirement for case manipulation. – Timothy Martin Jul 6 '17 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.