Has AudioScope found a bug in bash 4.4.5?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Has AudioScope found a bug in bash 4.4.5?
# 1  
Old 05-24-2017
Has AudioScope found a bug in bash 4.4.5?

Using AudioScope.sh on Ubuntu 17.04 from a live DVD disc I came across an error.
Consider the code below it is a MUCH shortened version of the KEYBOARD input in AudioScope.
Code:
#!/bin/bash
bash --version
uname -a
status=0
KEYBOARD()
{
	read -r -p "Enter QUIT or EXIT to quit:- " kbinput
	if [ "$kbinput" == "QUIT" ] || [ "$kbinput" == "EXIT" ]
	then
		status=255
		break
	fi
	if [ "$kbinput" == "TEST" ]
	then
		echo "Hello World!"
	fi
}
while true
do
	echo "This will loop and hold until keyboard input is quitted."
	KEYBOARD
done
echo "you are here..."

When run in OSX 10.12.5 this is the expected result:-
Code:
Last login: Wed May 24 16:37:36 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ./kb_loop.sh
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
Copyright (C) 2007 Free Software Foundation, Inc.
Darwin Barrys-MBP 16.6.0 Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELEASE_X86_64 x86_64
This will loop and hold until keyboard input is quitted.
Enter QUIT or EXIT to quit:- TEST
Hello World!
This will loop and hold until keyboard input is quitted.
Enter QUIT or EXIT to quit:- QUIT
you are here...
AMIGA:amiga~/Desktop/Code/Shell> _

When run in Linux Mint 18 I get the same expected result from a LIVE DVD:-
Code:
mint@mint ~ $ chmod 755 kb_loop.sh
mint@mint ~ $ ./kb_loop.sh
GNU bash, version 4.3.42(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Linux mint 4.4.0-21-generic #37-Ubuntu SMP Mon Apr 18 18:33:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
This will loop and hold until keyboard input is quitted.
Enter QUIT or EXIT to quit:- TEST
Hello World!
This will loop and hold until keyboard input is quitted.
Enter QUIT or EXIT to quit:- QUIT
you are here...
mint@mint ~ $ _

HOWEVER running from a Ubuntu 17.04 current LIVE DVD This is what ensues:-
Code:
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

ubuntu@ubuntu:~$ chmod 755 kb_loop.sh
ubuntu@ubuntu:~$ ./kb_loop.sh
GNU bash, version 4.4.5(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Linux ubuntu 4.10.0-19-generic #21-Ubuntu SMP Thu Apr 6 17:04:57 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
This will loop and hold until keyboard input is quitted.
Enter QUIT or EXIT to quit:- TEST
Hello World!
This will loop and hold until keyboard input is quitted.
Enter QUIT or EXIT to quit:- QUIT
./kb_loop.sh: line 11: break: only meaningful in a `for', `while', or `until' loop
This will loop and hold until keyboard input is quitted.
Enter QUIT or EXIT to quit:- _

NOTE the error:-
./kb_loop.sh: line 11: break: only meaningful in a `for', `while', or `until' loop
And it keeps looping with the same error.

Is this a bug in bash 4.4.5, as bash 4.3.42 and presumably earlier is perfectly fine?!

At what point between 4.3.42 and 4.4.5 would this have broken I wonder?

Can anyone else confirm this error please...

TIA.

Barry.
# 2  
Old 05-24-2017
What it says is perfectly true: 'break' is only meaningful inside a 'for', 'while', or 'until' loop. You can't use it to break a loop outside of your function.

It shouldn't have accepted that before, and probably wasn't doing quite exactly what you planned. You should use return codes instead, to inform whatever's calling keyboard whether it succeeded or failed. That will let you ditch the 'status' variables as well.

Your loop should instead look something like

Code:
while KEYBOARD
do
...
done

And the function would be
Code:
KEYBOARD()
{
	read -r -p "Enter QUIT or EXIT to quit:- " kbinput || return 1

	if [ "$kbinput" == "QUIT" ] || [ "$kbinput" == "EXIT" ]
	then
                return 1 # Nonzero return will break while loop
	elif [ "$kbinput" == "TEST" ]
	then
		echo "Hello World!"
	fi

        return 0 # Zero return will not break while loop
}


Last edited by Corona688; 05-24-2017 at 02:07 PM..
These 3 Users Gave Thanks to Corona688 For This Post:
# 3  
Old 05-24-2017
You can simplify that further with a case:

Code:
KEYBOARD()
{
	read -r -p "Enter QUIT or EXIT to quit:- " kbinput || return 1

        case "$kbinput" in
        QUIT|EXIT)        return 1 ;;
        TEST)        echo "Hello World" ;;
        esac

        return 0
}

