1

My variable is not getting replaced by its value in shell-script:

#!/bin/bash
read -p "Enter uuid "  uuid
read -p "Enter date in format yyyymmdd: " date

echo $uuid
echo $date

a=`zgrep 'Queue for uuid $uuid' reader_$date.gz`    
b=`zgrep 'Queue for uuid 23fef66b-fcf0-4a71-8ca3-a0761dffc473' reader_$date.gz`

echo $a
echo $b

output:

Enter uuid 23fef66b-fcf0-4a71-8ca3-a0761dffc473                                         
Enter date in format yyyymmdd: 20180323
23fef66b-fcf0-4a71-8ca3-a0761dffc473
20180323

[2018-03-23 17:27:10,535: INFO/Worker-1 None None tasks/push_to_rabbit] Queue for uuid 23fef66b-fcf0-4a71-8ca3-a0761dffc473 is 35.154.190.22_2_k_event

Why is variable a empty?

10
  • 1
    And did fixing the typo make your code work? If not, then please create a minimal reproducible example - that means something the rest of us can replicate without needing your input files. Commented Mar 26, 2018 at 16:47
  • 2
    Actually, it won't have fixed the problem, because you used single quotes around $uuid where you wanted double quotes (so that the parameter could be expanded). Commented Mar 26, 2018 at 16:48
  • 1
    Try this. It should be working as @TobySpeight suggested. a=zgrep "Queue for uuid $uuid" reader_$date.gz b=zgrep 'Queue for uuid 23fef66b-fcf0-4a71-8ca3-a0761dffc473' reader_$date.gz Commented Mar 26, 2018 at 16:54
  • 1
    Just tried and working fine for me. Replace with double quotes to allow expansion of variable. Commented Mar 26, 2018 at 16:56
  • 1
    @vishnunarayanan thanks it is working. replacing single quote with double quote. worked.Now variable a has same value as b Commented Mar 26, 2018 at 17:01

2 Answers 2

3

You need double-quotes where parameters should be expanded, not single-quotes.

Wrong quotation:

u=23; d=3; a=$(zgrep 'Mar $u 23' /var/log/syslog.$d.gz); echo $a

Right quotation:

u=23; d=3; a=$(zgrep "Mar $u 23" /var/log/syslog.$d.gz); echo $a
Mar 23 23:00:01 tux201t CRON[25808]: (stefan) CMD ....
Sign up to request clarification or add additional context in comments.

Comments

3

Replace single quotes with double quotes to allow for bash variable substitution.

From

a=`zgrep 'Queue for uuid $uuid' reader_$date.gz`

To

a=`zgrep "Queue for uuid $uuid" reader_$date.gz`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.