[Solved] Sourcing in Konsole Subshell gives Syntax Errors

For discussions about programming and projects not necessarily associated with Porteus.
Bogomips
Full of knowledge
Full of knowledge
Posts: 2564
Joined: 25 Jun 2014, 15:21
Distribution: 3.2.2 Cinnamon & KDE5
Location: London

[Solved] Sourcing in Konsole Subshell gives Syntax Errors

Post#1 by Bogomips » 23 Apr 2015, 18:50

In subshell of Konsole tab sourcing a script file gave 3 errors, all very similar, from out of some 500 lines.

Code: Select all

guest@porteus:~/pk01$ sh
sh-4.2$ . ../p10/Por/Komodo/funcs.sh
sh: ../p10/Por/Komodo/pak.sh: line 28: syntax error near unexpected token `<'
sh: ../p10/Por/Komodo/pak.sh: line 28: `  readarray -t vec < <(cat $1.pak)'
sh: ../p10/Por/Komodo/pakdep.sh: line 159: syntax error near unexpected token `<'
sh: ../p10/Por/Komodo/pakdep.sh: line 159: `  readarray -t req   < <( cat reqmts )'
sh: ../p10/Por/Komodo/selpak.sh: line 21: syntax error near unexpected token `<'
sh: ../p10/Por/Komodo/selpak.sh: line 21: `  readarray -t rps   < <( cat ${rq}_ar.rep )'
Needless to say, sourcing in just the tab was error free :unknown:
Linux porteus 4.4.0-porteus #3 SMP PREEMPT Sat Jan 23 07:01:55 UTC 2016 i686 AMD Sempron(tm) 140 Processor AuthenticAMD GNU/Linux
NVIDIA Corporation C61 [GeForce 6150SE nForce 430] (rev a2) MemTotal: 901760 kB MemFree: 66752 kB

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Re: Sourcing from Konsole Subshell results in Syntax Errors

Post#2 by Rava » 28 Apr 2015, 00:50

I never saw a redirect of "< < [whatever", only as "< [whatever]"

Try to compare it with the other lines of your script.
Cheers!
Yours Rava

Bogomips
Full of knowledge
Full of knowledge
Posts: 2564
Joined: 25 Jun 2014, 15:21
Distribution: 3.2.2 Cinnamon & KDE5
Location: London

Re: Sourcing from Konsole Subshell results in Syntax Errors

Post#3 by Bogomips » 01 May 2015, 19:48

Rava wrote:I never saw a redirect of "< < [whatever", only as "< [whatever]"

Code: Select all