# 4  
Old 05-25-2017
Hi.
Code:
$ shellcheck z7

In z7 line 4:
status=0
^-- SC2034: status appears unused. Verify it or export it.


In z7 line 10:
                status=255
                ^-- SC2034: status appears unused. Verify it or export it.


In z7 line 11:
                break
                ^-- SC2104: In functions, use return instead of break.

On a system like:
Code:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
bash GNU bash 4.3.30

Some details on shellcheck:
Code:
shellcheck      analyse shell scripts (man)
Path    : /usr/bin/shellcheck
Version : ShellCheck - shell script analysis tool
Type    : ELF 64-bit LSB executable, x86-64, version 1 (SYSV ...)
Help    : probably available with -h
Repo    : Debian 8.7 (jessie) 
Home    : http://hackage.haskell.org/package/ShellCheck (pm)

Best wishes ... cheers, drl
# 5  
Old 05-26-2017
(On holiday at the moment so a limited reply.)
Well what should have happened until bash version 4.4.5 and what actully happens is not the same.
Code:
#!/bin/bash
bash --version
uname -a
status=0
KEYBOARD()
{
	read -r -p "Enter QUIT or EXIT to quit:- " kbinput
	if [ "$kbinput" == "QUIT" ] || [ "$kbinput" == "EXIT" ]
	then
		status=255
		break
	fi
	if [ "$kbinput" == "TEST" ]
	then
		echo "Hello World!"
	fi
}
while true
do
	echo "This will loop and hold until keyboard input is quitted."
	KEYBOARD
done
echo "You are here..."
echo "Status = $status..."

Resukts:-
Code:
Last login: Fri May 26 09:45:32 on ttys000
AMIGA:barrywalker~> chmos 755 test.sh
-bash: chmos: command not found
AMIGA:barrywalker~> chmod 755 test.sh
AMIGA:barrywalker~> ./test.sh
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
Copyright (C) 2007 Free Software Foundation, Inc.
Darwin Patricias-iMac.local 16.6.0 Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELEASE_X86_64 x86_64
This will loop and hold until keyboard input is quitted.
Enter QUIT or EXIT to quit:- TEST
Hello World!
This will loop and hold until keyboard input is quitted.
Enter QUIT or EXIT to quit:- EXIT
You are here...
Status = 255...
AMIGA:barrywalker~> _

This is the same on bash version 4.3.42.
The status is/was exactly as I want it except I have changed the code now whilst away on a break!
Will be home again soon and get back to you fully...
# 6  
Old 05-26-2017
Quote:
Originally Posted by wisecracker
(On holiday at the moment so a limited reply.)
Well what should have happened until bash version 4.4.5 and what actually happens is not the same.
That it ever worked is, without exaggeration, the worst BASH bug I have seen to date. That behavior is explicitly barred in every language I know, SH, C, AWK, PERL -- even tinkertoys like Forth where I often wish it wasn't. break should not leap outside its local context into brave new worlds. Control statements control local blocks only.
Quote:
The status is/was exactly as I want it except I have changed the code now whilst away on a break!
I'm sorry, but there's nothing for it but to change your ways. You were relying on accidental, broken behavior. Your code will benefit greatly from learning how to use exit codes.

Last edited by Corona688; 05-26-2017 at 01:29 PM..
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 05-27-2017
Hi Corona688.

Still on a break with limited internet access but my 'ways are now changed'... ;o)
Well using the return method suits me fine.

I had no idea that this bug was near decades old and I was _exploiting_ it.
Thanks for pointing me in the right direction.

Here is a slightly simplified version of yours using dash as the prime mover.
OSX 10.12.5, default bash terminal calling 'dash' from the script...
I still get my variables where I want them so I am a happy bunny...
Code:
Last login: Sat May 27 10:20:46 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> cat kb_loop2.sh
#!/usr/local/bin/dash
# This passes the ShellCheck test as /bin/sh.
TEXT="Thank you Corona688."
status=0
KEYBOARD()
{
	printf "Enter QUIT to quit:- "
	read -r kbinput
	if [ "$kbinput" = "QUIT" ]
	then
		TEXT="YOU ARE HERE!"
		status=255
		echo "Exiting the KEYBOARD() function..."
                return 1
	elif [ "$kbinput" = "TEST" ]
	then
		echo "Hello World!"
	fi
        return 0
}
# Main loop...
while true
do
	echo "This will loop and hold until keyboard input is quitted."
	KEYBOARD
	if [ $? -eq 1 ]
	then
		break
	fi
