LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-26-2021, 04:34 PM   #1
Chuck56
Member
 
Registered: Dec 2006
Location: Colorado, USA
Distribution: Slackware
Posts: 744

Rep: Reputation: 293Reputation: 293Reputation: 293
Bash command substitution, subshell & redirect


I'm writing a bash script which needs the system bootloader type before taking additional actions. I'm focused primarily on LILO & EFI for Slackware systems but it should work for other bootloader types as listed in the table below.

I'm confused how the addition of a command substitution with a redirect to fd4 inside a subshell which is echoed inside another command substitution works?

How does the 80 in the 1st command convert to 5 in the 2nd command?

Code:
bash-5.1$ cat /proc/sys/kernel/bootloader_type
80

bash-5.1$ echo $(($(cat /proc/sys/kernel/bootloader_type)>>4))
5
Code:
		     THE LINUX/x86 BOOT PROTOCOL
		     ---------------------------

  Assigned boot loader ids (hexadecimal):

	0  LILO			(0x00 reserved for pre-2.00 bootloader)
	1  Loadlin
	2  bootsect-loader	(0x20, all other values reserved)
	3  Syslinux
	4  Etherboot/gPXE/iPXE
	5  ELILO
	7  GRUB
	8  U-Boot
	9  Xen
	A  Gujin
	B  Qemu
	C  Arcturus Networks uCbootloader
	D  kexec-tools
	E  Extended		(see ext_loader_type)
	F  Special		(0xFF = undefined)
       10  Reserved
       11  Minimal Linux Bootloader <http://sebastian-plotz.blogspot.de>
       12  OVMF UEFI virtualization stack

  Please contact <hpa@zytor.com> if you need a bootloader ID
  value assigned.
I found the original command on this page: https://unix.stackexchange.com/quest...ind-bootloader

Bash never ceases to amaze me with its never ending idiosyncrasies!
 
Old 05-26-2021, 04:46 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,599

Rep: Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933
Quote:
Originally Posted by Chuck56 View Post
I'm confused how the addition of a command substitution with a redirect to fd4 inside a subshell
That's not command substitution. It's Arithmetic Expansion.

Shell Arithmetic
Quote:
<< >>
left and right bitwise shifts
Code:
$ echo $((80 >> 4))
5
$ echo $((80 / (2*2*2*2) ))
5
$ echo $((80 >> 1))
40
$ echo $((80 >> 2))
20
$ echo $((80 >> 3))
10
$ echo $((80 / 2))
40
$ echo $((80 / (2*2) ))
20
$ echo $((80 / (2*2*2) ))
10
 
2 members found this post helpful.
Old 05-26-2021, 04:47 PM   #3
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,923

Rep: Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899
That's not a redirection, You're doing a binary shift right by 4. So,
80 = 01010000 right-shifted 4 places to the right = 0101 = 5

>&4 would be a redirection to fd4.

if you're just trying to get the value into your script, I'd just do

bl_type=$(< /proc/sys/kernel/bootloader_type ) and then work with $bl_type just like any other variable.
 
1 members found this post helpful.
Old 05-26-2021, 10:00 PM   #4
Chuck56
Member
 
Registered: Dec 2006
Location: Colorado, USA
Distribution: Slackware
Posts: 744

Original Poster
Rep: Reputation: 293Reputation: 293Reputation: 293
Thanks ntubski & GazL! So Arithmetic Expansion is something I need to read up on.
 
Old 05-27-2021, 12:31 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,741

Rep: Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790
Redirection with an extra fd

Redirection to file descriptor 4 goes with
Code:
cat /proc/sys/kernel/bootloader_type >&4
The descriptor must be already assigned to an opened file.
Either by calling the script or function with
Code:
myscript 4>>filename
Code:
myfunc(){
# fd 4 is opened for writing
cat /proc/sys/kernel/bootloader_type >&4
}
myfunc 4>>filename
Or by redirecting a code block
Code:
{
# fd 4 is opened for writing here
cat /proc/sys/kernel/bootloader_type >&4
} 4>>filename
(while-done, for-done, if-fi, case-esac are also code blocks.)
Or by an exec
Code:
exec 4>>filename
# fd 4 is open for writing till the end of the script or till another changing exec
cat /proc/sys/kernel/bootloader_type >&4
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash/fluxbox: subshell vs command grouping porphyry5 Programming 12 07-09-2013 10:37 AM
Making bash script WAIT after starting subshell mintybadger Programming 9 08-04-2011 04:49 AM
bash strange subshell command result fenor Programming 12 11-26-2009 07:35 AM
bash script and exporting variables into a subshell spork Programming 3 10-14-2008 11:38 PM
Gotta love those &#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&# iLLuSionZ Linux - General 5 11-18-2003 07:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration