Logo 
Search:

Unix / Linux / Ubuntu Forum

Ask Question   UnAnswered
Home » Forum » Unix / Linux / Ubuntu       RSS Feeds

basic shell script problem

  Date: Nov 24    Category: Unix / Linux / Ubuntu    Views: 764
  

am trying to make a simple shell script here (like a reminder).

1. how do i store the output of a program into a variable ?
like the 'date +%b' command gives 'Dec'.
now i want to store it into a variable say $TEMP.
posiible soln: tried rt=date+%b ; echo $rt
but it displays "date..." as the output.
whereas i want the output of the date command to be stored in the variable
temp. any suggestions ?

2. another doubt: i have a script like this
#!/bin/sh
grep -i dec /home/nerd/CALENDAR.TXT | sort
it searches alll the lines for ' dec' and displays them.
now i want the script to read the month from the system and then 'grep '
the file for those lines for that match the month(taken from the system
time)

Share: 

 

5 Answers Found

 
Answer #1    Answered On: Nov 24    

One of the possible Ans is (Tested on Solaris it works)

Answer to your both Questions in single Scr!!!

#!/bin/sh
date > tempdate
temp=`awk '{print $2}' tempdate`;
rm tempdate;
echo $temp;
for var in $temp
do
grep -i $var myfile | wc -l;
done;

Replace <myfile> with your absolute file path and <wc -l> whatever like <sort> or anything

 
Answer #2    Answered On: Nov 24    

1. Don't forget that ` is not the same as '.
The below statement sets the variable currentdirectory to the results of
the pwd command.
currentdirectory=`pwd`

2. You can either grep it through a case statment or you knuckle down
and learn regular expressions which I've not yet done either. ;-)

 
Answer #3    Answered On: Nov 24    

It is possible to store the value in a variable using the back quotes or grave
accent(`) This is not single quote. You will find it on the same key as tilde
(~).

rt=`date+%b` ; echo $rt

Your second question, reading the month from the system, refering to the first
question, you know how to get the month from the system date. Use the command in
back quotes that will help you. The command modifies as below

grep -i `date command to extract month` /home/nerd/CALENDAR.TXT | sort

 
Answer #4    Answered On: Nov 24    

#!/bin/sh
TEMP=`date +%b`
echo For $( date )
grep -i $TEMP /home/nerd/CALENDAR.TXT|sort

about the spam stuff, think spam assassin should do the job.
additionally, one could use a + tag to trace how your mail id was gathered by
the spammer in the first place.
for eg, if your mailid is sysadmin@..., when entering it anywhere(say
when you subscribe to a newsletter), you could enter
sysadmin+newsletter@....(i have been told anything after the '+' is
ignored by the SMTP servers.)

that way, the next time you get spam, you'll atleast be able to pin point the
source.

 
Answer #5    Answered On: Nov 24    

Modify the same as follows

#!/bin/sh
TEMP=`date +%b`
echo For $( date )
grep -i `date +%b` /home/nerd/CALENDAR.TXT|sort

 
Didn't find what you were looking for? Find more on basic shell script problem Or get search suggestion and latest updates.




Tagged: