0

I have below text in a file bacula-dir.conf

Job {
  Name = testbox_db_daily
  Type = Backup
  Client = testbox.domain.com-fd
  FileSet = testbox_fileset
  Schedule = testbox_cycle
  Storage = testbox_File
  Pool = testbox_two_week_daily
  Messages = Standard
  Allow Mixed Priority = yes
  Max Run Sched Time = 86400
   Enabled = no
}

Job {
  Name = testbox_db_weekly        ### for weekly full backup
  Type = Backup
  Client = testbox.domain.com-fd
  FileSet = testbox_fileset_weekly
  Schedule = testbox_cycle_weekly
  Storage = testbox_File
  Pool = testbox_two_week_weekly
  Messages = Standard
  Allow Mixed Priority = yes
  Max Run Sched Time = 86400
}

Below is the script

[root@host1 ~]# cat script.sh
#!/bin/bash
set -x
#trap read debug
read -p "Enter the job name : " job_name
printf "Entered job name is : $job_name\n"
job_output=$(sed  -n '/$job_name/,${p;/^}/q}' bacula-dir.conf)
echo "$job_output"
[root@host1 ~]#

Output through script-:

[root@host1 ~]# ./script.sh
+ read -p 'Enter the job name : ' job_name
Enter the job name : testbox_db_weekly
+ printf 'Entered job name is : testbox_db_weekly\n'
Entered job name is : testbox_db_weekly
++ sed -n '/$job_name/,${p;/^}/q}' bacula-dir.conf
+ job_output=
+ echo ''

[root@host1 ~]#

Output through command line -:

[root@host1 ~]# job_output=$(sed  -n '/testbox_db_weekly/,${p;/^}/q}' bacula-dir.conf)
[root@host1 ~]# echo "$job_output"
  Name = testbox_db_weekly        ### for weekly full backup
  Type = Backup
  Client = testbox.domain.com-fd
  FileSet = testbox_fileset_weekly
  Schedule = testbox_cycle_weekly
  Storage = testbox_File
  Pool = testbox_two_week_weekly
  Messages = Standard
  Allow Mixed Priority = yes
  Max Run Sched Time = 86400
}
[root@host1 ~]#

Requirement is when trying to search with job name through user input, need to get the output of the block. Command is working fine when passing with jobname through command line like above. But when trying to take input from user, output is not generating.

Tried with below option also "-e", but it is generating output of the complete file.

job_output=$(sed  -e '/$job_name/,${p;/^}/q}' bacula-dir.conf)
anji
  • 103
  • 1
  • 1
  • 6
  • 1
    Variables are only expanded inside double quotes, not single quotes. – Barmar Aug 22 '21 at 18:36
  • when trying with double quotes, getting below error. Please tell me where i am doing wrong. [root@host1 ~]# job_output=$(sed -n "/$job_name/,${p;/^}/q}" bacula-dir.conf) -bash: /$job_name/,${p;/^}/q}: bad substitution [root@host1 ~]# – anji Aug 22 '21 at 19:06
  • 1
    You need to escape the `$` that should be the `sed` address. – Barmar Aug 22 '21 at 19:07

0 Answers0