Assigning an expression to a variable in shell script


 
Thread Tools Search this Thread
Operating Systems Solaris Assigning an expression to a variable in shell script
# 1  
Old 04-14-2014
Assigning an expression to a variable in shell script

i am trying to assign the following expression to a variable in Unix shell script
and want to use that variable in some other expression. But unable to get the required thing done. Please help with this....

This is the expression which i want to provide as input the variable
Code:
date '+%y:%m:%d' | awk -F":" '{printf"20%2d%2d%2d\n",$1,$2,($3-1)}' | sed 's/ /0/g'

like

Code:
d = `date '+%y:%m:%d' | awk -F":" '{printf"20%2d%2d%2d\n",$1,$2,($3-1)}' | sed 's/ /0/g'`

Code:
mget m.log.$d*

i tried like this but it is showing "d" not found

Please help me to get output like this

Code:
m.log.20140413


Last edited by Scrutinizer; 04-14-2014 at 08:34 AM.. Reason: code tags
# 2  
Old 04-14-2014
That is not a good way to get the previous day. What happens on the 1st of a month?
Variable assignments do not have spaces around the = sign. You need to leave them out.
# 3  
Old 04-14-2014
OS : SunOS
Version : 5.10

---------- Post updated at 05:44 AM ---------- Previous update was at 05:41 AM ----------

Thanks Scrutinizer,,,

It worked when i removed the spaces before and after the "=" sign

---------- Post updated at 06:02 AM ---------- Previous update was at 05:44 AM ----------

Hi Scrutinizer
I understood your point about the date when it is the first day of next month,,,

can you please provide some script which can help me on this?

Thanks in advance

---------- Post updated at 06:08 AM ---------- Previous update was at 06:02 AM ----------

Hi Scrutinizer

I found something like this in the internet which gives yesterday's date automatically

d=`gdate -d'yesterday' +%Y%m%d`

I tried and it worked

Do u have any idea about this,, will it solve this problem when it is the first day of the month?

Please reply if u know anything about it

Last edited by ssk250; 04-14-2014 at 10:03 AM.. Reason: forgot to add the main line
# 4  
Old 04-14-2014
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Assigning Shell variable

Hello, Need a small help to execute below script. #!/bin/bash . new.txt for no in 3 4 do echo $((uname_$no)) done new.txt contains uname_1="XXXXXX" uname_2="YYYYY" uname_3="ZZZZZ" ......... (6 Replies)
Discussion started by: prasanna2166
6 Replies

2. Shell Programming and Scripting

assigning value of a expression to a variable

eval echo \$tts_space_name$count i m getting output of this stmnt as 'TBS_ADOX_EXTR3' but, I m not able to assign this value to a variable . i tried export j=`eval echo \$tts_space_name$count` eval j= `eval echo \$tts_space_name$count` and when i do echo $j ... i get o/p as 1 or 2... (1 Reply)
Discussion started by: Gl@)!aTor
1 Replies

3. Shell Programming and Scripting

assigning fields variables value to shell variable

suppose I have a file named abc.txt.The contents of the file is sited below abc.txt maitree,test,test3 Using awk command can I store these 3 values in 3 different variable and in one single line command of awk. suppose variable a b c is there. I don't want like this a=`awk -F"," '{print... (2 Replies)
Discussion started by: maitree
2 Replies

4. Shell Programming and Scripting

Assigning return value of an embedded SQL in a shell script variable

I've a script of the following form calling a simple sql that counts the no of rows as based on some conditions. I want the count returned by the sql to get assigned to the variable sql_ret_val1. However I'm finding that this var is always getting assigned a value of 0. I have verified by executing... (1 Reply)
Discussion started by: MxC
1 Replies

5. Shell Programming and Scripting

Assigning variable from command outputs to shell

First, this is bash (3.2.17), on a Mac, 10.5.7. What I'm trying to do is look at a list of users, and check to see if each exists. If they do, do some more stuff, if they don't, drop them into an error file. So, my user list is: foo - exists bar - does not exist blah - does not exist ... (2 Replies)
Discussion started by: staze
2 Replies

6. UNIX for Dummies Questions & Answers

Assigning evaluated expression to a variable

Hello, Could you please let me know what is the problem here.. 28:var1="SERVER_$j" 29:eval $var1=`grep "^DBSERVER=" "$i" |cut -d"=" -f2` i get this error: syntax error at line 29 : `|' unexpected Thanks for your quick response..This is urgent..pls.. Regards Kaushik (5 Replies)
Discussion started by: kaushikraman
5 Replies

7. Shell Programming and Scripting

Assigning output of command to a variable in shell

hi, I want to assign find command result into some temporary variable: jarPath= find /opt/lotus/notes/ -name $jarFile cho "the jar path $jarPath" where jarPath is temporary variable. Can anybody help on this. Thanks in advance ----Sankar (6 Replies)
Discussion started by: sankar reddy
6 Replies

8. Shell Programming and Scripting

assigning nawk output to shell variable

Hello friends, I doing the follwing script , but found problem to store it to a shell variable. #! /bin/sh for temp in `find ./dat/vector/ -name '*.file'` do echo $temp nawk -v temp=$temp 'BEGIN{ split(temp, a,"\/"); print a}' done output: ./dat/vector/drf_all_002.file... (6 Replies)
Discussion started by: user_prady
6 Replies

9. Shell Programming and Scripting

assigning command output to a shell variable

I have the sql file cde.sql with the below contents: abcdefghij abcwhendefothers sdfghj when no one else when others wwhen%others exception when others Now I want to search for the strings containing when others together and ceck whether that does not occur more than once in the... (2 Replies)
Discussion started by: kprattip
2 Replies

10. Shell Programming and Scripting

Assigning a shell variable as a float

I'm confused as to how to handle floating point numbers in shell scripts. Is there a way to convert a number (string) read into a shell variable so that it can be used as a floating point decimal for calculation purposes? Or am I stuck with integrating C or Perl into my script? Ex: --input ... (3 Replies)
Discussion started by: spieterman
3 Replies
Login or Register to Ask a Question

Featured Tech Videos