Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help on variables

Status
Not open for further replies.

Wrathchild

Technical User
Aug 24, 2001
303
US
Hi, I have a couple questions on using variables in scripts.
1. Can I have a command such as grep within a variable like below, or is the grep being executed as the variable is being set? If it's being executed then I can just pull it out and add it to the second line.
2. I can't figure out how to escape the date correctly; I assume it needs to be as the entire line is enclosed within the ` character

LOGCONTENTS=`grep \`date +%Y-%m-%d\` | grep -i "[n][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"`
cat /tmp/logs | ${LOGCONTENTS} > /tmp/test

thanks!
 
Perhaps this ?
grep `date +%Y-%m-%d` /tmp/logs | grep -i 'n[0-9][0-9][0-9][0-9][0-9][0-9][0-9]' > /tmp/test

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
thanks for responding PHV, however I would like to assign the commands to the variable if possible as I want to reduce redundancy in the script. If grep is being executed and the results are being stored in the variable, then I'm looking at it wrong. I was trying to "store" the entire string of commands so I can use them throughout the script. I have several occurances of grep \`date +%Y-%m-%d\` | grep -i "[n][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" in my script, so I thought I'd try to store the string in a variable and only have to update it in one place in the future.
 
You'll nead the eval command.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
thanks PHV, I did need EVAL. Below is what I changed:

LOGCONTENTS="grep `date +%Y-%m-%d` | grep -i \"[n][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\""
cat /tmp/logs | eval ${LOGCONTENTS} > /tmp/test


apparently the "ticks" or whatever you call them are telling the system to execute the commands immediately, so I was shooting myself in the foot there :)
 
LOGCONTENTS=`grep \`date +%Y-%m-%d\` | grep -i "[n][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"`
cat /tmp/logs | ${LOGCONTENTS} > /tmp/test


The answer is to simplify your script like so:

date +%Y-%m-%d | read yymmdd

cat /tmp/logs |
grep $yymmdd |
grep -i "[n][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" > \
/tmp/test
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top