variable value is not getting replaced


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable value is not getting replaced
# 1  
Old 02-16-2011
variable value is not getting replaced

Hi,

I am trying to generate list of employees from emp table who joined yesterday.

emp.sh
Code:
YEST=$(date --date='1 day ago' +%Y-%m-%d)
cat emp.sql | mysql -u <user> -p<pass> -h <host> -P <port>  -D <dbname> > emp.csv

emp.sql
Code:
select * from employee where join_date = '$YEST';

I expected that when i do cat emp.sql in emp.sh, value of YEST will get replaced with 2011-02-15. But the query that is getting passed to mysql is select * from employee where join_date = '$YEST';

I can do sed to replace variable with value in sql file and then run the query. But is there any other easy way of doing it. Why cat done in emp.sh is not replacing the value?
# 2  
Old 02-16-2011
cat dosn't expand environment variables, and even if it did you haven't exported YEST. Best to use sed in this instance.

If you do a bit of this sort of thing and need to replace multiple enviornment variables in template scripts have a look at expand.sh (post #5) in this thread
# 3  
Old 02-16-2011
cat takes the contents of a file and prints them to stdout. That's it. It doesn't care if there are any shell variables in there. It doesn't even know what they would look like. The only program that, by default, interprets shell variables is the shell itself.

And you can get the same result using
Code:
mysql -u <user> -p <pass> -h <host> -P <port> -D <dbname> 'SELECT * FROM employee WHERE join_date = DATE_SUB( DATE(NOW()), INTERVAL "-1 DAY" );'

See "Date and Time Functions" in the MySQL Reference Manual.
# 4  
Old 02-16-2011
Thanks for clarification. I will use sed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Broken pipe symbol replaced with <A6><A6>

hi, i am copying an xml file from windows to linux server using filezilla&winscp. xml file contains ¦¦ symbols, after copying xml file to server ¦¦ is replaced with <A6><A6>. tried with copying xml files from windows in ascii&binary but no luck. please suggest. thanks (1 Reply)
Discussion started by: Satyak
1 Replies

2. Linux

Grub not loading from replaced disk

Hi, a little while ago, one of the GPT Partitioned hard disk had gone faulty in a Mirror RAID and is now successfully replaced. here is how I did that. 1) created identical partition table on the new disk. 2) attached the mirrors using md commands. The whole procedure is given below:... (2 Replies)
Discussion started by: busyboy
2 Replies

3. Shell Programming and Scripting

How to deal with replaced files?

My task is to copy all files from many directories in one. The big problem i encounter is that some files in different directory have the same names. Is they are way to copy the files that have same names in a sub directory ( need to preserve the name of the files unchanged ) I have list with... (6 Replies)
Discussion started by: gogok_bg
6 Replies

4. Solaris

Veritas not attaching replaced disk

Hi, I`m on SunFire480R with Solaris 10. Disk in rootdg group failed, so it was replaced. However, I cannot make Veritas initalise the replaced disk: # vxdctl enable # vxdisk list c1t0d0s2 Device: c1t0d0s2 devicetag: c1t0d0 type: auto flags: online error private autoconfig... (1 Reply)
Discussion started by: masloff
1 Replies

5. Shell Programming and Scripting

Performing a calculation on string to be replaced

Hi, I have a file with occurances of the string "TO_DATE(<number here>,'J')" at random places. I need minus 2400000 from the number and replace the string with "convert(date, dateadd(dd, <new number here>,'16 Nov 1858')". I'm finding this difficult as the string isn't necessarily in the... (9 Replies)
Discussion started by: user_invalid
9 Replies

6. Shell Programming and Scripting

awk next time replaced by none

Hi Everyone, 1.txt a b c d e f d g gg output: abc de (3 Replies)
Discussion started by: jimmy_y
3 Replies

7. Shell Programming and Scripting

comma replaced with pipe

Source data: "123","aaa bbb CCC","12000" "134","HHH,bbc","13000" i have a delimited file. i want to replace with the pipe.The sed command is not working for replacing a delimeter. Command : sed s/\,/\|/g filename Output : When i run the command it is replacing the columns value... (7 Replies)
Discussion started by: number10
7 Replies

8. Programming

Can Mutex be replaced with anything?

Hi All, To avoid race condition, instead of using mutex, semaphore, spinlock etc.... Is there any other mechanism by which we can avoid race condition in an multi-threading environment. -Thanks (6 Replies)
Discussion started by: rvan
6 Replies

9. UNIX for Dummies Questions & Answers

.profile has been replaced and need to get the older version

Hi All, .profile has been replaced with a new one. I need to know the variables used in the older version of .profile. I have a session opened using the old .profile. Is there a way that I can get the exact file. I had some luck by using set command. But is there any way that I can get the... (5 Replies)
Discussion started by: shash
5 Replies

10. AIX

hdisk0 becomes hdisk2 after replaced

hello, I must've screwed something here.. I just had hdisk0 replaced by IBM.. now it shows up as hdisk2 instead. Before doing that, I've had it split from hdisk1, and reduced from rootvg. Just did a rmdev -dl hdisk2.. ran cfgmgr, but still shows up as hdisk2 instead of hdisk0.. help! (2 Replies)
Discussion started by: kiem
2 Replies
Login or Register to Ask a Question