Grep, Variables, IF statements... Fun


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Grep, Variables, IF statements... Fun
# 1  
Old 02-18-2013
Code 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.
Code:
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.

2. Relevant commands, code, scripts, algorithms:

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

searching for the string within the file.

3. The attempts at a solution (include all code and scripts):
Code:
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.


4. 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).

Last edited by vbe; 02-18-2013 at 09:06 AM.. Reason: code tags...
# 2  
Old 02-18-2013
what should I understand reading this line?:
Code:
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?
# 3  
Old 02-18-2013
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!
# 4  
Old 02-18-2013
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.
# 5  
Old 02-19-2013
Looks like that should work, glancing at your script. Pls post xtrace execution log (set -vx) if it does not.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using $variables in grep

Hi, I'm currently trying to use variables in grep on my script. Printing the variable via echo works fine. Also, if I hard coded the date of the appointment it works just fine. But, if I try to use the $DATE as an argument in grep it doesn't do anything. #!/bin/bash DATE=${2}/${3}/${1} ... (6 Replies)
Discussion started by: nuclearpenguin
6 Replies

2. Shell Programming and Scripting

[Help] Grep, Variables, IF statements... Fun

Hello, this will be my first post. I've been browsing around for a bit and have found a lot of useful information on here, hopefully a solution can be provided to me. Issue: Alright, I am looking to search for strings within a file using variables. I have a script that will accept 3 or 4... (2 Replies)
Discussion started by: JonLaberge
2 Replies

3. Shell Programming and Scripting

grep and variables ?

I would like to know if grep can extract the following requirement. I have the folllowing piece of SQL in a file and need to grep the FROM part. mp db-ter-fast-export C100_Input_Target_Table__table_ "${DB}"'/teradata.dbc' -select 'SELECT UPPER(trim(proj_id)) as proj_id, latest_job_id,... (9 Replies)
Discussion started by: anduzzi
9 Replies

4. Shell Programming and Scripting

grep variables nested within grep

Help, I have a ksh script that has some variables within the grep command, I am then setting another variable that greps the variables that have greps within them. cat $WORKINGDIR/meter_list.txt | while read meter do serialnum=$(cat masterlogfile.txt | grep "$meter" | awk '{ print $19 }'... (7 Replies)
Discussion started by: srichard9
7 Replies

5. Programming

Grep with C variables.

Hi all, I have a problem, I need a grep -v but, the patterns of the grep should be C variables. Example: char var1="h"; char var2="o"; char var3="d"; system("grep -v \"var1 var2 var3\" file.txt"); ---- ---- ---- I try it but... Can u help me? Thanks beforehand P.S. Sorry for... (4 Replies)
Discussion started by: AbelBs
4 Replies

6. Shell Programming and Scripting

combine two grep statements

Hi I am wondering is it possible to combine two greps together I have two greps. grep "^,, *\." file (grep the line which has a '.' in the third column) grep "=" file (grep the line which has = anywhere) How to put them together so that if the content of the file that match either... (1 Reply)
Discussion started by: tiger66
1 Replies

7. Shell Programming and Scripting

put value of multiple sql statements into unix variables

i want to use multple sql count statements and store these count values in unix variable but in one connection only i.e. in only 1 time database should be hit ,which is the main requirement. (1 Reply)
Discussion started by: sw@pnil
1 Replies

8. Shell Programming and Scripting

awk as grep with variables

i have such awk working fine but how to use variable instead of strings awk '/asdasd.*asda.*asdasd/' file2.txt This is not working: awk '/${a}.*${b}.*${c}/' file2.txt Thanks & regards Peter (7 Replies)
Discussion started by: pp56825
7 Replies

9. UNIX for Advanced & Expert Users

SQLPLUS and update statements using bind variables

Hello- The code below works fine expect that it does not update the table CTL_INTERFACE "red highlight". Any idea what I'm doing wrong here? Thanks:D # coNNECT to the database and insert a row then get the new row id cycle_id=`sqlplus -S $XXX_USER/$XXX_PW@$XXX_CONNECT << EOF SET ECHO... (3 Replies)
Discussion started by: Nomaad
3 Replies

10. Shell Programming and Scripting

Using grep with variables

Being new to shell scripting I hope this question isn't too elementary but here goes. I run a grep statement - grep 'sqr' sqrmodule.par and the grep statement returns correctly the information that I'm looking for. Now I want to take the output from the grep statement and load it into a... (2 Replies)
Discussion started by: gettingstarted
2 Replies
Login or Register to Ask a Question