guest@porteus:~$ readarray -t vc <(cat p10/Por/Komodo/funcs.sh)
^C
guest@porteus:~$ readarray -t vc < (cat p10/Por/Komodo/funcs.sh)
bash: syntax error near unexpected token `('

guest@porteus:~$ readarray -t vec <<(cat p10/Por/Komodo/funcs.sh)
bash: syntax error near unexpected token `('

guest@porteus:~$ readarray -t vec < <(cat p10/Por/Komodo/funcs.sh)
guest@porteus:~$ echo ${#vec[*]}
28
Why this is the case? Search me! Got it from that fount of all wisdom: the Internet :Search:
Linux porteus 4.4.0-porteus #3 SMP PREEMPT Sat Jan 23 07:01:55 UTC 2016 i686 AMD Sempron(tm) 140 Processor AuthenticAMD GNU/Linux
NVIDIA Corporation C61 [GeForce 6150SE nForce 430] (rev a2) MemTotal: 901760 kB MemFree: 66752 kB

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Re: Sourcing from Konsole Subshell results in Syntax Errors

Post#4 by Rava » 01 May 2015, 20:01

[quote="Bogomips"]

Code: Select all

guest@porteus:~$ readarray -t vec < <(cat p10/Por/Komodo/funcs.sh)
guest@porteus:~$ echo ${#vec[*]}[/quote]
 O__o

Never saw that, and could not think what it can be good for, not even a clue what it means.

Seems, when you already now have such deep questions, go to the best place for all answers with coding, any kind of high language, or bash scripts: https://stackoverflow.com 8) 

Please post here what "type [scriptname]" says. Replace [scriptname] accordingly to the full path to your script...
Cheers!
Yours Rava

Bogomips
Full of knowledge
Full of knowledge
Posts: 2564
Joined: 25 Jun 2014, 15:21
Distribution: 3.2.2 Cinnamon & KDE5
Location: London

Re: Sourcing from Konsole Subshell results in Syntax Errors

Post#5 by Bogomips » 01 May 2015, 22:59

If I should do type pakfuncs.sh I fear I'll get a very long list of functions, so here the short and inexplicable:

Code: Select all

guest@porteus:~$ ls *.sh
pakdefs.sh  pakfuncs.sh
guest@porteus:~$ source pakfuncs.sh 
guest@porteus:~$ ver usm
3.1.7-3
guest@porteus:~$ sh
sh-4.2$ ver usm
sh: ver: command not found
sh-4.2$ source pakfuncs.sh
sh: source: pakfuncs.sh: file not found
sh-4.2$ ls *.sh
pakdefs.sh  pakfuncs.sh
sh-4.2$ . pakfuncs.sh 
sh: .: pakfuncs.sh: file not found
sh-4.2$ . ./pakfuncs.sh 
sh: ./pakfuncs.sh: line 231: syntax error near unexpected token `<'
sh: ./pakfuncs.sh: line 231: `  readarray -t vec < <(cat $1.pak)'
sh-4.2$ 
Suspect this is terminal implementation dependent.
Linux porteus 4.4.0-porteus #3 SMP PREEMPT Sat Jan 23 07:01:55 UTC 2016 i686 AMD Sempron(tm) 140 Processor AuthenticAMD GNU/Linux
NVIDIA Corporation C61 [GeForce 6150SE nForce 430] (rev a2) MemTotal: 901760 kB MemFree: 66752 kB

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Re: Sourcing from Konsole Subshell results in Syntax Errors

Post#6 by Rava » 02 May 2015, 02:27

.... Okay.
Please copy the below code in a text file and save it in a folder in $PATH. best would be /usr/local/bin

And since the script believes it is called fw cause I named it so and told it to believe in it's given name :D best call it "fw". Make it executable and run it with the parameter or any executable program you have in $PATH. [Be aware that the colourings, especially the bright red when having an error won't work for you. If interested I could post in Other ‹ Programming a HOWTO how I implementing; I already mentioned it in another thread about yet another or my scripts why I no longer hardcode it to it's fixed file name target in /usr/local/bin]

Code: Select all

#!/bin/bash
VERSION="0.3"
MYNAME="fw (file-which)"

test ! "$ECHO_COLORS"x = "x" && test -f $ECHO_COLORS && . $ECHO_COLORS

if [ $# -ne 1 ]; then
    echo -e $yel$bld$MYNAME$off$yel V$VERSION$off
    echo -e $bld${red}"You must specify *one* filename for which!"$off
    exit 1
else
    file `which "$1"` 2>/dev/null
    errorcode=$?
    if [ $errorcode -ne 0 ]; then
        echo -e $yel$bld$MYNAME$off$yel V$VERSION$off
	echo -e $bld$red"Errorcode $errorcode returned!
(Errormessage of \`file\` is suppressed)"$off
        exit 2
    fi
fi

exit 0
Cheers!
Yours Rava

Bogomips
Full of knowledge
Full of knowledge
Posts: 2564
Joined: 25 Jun 2014, 15:21
Distribution: 3.2.2 Cinnamon & KDE5
Location: London

Re: Sourcing from Konsole Subshell results in Syntax Errors

Post#7 by Bogomips » 02 May 2015, 14:08

Running in VT no different from running in Konsole tab:

Code: Select all

Last login: Sat May  2 15:10:13 +0100 2015 on /dev/tty3.
guest@porteus:~$ ver usm
-bash: ver: command not found
guest@porteus:~$ sh
guest@porteus:~$ ls *.sh
pakfuncs.sh
guest@porteus:~$ source pakfuncs.sh
sh: source: pakfuncs.sh: file not found
guest@porteus:~$ source ./pakfuncs.sh
sh: ./pakfuncs.sh: line 231: syntax error near unexpected token `<'
sh: ./pakfuncs.sh: line 231: `  readarray -t vec < <(cat $1.pak)'
guest@porteus:~$ echo $$
2244
guest@porteus:~$ exit
guest@porteus:~$ echo $$
2230
guest@porteus:~$ source pakfuncs.sh
guest@porteus:~$ ver usm
3.1.7-3
Now for script fw:

Code: Select all

guest@porteus:~$ fw kate
/usr/bin/kate: ELF 32-bit LSB  executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), stripped
guest@porteus:~$ sh
sh-4.2$ fw kate
/usr/bin/kate: ELF 32-bit LSB  executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), stripped
sh-4.2$ exit
guest@porteus:~$ fw vlc
/usr/bin/vlc: ELF 32-bit LSB  executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), stripped
Linux porteus 4.4.0-porteus #3 SMP PREEMPT Sat Jan 23 07:01:55 UTC 2016 i686 AMD Sempron(tm) 140 Processor AuthenticAMD GNU/Linux
NVIDIA Corporation C61 [GeForce 6150SE nForce 430] (rev a2) MemTotal: 901760 kB MemFree: 66752 kB

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Re: Sourcing from Konsole Subshell results in Syntax Errors

Post#8 by Rava » 02 May 2015, 16:36

Good, fw does what it should do on your system. At times it is handy to know what is behind a program, since I can more or less easily debug a script, but with a compiled program, not so easily.
And "fw" does the search and check for the kind of file all by itself without me having to do both manually...

_____________


Anyhow, back to your issue.

Like I said, the "< <" is a bug:

Code: Select all

sh: ./pakfuncs.sh: line 231: `  readarray -t vec < <(cat $1.pak)'
Have you tried searching for that error via duckduckgo? When the script is run by others, the very same bug must have been encountered by others as well...
Cheers!
Yours Rava

Bogomips
Full of knowledge
Full of knowledge
Posts: 2564
Joined: 25 Jun 2014, 15:21
Distribution: 3.2.2 Cinnamon & KDE5
Location: London

Re: Sourcing from Konsole Subshell results in Syntax Errors

Post#9 by Bogomips » 02 May 2015, 19:49

Rava wrote: Like I said, the "< <" is a bug:

Code: Select all

sh: ./pakfuncs.sh: line 231: `  readarray -t vec < <(cat $1.pak)'
Don't see why you insist on calling it a bug, when it does the job. :good: See, all hunky dory!

Code: Select all

guest@porteus:~$ sh
sh-4.2$ grep "< <" pakfuncs.sh
  readarray -t vec < <(cat $1.pak)
  readarray -t req   < <( cat reqmts )
  readarray -t rfi   < <( cat reqmts.html )
  [[ -f ulibs ]] && readarray -t ulb   < <( cat ulibs )         # Upd:2015-03-26 18:22:51 Conditionally generated file
        [[ ! $u == $lb ]] && continue;  readarray -t fi < <( cat $1~$lb );      # Amended 2015-03-29 17:55:06
    readarray -t rps   < <( cat ${rq}_ar.rep )  # <- Repositories <- awk_distro
readarray -t any < <(awk '/class="repo"/,/<li>/ {if ($0 ~ /class="repo"/) {split($0,a,":"); split(a[1],a,">"); print a[2];}
  readarray -t rps   < <( cat ${rq}_ar.rep )
sh-4.2$ cat 1>y
To be
or not to be?
That is the question.
sh-4.2$ readarray -t a < <(cat y)
sh: syntax error near unexpected token `<'
sh-4.2$ readarray -t a <<(cat y)
sh: syntax error near unexpected token `('
sh-4.2$ readarray -t a < < (cat y)
sh: syntax error near unexpected token `<'
sh-4.2$ exit
guest@porteus:~$ readarray -t a < <(cat y)
guest@porteus:~$ for x in ${!a[*]}; do echo $x; done
0
1
2
guest@porteus:~$ for x in ${!a[*]}; do echo $x: "${a[x]}"; done
0: To be
1: or not to be?
2: That is the question.
Does exactly what I want it to do. :)

If anything it is beginning to look like a subshell bug =@
Linux porteus 4.4.0-porteus #3 SMP PREEMPT Sat Jan 23 07:01:55 UTC 2016 i686 AMD Sempron(tm) 140 Processor AuthenticAMD GNU/Linux
NVIDIA Corporation C61 [GeForce 6150SE nForce 430] (rev a2) MemTotal: 901760 kB MemFree: 66752 kB

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Re: Sourcing from Konsole Subshell results in Syntax Errors

Post#10 by Rava » 02 May 2015, 21:21

Bogomips wrote:Don't see why you insist on calling it a bug, when it does the job. [...]Does exactly what I want it to do. :)
I call it a but because it is a bug.
You forgot the line prior to the one I quoted:
sh: ./pakfuncs.sh: line 231: syntax error near unexpected token `<'
sh: ./pakfuncs.sh: line 231: ` readarray -t vec < <(cat $1.pak)'
To bash, the token "<" is an error, then, same with all reported syntax errors, the shell prints the line with the error (the one I quoted, the one I call a bug) and the line is not executed by the shell due to the syntax error, but ignored.
All lines below and above are still executed, but that one line is not.

Therefore, the line contains a bug. Every syntax error is a bug.

Just try to change it into

Code: Select all

  readarray -t vec <<(cat $1.pak)
and let's see if the very syntax error is then gone.
Cheers!
Yours Rava

Bogomips
Full of knowledge
Full of knowledge
Posts: 2564
Joined: 25 Jun 2014, 15:21
Distribution: 3.2.2 Cinnamon & KDE5
Location: London

Re: Sourcing from Konsole Subshell results in Syntax Errors

Post#11 by Bogomips » 02 May 2015, 23:49

Rava wrote:Just try to change it into

Code: Select all

  readarray -t vec <<(cat $1.pak)
and let's see if the very syntax error is then gone.
Is already to be found in previous post output :)

Code: Select all

sh-4.2$ readarray -t a <<(cat y)
sh: syntax error near unexpected token `('
Update Sun May 3 13:17:48 UTC 2015

Tried on Mint vt. Is ok. But subshell doesn't know source, but could do . ./pakfuncs.sh, and gives different error: Bad for loop variable

Did not snapshot it, as don't know yet how to prepare it, and whether it would work on Mint.
Linux porteus 4.4.0-porteus #3 SMP PREEMPT Sat Jan 23 07:01:55 UTC 2016 i686 AMD Sempron(tm) 140 Processor AuthenticAMD GNU/Linux
NVIDIA Corporation C61 [GeForce 6150SE nForce 430] (rev a2) MemTotal: 901760 kB MemFree: 66752 kB

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Re: Sourcing from Konsole Subshell results in Syntax Errors

Post#12 by Rava » 03 May 2015, 17:58

Can you somehow give me the URL to that script that I look into the whole code myself?
Cheers!
Yours Rava

Bogomips
Full of knowledge
Full of knowledge
Posts: 2564
Joined: 25 Jun 2014, 15:21
Distribution: 3.2.2 Cinnamon & KDE5
Location: London

Re: Sourcing from Konsole Subshell results in Syntax Errors

Post#13 by Bogomips » 03 May 2015, 21:02

Think there has been a bit of a misunderstanding here. What I meant was that I got the instruction from the Internet, and which I'm now using in my script. Not sure if this is the exact url, but the statement is very much the same

Code: Select all

$ read test < <(echo hello world)
$ echo $test
hello world
It's answer #14 at http://stackoverflow.com/questions/2746 ... stdin-pipe
Linux porteus 4.4.0-porteus #3 SMP PREEMPT Sat Jan 23 07:01:55 UTC 2016 i686 AMD Sempron(tm) 140 Processor AuthenticAMD GNU/Linux
NVIDIA Corporation C61 [GeForce 6150SE nForce 430] (rev a2) MemTotal: 901760 kB MemFree: 66752 kB

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Re: Sourcing from Konsole Subshell results in Syntax Errors

Post#14 by Rava » 03 May 2015, 23:51

Bogomips wrote:Think there has been a bit of a misunderstanding here. What I meant was that I got the instruction from the Internet, and which I'm now using in my script. Not sure if this is the exact url, but the statement is very much the same

Code: Select all

$ read test < <(echo hello world)
$ echo $test
hello world
It's answer #14 at http://stackoverflow.com/questions/2746 ... stdin-pipe
Ohhhhh I see, now that's something new to me... I have to find other occurences of that very syntax to fully understand it... *le dramatic sigh*

Yeah, that's simple script coding magic for you, able to surprise you with unknown tricks years after practising scripting... :) :D Never ever gets boring. Nice, eh?

Anyhow, since the script is written by you (my mistake in misunderstanding that detail) , it still would be helpful when you sent it to me for examination. (Via PM) Especially when you documented it well, meaning explained in-script most of what the lines are about, what they should accomplish.

...

I know, most coders [incl. yours truly, being guilty of documenting less that could be documented - compared to what could be done in that regard] are lazy when it comes to documentation, but the more, the better. You now get your code, but what if an bug appears months later, due to some changes in your environment, or due to changes to bash, or due to something else entirely?

Often, then, months or even years later, a programmer not really knows/recalls what all his / her lines are meant to be; then (s)he has more or less grand issues debugging the code.

...

Is bash still complaining about the syntax error you posted above?

The tricky thing, at times, with syntax errors is that, unlike one might think, at times the bug is not in the blamed line, but some other lines prior to that, but first occurs to bash as syntax error in the line bash prints to you.

That often happens when forgetting or including a " or ' [or ( as part of ) ]too much, or missing one.
Only a line or lines later, when a new, seemingly unrelated " or ' is used, bash connects both thinking they belong together, making, to bash, the later line a "syntax error", when the later line by itself is okay, and the error resulting in the missing character a line or lines above.

Hope you get what I mean by that. Sadly, I cannot think of an example. I had such bugs on a regular basis, over my years coding, but since they are bugs, I exterminate them as swoon as bash tells me about a syntax error, never tried conserving any such bug for documentation or teaching purposes...-

But even without you having documented your script, as soon as you comment at least the main basic goal of the main segments of your script, In still could be able to debug it.

[And as long as bash complains about a syntax error, there is a bug; like I tried explaining above, most often the bug really is in the line bash blames and prints out, but at times the bug itself is in other lines.

What is helpful here is a IDE or at least an editor that not only highlights codes, but also prints in colour the corresponding character to a pair of characters, like highlighting the closing ) when the cursor is at/above the starting ( .

mc's internal editor, mcedit [some info: "man mcedit"], does so. And sure, the genius IDE geany [man geany for basic info, geany is no longer by default part of Porteus, but I very much recommend it you you!] does so, too.

But both need to know what kind of code it is. Either they get that via file extension, e.g. filename.html tells a syntax highlighting editor, or any IDE, it's a html code, Same with file extension css being CSS code, js being javascript and so on.. And when it comes to a bash or sh script, the first magic line is the trick here. As in so:

Code: Select all

rava@porteus:/usr/local/bin$ file sil thumbnails.sh tzacpi 
sil:           Bourne-Again shell script, ISO-8859 text executable
thumbnails.sh: Bourne-Again shell script, ASCII text executable
tzacpi:        POSIX shell script, ASCII text executable
rava@porteus:/usr/local/bin$ head -n 1 sil thumbnails.sh tzacpi 
==> sil <==
#!/bin/bash

==> thumbnails.sh <==
#!/bin/bash

==> tzacpi <==
#!/bin/sh
See? To "file" sil and thumbnails.sh differ. Still, both are : Bourne-Again shell scripts, or for short, bash scripts. I guess you realize in what they differ even when all info you can go with is the above first line and what file tells us?

_________

I think we best should move this thread staring at a certain point to "programming" since it became mostly a matter of scripting/programming, and less about "KDE" or "Desktop help". Feel free to do the move, as you can do so same as me, or else I do the move when figured out where the thread became mainly a matter of coding and less a matter of the current forum's. Maybe the whole thread should be moved, even.
Just too tired ATM to get that perfectly right, but will do so in a few hours, unless you did move it prior me doing so, that is. :D
Cheers!
Yours Rava

Bogomips
Full of knowledge
Full of knowledge
Posts: 2564
Joined: 25 Jun 2014, 15:21
Distribution: 3.2.2 Cinnamon & KDE5
Location: London

Re: Sourcing from Konsole Subshell results in Syntax Errors

Post#15 by Bogomips » 04 May 2015, 01:11

Agree with you. It's no longer KDE. Think it's a Linux subshell bug. The vt in Mint, Ubuntu base was ok. But the moment descend into subshell, gave different error as explained above. Suppose could try Kanotix next, Knoppix based and then Agilia and Chakra.

(Using Programmers Editor Kate, which has comprehensive syntax highlighting.)
Linux porteus 4.4.0-porteus #3 SMP PREEMPT Sat Jan 23 07:01:55 UTC 2016 i686 AMD Sempron(tm) 140 Processor AuthenticAMD GNU/Linux
NVIDIA Corporation C61 [GeForce 6150SE nForce 430] (rev a2) MemTotal: 901760 kB MemFree: 66752 kB

Post Reply