Thursday, February 17, 2022

[SOLVED] Argument not recognised/accesed by egrep - Shell

Issue

Egrep and Awk to output columns of a line , with a specific value for the first column

I am to tasked to write a shell program which when ran as such

./tool.sh -f file -id id OR ./tool.sh -id id -f file

must output the name surname and birthdate (3 columns of the file ) for that specific id.

So far my code is structured as such :

    elif [ "$#" -eq 4 ];
then
        while [ "$1" != "" ];
        do
        case $1 in
             -f)
                cat < "$2" | egrep '"$4"' | awk ' {print $3 "\t" $2 "\t" $5}'
        shift 4
        ;;
             -id)
                cat < "$4" | egrep '"$2"' | awk ' {print $3 "\t" $2 "\t" $5}'
        shift 4
        esac
        done

(Ignoring the opening elif cause there are more subtasks for later)

My output is nothing. The program just runs.

I've tested the cat < people.dat | egrep '125' | awk ' {print $3 "\t" $2 "\t" $5}'

and it runs just fine.

I also had an instance where i had an output from the program while it was run like so

cat < "$2" | egrep '["$4"]' | awk ' {print $3 "\t" $2 "\t" $5}'

but it wasnt only that specific ID.


Solution

`egrep "$4"` was correct instead of `egrep '["$4"]'` in 

`cat < "$2" | egrep '["$4"]' | awk ' {print $3 "\t" $2 "\t" $5}'`

Double quotes allow variables, single quotes don't. No commands need certain types of quotes, they are purely a shell feature that are not passed to the command. mentioned by(@that other guy)



Answered By - PAV
Answer Checked By - Katrina (WPSolving Volunteer)