LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 06-07-2014, 09:52 AM   #1
dederon
Member
 
Registered: Oct 2013
Posts: 94

Rep: Reputation: 46
Bashism in /etc/rc.d/rc.S


as an experiment (and to get rid of bash) i changed /bin/sh to point to /bin/mksh.

the boot process fails at line 109 in /etc/rc.d/rc.S:

eval LUKSARRAY=( $line )

This line, and the following lines

LUKS="${LUKSARRAY[0]}"
DEV="${LUKSARRAY[1]}"
PASS="${LUKSARRAY[2]}"
OPTS="${LUKSARRAY[3]}"

look like bash code to me. Affected is X86_64 current and i686 14.1.

I am not an shell expert, can someone confirm this? Or provide a simple solution? Otherwise I will sot down and write a patch to solve this issue.

heiko

Last edited by dederon; 06-07-2014 at 09:57 AM.
 
Old 06-07-2014, 10:18 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,843

Rep: Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472
Why would you switch to the korn shell?

If you think that little bashism is bad, just wait... nearly all the startup scripts have bashisms in them.

I could understand switching to something like ash or busybox (useful for very small distributions)... but the korn shell?

Last edited by jpollard; 06-07-2014 at 10:19 AM.
 
Old 06-07-2014, 10:21 AM   #3
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,314
Blog Entries: 18

Rep: Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082
Yep, unfortunately a number of important Slackware initscripts contain bashisms.

Gilbert did some nice work on POSIX compliant startup-scripts a while back, which you may find useful:
http://www.linuxquestions.org/questi...1/#post5156115

Only problem is that once you start making changes like this you're stepping outside of "Official Slackware" and that may make your system maintenance more difficult when Pat ships a new set of init-scripts.
 
Old 06-07-2014, 10:28 AM   #4
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,314
Blog Entries: 18

Rep: Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082
Quote:
Originally Posted by jpollard View Post
Why would you switch to the korn shell?
Maybe he just prefers it, or wants a sh that is more POSIXly correct. Personally, I'd also prefer that /bin/sh didn't point to bash.
 
Old 06-07-2014, 10:42 AM   #5
dederon
Member
 
Registered: Oct 2013
Posts: 94

Original Poster
Rep: Reputation: 46
Quote:
Originally Posted by GazL View Post
Gilbert did some nice work on POSIX compliant startup-scripts a while back, which you may find useful:
http://www.linuxquestions.org/questi...1/#post5156115
i wonder why i missed this topic.

i don't mind using bash for the rc scripts, but if bash is required a "/bin/bash" shebang line should be used instead of "/bin/sh".
 
Old 06-07-2014, 11:29 AM   #6
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2.1.2 on Lenovo Thinkpad W520
Posts: 9,116

Rep: Reputation: Disabled
Well, I think that Pat just follows the old saying here "if it ain't broke, don't fix it". If he were to rewrite all the init scripts from scratch, maybe he would use another syntax here and there. But there is no need for that as far as I know (please no more flame war about _you_know_what_).

Last edited by Didier Spaier; 06-07-2014 at 12:44 PM. Reason: Last sentence deleted, usless and statemrnt not checked.
 
Old 06-07-2014, 11:50 AM   #7
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,322

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I giggled when I saw this recently.
From pm-functions (part of pm-utils)
Code:
#!/bin/sh
...
# Simple little logging function.
# We do it this way because 'echo -n' is not posix.
log()
{
	is_set "$LOGGING" || return 0;
	local fmt='%s\n'
	[ "$1" = "-n" ] && { fmt='%s'; shift; }
	printf "$fmt" "$*"
}
...
# always fall back to kernel methods if nothing else was declared

if [ -z "$SUSPEND_MODULE" ]; then
	if grep -q mem /sys/power/state; then
		SUSPEND_MODULE="kernel"
		do_suspend() { echo -n "mem" >/sys/power/state; }
	elif [ -c /dev/pmu ] && pm-pmu --check; then
		SUSPEND_MODULE="kernel"
		do_suspend() { pm-pmu --suspend; }
	elif grep -q standby /sys/power/state; then
		SUSPEND_MODULE="kernel"
		do_suspend() { echo -n "standby" >/sys/power/state; }
	fi
fi
Just an example of POSIX compliance being inconsistently applied in upstream and how POSIX compliance can result in some ugly code constructions.

BTW There are also bashisms in the code, despite the /bin/sh shebang.
Removing bash is a larger task than just looking at the init scripts.

Last edited by allend; 06-07-2014 at 12:02 PM.
 
2 members found this post helpful.
Old 06-07-2014, 12:34 PM   #8
drmozes
Slackware Contributor
 
Registered: Apr 2008
Location: Surrey, England
Distribution: Slackware
Posts: 865

Rep: Reputation: 652Reputation: 652Reputation: 652Reputation: 652Reputation: 652Reputation: 652
Quote:
Originally Posted by Didier Spaier View Post
Well, I think that Pat just follows the old saying here "if it ain't broken, don't fix it". If he were to rewrite all the init scripts from scratch, maybe he would use another syntax here and there. But there is no need for that as far as I know (please no more flame war about _you_know_what_).
Well, what would the point be? bash is a 'required' package in Slackware, meaning that we know some parts of the OS rely upon it.
You'd remove bash at your peril.

I added bashisms into the initrd 'init' script because it meant I could achieve a string manipulation inside of bash rather than forking to some other utilities. In my opinion since you're using a particular language which is mandated to be present, you should exercise its abilities where possible - particularly if it helps improve efficiency.

If these scripts were intended to be used outside of Slackware then it would make sense to make them as POSIX compliant as possible to maximise their usability. But they aren't.

Last edited by drmozes; 06-07-2014 at 12:38 PM.
 
3 members found this post helpful.
Old 06-07-2014, 01:09 PM   #9
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2.1.2 on Lenovo Thinkpad W520
Posts: 9,116

Rep: Reputation: Disabled
Quote:
Originally Posted by drmozes View Post
I added bashisms into the initrd 'init' script because it meant I could achieve a string manipulation inside of bash rather than forking to some other utilities.
I guess you mean /etc/rc.d/rc.S in the initrd? I ask because in Slackware initrd as well as in intrd-versatile.img (I just had a look there) /sbin/init links to /bin/busybox.
 
Old 06-07-2014, 01:14 PM   #10
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,897

Rep: Reputation: 578Reputation: 578Reputation: 578Reputation: 578Reputation: 578Reputation: 578
Any initrd is a special case. In this case, whether you mean the intrd from the installer, or one generated by mkinitrd, bash is not included. rc.S and all other init scripts are not used by the initrd. Any scripts run by the initrd will be busybox/sh compatible -as are doinst.sh scripts.
 
Old 06-07-2014, 01:39 PM   #11
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2.1.2 on Lenovo Thinkpad W520
Posts: 9,116

Rep: Reputation: Disabled
Quote:
Originally Posted by gnashley View Post
In this case, whether you mean the intrd from the installer, or one generated by mkinitrd, bash is not included.
Bash is included at least in initird-versatile.img for Slackware ARM, I just checked, and it is a standalone executable.
Quote:
rc.S and all other init scripts are not used by the initrd.
/etc/rc.d/rc.S is included and used in Slackware installer's initrd. Of course that's not the same as in the installed Slackware.
Quote:
Any scripts run by the initrd will be busybox/sh compatible -as are doinst.sh scripts.
That's what I thought, but if I understand well what Stuart said that's wrong. I didn't check myself (yet).

Last edited by Didier Spaier; 06-07-2014 at 01:40 PM.
 
Old 06-07-2014, 02:34 PM   #12
drmozes
Slackware Contributor
 
Registered: Apr 2008
Location: Surrey, England
Distribution: Slackware
Posts: 865

Rep: Reputation: 652Reputation: 652Reputation: 652Reputation: 652Reputation: 652Reputation: 652
Quote:
Originally Posted by Didier Spaier View Post
I guess you mean /etc/rc.d/rc.S in the initrd? I ask because in Slackware initrd as well as in intrd-versatile.img (I just had a look there) /sbin/init links to /bin/busybox.
I was confused where I'd made the change- it was actually mkinitrd. One would have to be careful with the initrd itself since it only has Busybox. The installer uses bash.
 
Old 06-07-2014, 02:36 PM   #13
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,843

Rep: Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472
About the only place I would expect bashisms to be rare would be those multi-platform packages that are expected on things like AIX and SunOS. Though even there, bash is also frequently installed on the system.
 
Old 06-07-2014, 02:38 PM   #14
TommyC7
Member
 
Registered: Mar 2012
Distribution: Slackware, CentOS, OpenBSD, FreeBSD
Posts: 503

Rep: Reputation: Disabled
Quote:
dederon:
LUKS="${LUKSARRAY[0]}"
DEV="${LUKSARRAY[1]}"
PASS="${LUKSARRAY[2]}"
OPTS="${LUKSARRAY[3]}"
Yes these are bashisms.

First, the LUKSARRAY array is created via:

Code:
cat /etc/crypttab | grep -v "^#" | grep -v "^$" | while read line; do ... ; done
It removes empty lines and any commented lines then reads each line. Each line that is read gets put into an array and each element of the array is then put into it's own variable.

On a secondary note, that could be replaced with:
Code:
egrep -v '(^#|^$)' /etc/crypttab | while read -r line; do ... ; done
Quote:
as an experiment (and to get rid of bash) i changed /bin/sh to point to /bin/mksh.
I don't know too much about mksh specifically, but ksh itself still comes with arrays (some of them are interchangeable with bash-arrays). So even by pointing the symlink to ksh there might be some things that you missed in this little endeavor.

I see a related thread in the "Similar Threads" section and it asks "anyone uses ash as /bin/sh?" which is probably the closest to a POSIX/bourne shell that Slackware has.

There are some things that aren't part of the bourne shell or defined by POSIX in the almquist shell (local for example), but it's pretty close.
 
Old 06-07-2014, 02:57 PM   #15
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,314
Blog Entries: 18

Rep: Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082Reputation: 3082
I actually don't mind bash when it's running in bash mode, and some of the bashisms are actually quite useful. It's when it's trying to take on its POSIX persona when run as 'sh' that you start to hit problems, particularly around shell invocation.

BTW, doesn't debian use 'dash' to provide their /bin/sh?
 
  


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
Bashism in rc.udev - anyone uses ash as /bin/sh ? Camarade_Tux Slackware 3 03-02-2008 03:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 04:15 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration