0

I am trying to write a bash script to automatically print the date command but unfortunately, it is not working.

here is a snippet of the script... I have used 'EOF' as most suggested, but still not working.

#!/bin/bash
cat<<'EOF'>/etc/yum.repos.d/local.repo
[local]
name=Red Hat Enterprise Linux $releasever - $basearch - Base
baseurl=file:///home/user1/updates-$(date +%d%b%y)
gpgcheck=0
enabled=1
EOF

Could someone help here?

2 Answers 2

3

Don't quote your here-document. Quoting (any part of) the initial EOF marker will prevent the shell from performing any of the expansions in the document. Since you want the shell to handle the command substitution around date, you need to leave the document unquoted.

If you want $releasever and $basearch left unexpanded, you will need to escape the $ in front of it as shown here:

#!/bin/sh
cat <<EOF >/etc/yum.repos.d/local.repo
[local]
name=Red Hat Enterprise Linux \$releasever - \$basearch - Base
baseurl=file:///home/user1/updates-$(date +%d%b%y)
gpgcheck=0
enabled=1
EOF

Also note that this script is better executed by /bin/sh as it contains nothing bash-specific.

If the file that you are creating is a TOML file, you could also create it like so:

tomlq -n -t \
    --arg name 'Red Hat Enterprise Linux $releasever - $basearch - Base' \
    --arg baseurl "file:///home/user1/updates-$(date +%d%b%y)" \
    --argjson gpgcheck 0 \
    --argjson enabled 1 \
    '.local = $ARGS.named' >/etc/yum.repos.d/local.repo

This uses tomlq (a TOML parser wrapper around jq) and creates the TOML output

[local]
name = "Red Hat Enterprise Linux $releasever - $basearch - Base"
baseurl = "file:///home/user1/updates-29May22"
gpgcheck = 0
enabled = 1
1
  • Thanks, @kusalananda.. I just found that myself as well... thank you for your quick reply. Appreciate it
    – xrkr
    May 29, 2022 at 14:13
0

Ok, I think I found the answer... I have mistaken how to use the ''. So I modified the script as below.

#!/bin/bash
cat<<EOF>/etc/yum.repos.d/local.repo
[local]
name=Red Hat Enterprise Linux \$releasever - \$basearch - Base
baseurl=file:///home/user1/updates-$(date +%d%b%y)
gpgcheck=0
enabled=1
EOF

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .