how to grep a value stored in a variable [closed]
Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
i am writing a bash script in which i am storing current date in a variable and then i am greping that variable . issue is its not working
currentdate= $(date +%b %d)
echo "$currentdate"
last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt
users.txt is showing empty . if i write mannualy the current date then it works
what am i doing wrong?
bash shell-script aix
closed as off-topic by Sparhawk, G-Man, roaima, Kusalananda, Christopher Dec 4 at 16:44
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Sparhawk, G-Man, Kusalananda
add a comment |
up vote
-1
down vote
favorite
i am writing a bash script in which i am storing current date in a variable and then i am greping that variable . issue is its not working
currentdate= $(date +%b %d)
echo "$currentdate"
last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt
users.txt is showing empty . if i write mannualy the current date then it works
what am i doing wrong?
bash shell-script aix
closed as off-topic by Sparhawk, G-Man, roaima, Kusalananda, Christopher Dec 4 at 16:44
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Sparhawk, G-Man, Kusalananda
4
Single quotes'
vs. double quotes"
. The former won't expand variables.
– Sparhawk
Dec 4 at 5:58
2
remove that space between = and $ in currentdate. and date syntax in last is likeDEC 1
. that date command returnsDEC 01
– BlackCrystal
Dec 4 at 6:26
1
Possible duplicate of When is double-quoting necessary?
– roaima
Dec 4 at 8:56
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
i am writing a bash script in which i am storing current date in a variable and then i am greping that variable . issue is its not working
currentdate= $(date +%b %d)
echo "$currentdate"
last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt
users.txt is showing empty . if i write mannualy the current date then it works
what am i doing wrong?
bash shell-script aix
i am writing a bash script in which i am storing current date in a variable and then i am greping that variable . issue is its not working
currentdate= $(date +%b %d)
echo "$currentdate"
last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt
users.txt is showing empty . if i write mannualy the current date then it works
what am i doing wrong?
bash shell-script aix
bash shell-script aix
edited Dec 5 at 6:04
asked Dec 4 at 5:43
Black Virus
13
13
closed as off-topic by Sparhawk, G-Man, roaima, Kusalananda, Christopher Dec 4 at 16:44
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Sparhawk, G-Man, Kusalananda
closed as off-topic by Sparhawk, G-Man, roaima, Kusalananda, Christopher Dec 4 at 16:44
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Sparhawk, G-Man, Kusalananda
4
Single quotes'
vs. double quotes"
. The former won't expand variables.
– Sparhawk
Dec 4 at 5:58
2
remove that space between = and $ in currentdate. and date syntax in last is likeDEC 1
. that date command returnsDEC 01
– BlackCrystal
Dec 4 at 6:26
1
Possible duplicate of When is double-quoting necessary?
– roaima
Dec 4 at 8:56
add a comment |
4
Single quotes'
vs. double quotes"
. The former won't expand variables.
– Sparhawk
Dec 4 at 5:58
2
remove that space between = and $ in currentdate. and date syntax in last is likeDEC 1
. that date command returnsDEC 01
– BlackCrystal
Dec 4 at 6:26
1
Possible duplicate of When is double-quoting necessary?
– roaima
Dec 4 at 8:56
4
4
Single quotes
'
vs. double quotes "
. The former won't expand variables.– Sparhawk
Dec 4 at 5:58
Single quotes
'
vs. double quotes "
. The former won't expand variables.– Sparhawk
Dec 4 at 5:58
2
2
remove that space between = and $ in currentdate. and date syntax in last is like
DEC 1
. that date command returns DEC 01
– BlackCrystal
Dec 4 at 6:26
remove that space between = and $ in currentdate. and date syntax in last is like
DEC 1
. that date command returns DEC 01
– BlackCrystal
Dec 4 at 6:26
1
1
Possible duplicate of When is double-quoting necessary?
– roaima
Dec 4 at 8:56
Possible duplicate of When is double-quoting necessary?
– roaima
Dec 4 at 8:56
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
In addition to the quotes, you also need to account for the date format used by the last
command, which looks like the following:
Dec 3 # Note the padding to the left of '3'.
Nov 23
This requires a slightly different date command:
date "+%b %_d"
Dec 4
The underscore instructs date
to pad the field with spaces. You can also use %e as an alternative.
Putting these together, you can modify your script as shown below:
currentdate=$(date "+%b %_d")
last | grep "$currentdate" >> /usr/IBM/HTTPServer7/logs/alert/users.txt
add a comment |
up vote
0
down vote
@Sparhawk is correct.
Double quotes cause the shell to expand variables.
$ VAR=blahblah
$ echo "$VAR"
blahblah
Single quotes cause the shell to use the text literally.
$ VAR=blahblah
$ echo '$VAR'
$VAR
You would use this if you wanted to prevent the shell from thinking a dollar sign plus some other text was a variable.
echo 'This script is terminating because you didn't set $IMPORTANT_VARIABLE'
Without single quotes the above message wouldn't output properly.
Issue is with this command all though currentdate has correct value which is today Dec 04 but in users.txt the file is empty . last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt if i use actual value Dec 04 in the above command the result is fine .
– Black Virus
Dec 4 at 6:29
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
In addition to the quotes, you also need to account for the date format used by the last
command, which looks like the following:
Dec 3 # Note the padding to the left of '3'.
Nov 23
This requires a slightly different date command:
date "+%b %_d"
Dec 4
The underscore instructs date
to pad the field with spaces. You can also use %e as an alternative.
Putting these together, you can modify your script as shown below:
currentdate=$(date "+%b %_d")
last | grep "$currentdate" >> /usr/IBM/HTTPServer7/logs/alert/users.txt
add a comment |
up vote
0
down vote
accepted
In addition to the quotes, you also need to account for the date format used by the last
command, which looks like the following:
Dec 3 # Note the padding to the left of '3'.
Nov 23
This requires a slightly different date command:
date "+%b %_d"
Dec 4
The underscore instructs date
to pad the field with spaces. You can also use %e as an alternative.
Putting these together, you can modify your script as shown below:
currentdate=$(date "+%b %_d")
last | grep "$currentdate" >> /usr/IBM/HTTPServer7/logs/alert/users.txt
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
In addition to the quotes, you also need to account for the date format used by the last
command, which looks like the following:
Dec 3 # Note the padding to the left of '3'.
Nov 23
This requires a slightly different date command:
date "+%b %_d"
Dec 4
The underscore instructs date
to pad the field with spaces. You can also use %e as an alternative.
Putting these together, you can modify your script as shown below:
currentdate=$(date "+%b %_d")
last | grep "$currentdate" >> /usr/IBM/HTTPServer7/logs/alert/users.txt
In addition to the quotes, you also need to account for the date format used by the last
command, which looks like the following:
Dec 3 # Note the padding to the left of '3'.
Nov 23
This requires a slightly different date command:
date "+%b %_d"
Dec 4
The underscore instructs date
to pad the field with spaces. You can also use %e as an alternative.
Putting these together, you can modify your script as shown below:
currentdate=$(date "+%b %_d")
last | grep "$currentdate" >> /usr/IBM/HTTPServer7/logs/alert/users.txt
answered Dec 4 at 9:30
Haxiel
851310
851310
add a comment |
add a comment |
up vote
0
down vote
@Sparhawk is correct.
Double quotes cause the shell to expand variables.
$ VAR=blahblah
$ echo "$VAR"
blahblah
Single quotes cause the shell to use the text literally.
$ VAR=blahblah
$ echo '$VAR'
$VAR
You would use this if you wanted to prevent the shell from thinking a dollar sign plus some other text was a variable.
echo 'This script is terminating because you didn't set $IMPORTANT_VARIABLE'
Without single quotes the above message wouldn't output properly.
Issue is with this command all though currentdate has correct value which is today Dec 04 but in users.txt the file is empty . last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt if i use actual value Dec 04 in the above command the result is fine .
– Black Virus
Dec 4 at 6:29
add a comment |
up vote
0
down vote
@Sparhawk is correct.
Double quotes cause the shell to expand variables.
$ VAR=blahblah
$ echo "$VAR"
blahblah
Single quotes cause the shell to use the text literally.
$ VAR=blahblah
$ echo '$VAR'
$VAR
You would use this if you wanted to prevent the shell from thinking a dollar sign plus some other text was a variable.
echo 'This script is terminating because you didn't set $IMPORTANT_VARIABLE'
Without single quotes the above message wouldn't output properly.
Issue is with this command all though currentdate has correct value which is today Dec 04 but in users.txt the file is empty . last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt if i use actual value Dec 04 in the above command the result is fine .
– Black Virus
Dec 4 at 6:29
add a comment |
up vote
0
down vote
up vote
0
down vote
@Sparhawk is correct.
Double quotes cause the shell to expand variables.
$ VAR=blahblah
$ echo "$VAR"
blahblah
Single quotes cause the shell to use the text literally.
$ VAR=blahblah
$ echo '$VAR'
$VAR
You would use this if you wanted to prevent the shell from thinking a dollar sign plus some other text was a variable.
echo 'This script is terminating because you didn't set $IMPORTANT_VARIABLE'
Without single quotes the above message wouldn't output properly.
@Sparhawk is correct.
Double quotes cause the shell to expand variables.
$ VAR=blahblah
$ echo "$VAR"
blahblah
Single quotes cause the shell to use the text literally.
$ VAR=blahblah
$ echo '$VAR'
$VAR
You would use this if you wanted to prevent the shell from thinking a dollar sign plus some other text was a variable.
echo 'This script is terminating because you didn't set $IMPORTANT_VARIABLE'
Without single quotes the above message wouldn't output properly.
answered Dec 4 at 6:18
LawrenceC
8,42222340
8,42222340
Issue is with this command all though currentdate has correct value which is today Dec 04 but in users.txt the file is empty . last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt if i use actual value Dec 04 in the above command the result is fine .
– Black Virus
Dec 4 at 6:29
add a comment |
Issue is with this command all though currentdate has correct value which is today Dec 04 but in users.txt the file is empty . last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt if i use actual value Dec 04 in the above command the result is fine .
– Black Virus
Dec 4 at 6:29
Issue is with this command all though currentdate has correct value which is today Dec 04 but in users.txt the file is empty . last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt if i use actual value Dec 04 in the above command the result is fine .
– Black Virus
Dec 4 at 6:29
Issue is with this command all though currentdate has correct value which is today Dec 04 but in users.txt the file is empty . last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt if i use actual value Dec 04 in the above command the result is fine .
– Black Virus
Dec 4 at 6:29
add a comment |
4
Single quotes
'
vs. double quotes"
. The former won't expand variables.– Sparhawk
Dec 4 at 5:58
2
remove that space between = and $ in currentdate. and date syntax in last is like
DEC 1
. that date command returnsDEC 01
– BlackCrystal
Dec 4 at 6:26
1
Possible duplicate of When is double-quoting necessary?
– roaima
Dec 4 at 8:56