Grep, Variables, IF statements... Fun

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

The issue I am having is part of a validation problem. My script will validate 3 or 4 parameters entered by the user
the first 3 are mandatory the 4th one is not.
After validating that the parameters entered are Ok, it should then (This part being the problem) check to see if the 3 parameters are already within the file that will be modified, to avoid duplicate entries. if the 3 parameters are found inside, than it will replace those with what was entered. I preferred to use grep to remove them out of the file rather than sed, because it doesn't matter which order the entries are in, so appending to the end of the file works fine. *Also within the file it should also not care about the spaces between the entries since it is kind of random, so ignoring the white spaces would be needed.

variables:
domain=$1
type=$2
item=$3
value=$4

This is for school work, so I'd appreciate all help but I would like to understand about it, if an answer is given and that poster could kindly give me some feedback on what I was doing wrong, I will also be researching up on the input I get from you guys, I am not just looking for an easy way out.

  1. Relevant commands, code, scripts, algorithms:

egrep, modifying limits.conf from the "~/etc/security" directory.

searching for the string within the file.

  1. The attempts at a solution (include all code and scripts):
input= egrep -e '$domain\s+$type\s+$item' ~/etc/security/limits.conf
if [ "$input" == "" ]; then

        echo $domain $type $item $value >>~/etc/security/limits.conf

        echo \"$domain\" \"$type\" \"$item\" \"$value\" has been successfully added to your limits.conf file.


else
        cat ~/etc/security/limits.conf | egrep -v "$domain|$type|$item" >~/etc/security/limits.conf1
        rm -rf ~/etc/security/limits.conf
        mv ~/etc/security/limits.conf1 ~/etc/security/limits.conf

        echo $domain    $type    $item   $value >>~/etc/security/limits.conf

        echo \"$domain\" \"$type\" \"$item\" \"$value\" has been successfully added to your limits.conf file.
        exit 0
fi

The red text is where I'm having trouble with.

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Seneca School of Information and Technology, college.

Toronto, ON - Canada

Professor: John Selmys

OPS435

scs.senecac.on.ca/course/ops435

Thank you for any helps and suggestions/feedback

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

what should I understand reading this line?:

input= egrep -e '$domain\s+$type\s+$item' ~/etc/security/limits.conf

if that is a variable affectation then sure it will not work (syntax: VAR=value or VAR=$var or... No spaces after = for a start...)

What shell are you using?

And, I guess it should make use of "command substitution", so wrap the entire egrep command in $(...) or backtics `...` .
Shell and system info would be nice, indeed!

Sorry! Should have mentioned that.

Linux OS, Bash Shell.

input=$(egrep -e '$domain\s+$type\s+$item' ~/etc/security/limits.conf)

I would like it so that if this returns as blank, no results. than my script will append the 4 parameters to the end of the file.

Looks like that should work, glancing at your script. Pls post xtrace execution log (set -vx) if it does not.