Variable being interpreted as a command in a bash script
Hello,
I have a bash script to do some security tasks for new Ubuntu or Red Hat based servers.
As one of the steps, if epel isn't supported, I add it to facilitate installation of fail2ban.
I collect the information needed and store it in variables:
# Determine architecture and store in "arch" variable arch=$( /bin/arch ); # Determine major release and store in "release" variable osRelease=$( cat /etc/*os-release | grep PRETTY_NAME | cut -d " " -f 5 | cut -d "." -f 1 );
I then use the variables in a command to support epel:
sudo "subscription-manager repos --enable codeready-builder-for-rhel-"$(osRelease)"-"$(arch)"-rpms"
When the command runs, the place where osRelease should be is empty and when it runs, I get an osRelease: command not found error when it reaches that line in the script:
./first-ten.sh: line 337: release: command not found Error: 'codeready-builder-for-rhel--x86_64-rpms' does not match a valid repository ID. Use "subscription-manager repos --list" to see valid repositories.
Note that the number 9 should be between the hyphens between rhel and x86_64, but it is empty.
When I run echo $osRelease, I get the number 9, which is correct.
I believe I'm missing something simple but have not been able to figure it out yet. Any insight is much appreciated!
Full script is available on github here: https://github.com/TedLeRoy/first-ten-seconds-redhat-ubuntu/blob/master/first-ten.sh
Any thoughts on script improvements are most welcome too.
Thanks!
Best Answer
-
Got it working. Had to get rid of the ()'s and some of the "'s. Working line:
sudo subscription-manager repos --enable codeready-builder-for-rhel-"$osRelease"-"$arch"-rpms0
Answers
-
In the captured text, it says
release: command not found, but I changed the variable name to osRelease in case release is a system variable. Same result. It now saysosRelease: command not found. Sorry for any confusion this may cause.0 -
Run of setting the variables and running the relevant line from the script:
[tedl@localhost ~]$ osRelease=$( cat /etc/*os-release | grep PRETTY_NAME | cut -d " " -f 5 | cut -d "." -f 1 ); [tedl@localhost ~]$ echo $osRelease 9 [tedl@localhost ~]$ arch=$( /bin/arch ); [tedl@localhost ~]$ echo $arch x86_64 [tedl@localhost ~]$ sudo subscription-manager repos --enable codeready-builder-for-rhel-"$(osRelease)"-"$(arch)"-rpms -bash: osRelease: command not found [sudo] password for tedl: Error: 'codeready-builder-for-rhel--x86_64-rpms' does not match a valid repository ID. Use "subscription-manager repos --list" to see valid repositories.
The arch variable populates and works fine while osRelease does not, and osRelease is interpreted as a command.
0
Categories
- All Categories
- 177 LFX Mentorship
- 177 LFX Mentorship: Linux Kernel
- 766 Linux Foundation IT Professional Programs
- 378 Cloud Engineer IT Professional Program
- 174 Advanced Cloud Engineer IT Professional Program
- 75 DevOps IT Professional Program - Discontinued
- 7 DevOps & GitOps IT Professional Program
- 101 Cloud Native Developer IT Professional Program
- 7.6K Training Courses & Learning Paths
- 4 AI & ML Training
- 1 Blockchain & Decentralized Identity Training
- 10 Cloud & Containers Training
- 1 Cybersecurity Training
- 2 DevOps & Site-Reliability Training
- 1 Linux Kernel Development Training
- 1 Networking Training
- 2 Open Source Best Practice Training
- 2 System Administration Training
- 1 System Engineering Training
- 1 Web & Application Development Training
- 796 Hardware
- 202 Drivers
- 68 I/O Devices
- 37 Monitors
- 95 Multimedia
- 173 Networking
- 91 Printers & Scanners
- 91 Storage
- 770 Linux Distributions
- 81 Debian
- 68 Fedora
- 23 Linux Mint
- 13 Mageia
- 24 openSUSE
- 150 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 356 Ubuntu
- 465 Linux System Administration
- 31 Cloud Computing
- 73 Command Line/Scripting
- Github systems admin projects
- 98 Linux Security
- 78 Network Management
- 101 System Management
- 46 Web Management
- 116 Mobile Computing
- 20 Android
- 81 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 395 Off Topic
- 121 Introductions
- 30 Study Material
- 997 Programming and Development
- 310 Kernel Development
- 669 Software Development
- 1K Software
- 397 Applications
- 182 Command Line
- 5 Compiling/Installing
- 69 Games
- 318 Installation
- Archived
- 183 Small Talk
- 2 LFD140 Class Forum
- 1.4K LFS258 Class Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)