done
echo "Now outside the loop..."
echo "$TEXT"
echo "Status = $status..."
AMIGA:amiga~/Desktop/Code/Shell> 
AMIGA:amiga~/Desktop/Code/Shell> 
AMIGA:amiga~/Desktop/Code/Shell> ./kb_loop2.sh
This will loop and hold until keyboard input is quitted.
Enter QUIT to quit:- NOTHING
This will loop and hold until keyboard input is quitted.
Enter QUIT to quit:- TEST
Hello World!
This will loop and hold until keyboard input is quitted.
Enter QUIT to quit:- QUIT
Exiting the KEYBOARD() function...
Now outside the loop...
YOU ARE HERE!
Status = 255...
AMIGA:amiga~/Desktop/Code/Shell> _

Thank you.
This User Gave Thanks to wisecracker For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. OS X (Apple)

AudioScope Project.

AudioScope Project. (Apologies for any typos.) For the few following...... AudioScope.sh... Now at Version 0.60.00. Well this baby has come a long way since its inception in January 2013. It is now at Version 0.60.00. It is MUCH more Apple centric now with a new OSX Sierra minimum _silent_... (7 Replies)
Discussion started by: wisecracker
7 Replies

2. What is on Your Mind?

AudioScope...

Boy oh boy, with only a MONO mic input to use AudioScope gets much more difficult when the ALTDC board is included. It needs, so far, two hits at the MIC input with a single hit at the HEADPHONE audio output. The first at the highest practical resolution for the AC component and the second... (0 Replies)
Discussion started by: wisecracker
0 Replies

3. Shell Programming and Scripting

-bash-3.2$: not found

I am wondering if someone can help me out. I am new to oracle and given a task to install Oracle 11g on Solaris. I am running into some major problems since last week since I can't seem to get it to work. I can't start GUI, tried different blogs but no luck. Then, I decided to install it in a... (4 Replies)
Discussion started by: newborndba
4 Replies

4. UNIX for Dummies Questions & Answers

Im new to bash scriping and i found this expression on a bash script what does this mean.

# check host value regex='^(||1|2|25)(\.(||1|2|25)){3}$' if ')" != "" ]; then if ]; then echo host $host not found exit 4 fi elif ]; then echo $host is an invalid host address exit 5 fi espeacailly the top regex part? ---------- Post updated at 06:58 PM ---------- Previous update was... (1 Reply)
Discussion started by: kevin298
1 Replies

5. Shell Programming and Scripting

bash:vi:command not found

I downloaded and installed "Cygwin yesterday onto my PC running Windows XP. When I tried to type "vi" in Cygwin's window, I got the following message bash: vi: Command not found What shud i do inorder to get into vi editor Thanks (10 Replies)
Discussion started by: bobby1015
10 Replies

6. Shell Programming and Scripting

mv command not found bug

foreach x ( *.foo) echo "move file?" set move=$< if($move == y) then echo "enter new pathname:" set path=$< mv $x $path/$x endif end ok guys, im creating this script so i can move files with *.foo extensions and *.bar... (6 Replies)
Discussion started by: pantelis
6 Replies

7. Shell Programming and Scripting

bash-function with array acting bizarre, bug?

Hello, basically what this script is supposed to do is showing a list of hosts that is given a number, that you will be able to choose from a list. A check is made to verify that the chosen number is within the array and this is where things go bad and I don't know why, bizarre. I've spent... (5 Replies)
Discussion started by: gand
5 Replies

8. Post Here to Contact Site Administrators and Moderators

Have I found a bug?

When searching for new posts, I see that my voting in one of the polls counts as a 'new post'. However, while the '<blah> minutes ago' entry updates correctly, the 'by <username>' is the last user to actually post a comment in the poll instead. Result: Poll: vB Guest Book 39... (4 Replies)
Discussion started by: Smiling Dragon
4 Replies
Login or Register to Ask a Question