|
Post by rikky on Dec 6, 2018 17:11:36 GMT 1
I wanted to make some bash commands, that perform delimited string functions as in BaCon, are called like the BaCon functions, and take BaCon like arguments as food. but there are too many, and I'm lazy. So, this program makes them. That is for the Raspberry pi, otherwise you have to compile EDIT : no more. Compiling is now automatically, just run START.sh After that, something like echo hela hola kindercola | B64ENC$ | B64DEC$ should give: hela hola kindercola And so on. Rik. Attachments:bash4dumbies.tgz (59.05 KB)
|
|
|
Post by vovchik on Dec 6, 2018 18:28:38 GMT 1
Dear Rik, Thanks - very nice. It is almost like having a Bacon interpreter together with bash. With kind regards, vovchik
|
|
|
Post by rikky on Dec 7, 2018 6:46:00 GMT 1
A BaCon interpreter? hmm, well yes, sort of. I'm sure other BaCon functions then the delimited string functions can be translated via (more or less) the same program. Jah, then you nearly have something like a BaCon interpreter, I suppose. Also, just a template maker, for when you need to look up, how it was again, to write such and so command. Just by performing the ./make_bacon_bash_command ALIGN$ Then you get your ALIGN$.bac template, for example. For clarity. These commands are meant to run on bash, NOT Bacon. So either the whole of the argument has to be single quoted over all, or you have to backslash and the like, the way you would on a normal bash commandline. So it is CHOP$ -h | TOKEN$ '6,NL$' gives : Returns a string defined in x$ where on both sides <CR>, <NL>, <TAB> and Bad example for CHOP$ -h | TOKEN$ 6, NL$ etc. also happen to work. But this is just pure luck. CHOP$ -h | TOKEN$ '7 ,"d"' gives: to be choppe But CHOP$ -h | TOKEN$ 7 ,"d"
gives: some error has occured. Did you single quote the whole ARGUMENT$?
It should be written like: CHOP$ -h | TOKEN$ 7 ,\"d\" which gives: to be choppe as it should. Also LOADing the contents of a file is possible as any one argument. echo -n d > character_d CHOP$ -h | TOKEN$ '7 , LOAD "character_d"'
gives: to be choppe
as it should. DO NOT FORGET the quotes around the filename, for it doesn't give an error, yet. I noticed. This is a bug, I need to repair. EDIT: fixed. Finally, somehow the Bacon(bash) commands add a newline at the end of the output, when using multiple pipes. echo -n \"hela hola | CHOP$ '"\""' gives: hela hola as it should. But: echo -n \"hela hola | CHOP$ '"\""' | UCASE$ | REVERSE$
gives:
ALOH ALEH
with 2 empty lines before it. I cannot program this out, for then there would be no newline, if there is only one command to perform. Unless someone has a sugestion? The solution for this is: echo -n \"hela hola | CHOP$ '"\""' | UCASE$ | REVERSE$ | CHOP$ gives : ALOH ALEH as it should. Rik.
|
|
|
Post by rikky on Dec 7, 2018 6:53:01 GMT 1
We can now teach them bash nerds, with their bulls°°t sed and awk a lesson, they will not easily forget.
|
|
|
Post by vovchik on Dec 7, 2018 12:43:53 GMT 1
Dear Rik, I have been testing and seem to have come up with a way to get those results returned. It's not pretty, because of all the escaping, but it seems to work. Thanks, again. #!/bin/bash PATH="$HOME/bin/bacon-bash/bin:$PATH"
q=`MID$ "12345", 2, 3` && echo $q x=`MID$ $q, 1, 1` && echo $x DEC '"0f0f0f"' q="0f0f0f" x=`DEC \"$q\"` && echo $x HEX$ '255' HEX$ '123' REVERSE$ '"123"' REVERSE$ '"ABC"' q="DEF" x=`REVERSE$ \"$q\"` && echo $x ROTATE$ '"Rome Amsterdam Kiev Bern Paris London", 2' q="Rome Amsterdam Kiev Bern Paris London" x=`ROTATE$ \"$q\", 2` && echo $x REPLACE$ '"quote", "t", "x"' q="quote" x=`REPLACE$ \"$q\", \"t\", \"x\"` && echo $x INBETWEEN$ '"<time>", "<", ">"' q="<TIMES>" x=`INBETWEEN$ \"$q\", \"\<\", \"\>\"` && echo $x don="I have nothing to do with Russia. I have no business in Russia. I have no deals in Russia. No loans with Russia. I have no relationship to Russia. I have no investments in Russia. I can’t spell Russia. I don’t know where it is on a map. I don’t even eat Russian dressing." echo "$don" echo z=`ALIGN$ \"$don\",40,2,10` && echo "$z" With kind regards, vovchik Attachments:test01.tar.gz (230 B)
|
|
|
Post by rikky on Dec 7, 2018 14:00:03 GMT 1
Dear Vovchick, At first I thought, that you had some virus in your computer, inserted there by the CIA,FBI,MI6, which was triggered by the overusing of the word Russia. You really should be more careful. People have been send to the labour camps in Alaska for less. But when I put some PRINT ARGUMENT$ in INBETWEEN$.bac, then I discovered That the argument$ = INBETWEEN$ "<TIMES>", <, > So, the < and the > must be quoted also. But that doesn't help either, for now bash interprets the < as something it recognizes for sending output, to someones input, or so. bash: ": No such file or directory The right notation then becomes: INBETWEEN$ \"$q\" , \"\<\" , \"\>\" arrghhh, bash is really difficult. Easiest is single quoting everything. (But not always possible ) Rik.
|
|
|
Post by juppel on Mar 26, 2019 22:02:21 GMT 1
another way to directly execute basic like bash scripts. I found in a forum for freebasic and for bacon rewritten
Save the following code in /bin/bac
#!/bin/bash bacon=/usr/local/bin/bacon fullpath=$(readlink -f "$1") tempbac=$(mktemp) cat "$fullpath" | egrep -v '^#!' > "$tempbac.bac" "$bacon" -ys "$tempbac.bac" > /dev/null shift "$tempbac" "$@" rm -f "$tempbac.bac" "$tempbac"
then you can run a script without compiling it manually. The code looks like this:
#!/bin/bac PRINT "Hello World!!!"
|
|
|
Post by rikky on Mar 27, 2019 8:51:38 GMT 1
Nice one. It works This will come in quite handy. Thanks. Rik.
|
|
|
Post by vovchik on Mar 27, 2019 12:14:50 GMT 1
Dear juppel, I agree with Rik. It is very clever and useful. Thanks. I have attached a test that produces a nice m3u playlist of channels available on usatv247.com. You have to run the script perhaps twice a day because the authorization tokens change rather frequently. You can play the channels using smplayer or vlc. Again, many thanks. With kind regards, vovchik PS. I added the original bash scripts that served as my templates for the bac versoin.
|
|
|
Post by rikky on Jul 15, 2019 17:04:58 GMT 1
I changed it a little bit. Now the errors, if any, will be printed to stdout, like a normal bash script does, And you can use stdin as well, for your bacon script. I putted it under /bin/basic #!/bin/bash
bacon=/usr/bin/bacon fullpath=$(readlink -f "$1") tempbac=$(mktemp)
if test "$fullpath" = ""; then while read LINE; do echo ${LINE} >> $tempbac.bac done else echo > "$tempbac.bac" cat "$fullpath" | egrep -v '^#!' >> "$tempbac.bac" fi
foo=$("$bacon" -ys "$tempbac.bac" > >(tee -a $tempbac.stdout.log) 2> >(tee -a $tempbac.stderr.log >&2))
retval=$? if test "$retval" != "0"; then 1>&2 echo 1>&2 echo Command $@ exited with status $retval 1>&2 echo rm -f "$tempbac"* rm -f "/tmp/Makefile.bacon" exit $retval; fi
if test -s $tempbac.stderr.log then # example : unknown escape sequence: '\d' 1>&2 echo bacon spitted out something to stderr, but exited with a zero status. 1>&2 echo rm -f "$tempbac"* rm -f "/tmp/Makefile.bacon" exit 1 fi shift
"$tempbac" "$@" rm -f "$tempbac"* rm -f "/tmp/Makefile.bacon"
And now I've got what I wanted a few months ago basic PRINT "hallo" cntrl D hallo OR foo="deze hier was vroeger hardcore punk en anarchist, maar nu is hij getrouwd, hij heeft een baantje en hij vist."
basic <<EOF PRINT CHOP\$(LEFT\$(INBETWEEN\$("$foo","vroeger","maar"),LEN(INBETWEEN\$("$foo","vroeger","maar"))-2)) EOF
hardcore punk en anarchist
As you can see, you still have to backslash the $, for else bash will interpret this, and the variable $foo from bash, you have to put between doublequotes, otherwise bacon will break its neck over this. A baconvariable works without doublequotes however,, neither do you need to backslash the $, as far as I can see. basic <<EOF PRINT ARGUMENT$ EOF /tmp/tmp.x8T8iyhbZy
I haven't found a method for using standard input AND arguments yet however. But sofar this is like unbelevable to me. I'm verry happy Rik. Editted: print the error to standard error of course. Editted: an extra 'echo > "$tempbac.bac"' so that the LINENO corresponds with the real linenumber Editted: extra test for compile error, with exit status 0, and removing /tmp/Makefile.bacon
|
|
|
Post by juppel on Jul 15, 2019 18:08:28 GMT 1
Hello rikky, it works. Thanks for that!
juppel
|
|
|
Post by rikky on Mar 14, 2020 18:22:03 GMT 1
Changed it a little bit again. Called it /bin/BACON What it does is basically the same as before. Only now it is written in BaCon. And you can add the bacon options on the commandline. Did not test it thoroughly though, but it should work. added a --help added an editor, which currently does only the backspace (as did the bash script) advantages: -making an editor in bash is beyond my skills, to put it nicely. -something like bish could probably be integrated. OPTION EXPLICIT TRUE
GLOBAL basename$ = BASENAME$(TOKEN$(ARGUMENT$,1)) GLOBAL BACON_version$ = "0.1" GLOBAL debug = 0 GLOBAL stndin GLOBAL y_pos = 1 GLOBAL x_pos = 1 GLOBAL program$ = "" DECLARE y_pos$ ASSOC STRING GLOBAL irritating_bacon_options$ = "-c -l -o -i -d -b -r -w" GLOBAL option$ GLOBAL response$ GLOBAL argument$ GLOBAL retval GLOBAL irritating_option_found = 0 GLOBAL character = 0 GLOBAL var GLOBAL bacon$ = "/usr/bin/bacon" GLOBAL fullpath$ GLOBAL tempbac$ = CHOP$(EXEC$("mktemp")) GLOBAL file_contents$ = "" GLOBAL pos GLOBAL verbose = 0 GLOBAL dryrun = 0
response$ = EXEC$(bacon$ & " -v") retval = RETVAL IF retval THEN EPRINT "bacon is probably not installed (on the right place)." EPRINT bacon$ & " -v exited with code " & STR$(retval) EPRINT response$ END retval ENDIF
argument$ = DEL$(ARGUMENT$,1)
SUB HELP 'bacon -h is not up to date unfortunately 'missing options -b -r -w PRINT PRINT CHOP$(REPLACE$(EXEC$(bacon$ & " -h"),"USAGE: bacon [options] program[.bac]","USAGE: " & \ basename$ & " [options] program[.bac]" & NL$ & "Compiles and runs a bacon program.")) PRINT " --verbose" & SPC$(6) & "prints the program before compling." PRINT " --dryrun" & SPC$(7) & "prints the program, but doesn't compile neither run." PRINT PRINT "Usage:" PRINT PRINT " " & basename$ & " [options] program[.bac]" PRINT PRINT " echo 'programtext' | " & basename$ & " [options]" PRINT PRINT " " & basename$ PRINT " type your programtext, close with cntr_d." PRINT PRINT " As an executable #!" & TOKEN$(ARGUMENT$,1) & " file" PRINT PRINT " " & basename$ & " <<EOF" PRINT " type your programtext." PRINT " EOF" PRINT PRINT "examples:" PRINT PRINT " " & basename$ & " -l some_lib program.bac" PRINT PRINT " foo=monkey" PRINT " echo 'PRINT " & CHR$(34) & "the '" & CHR$(34) & "$foo" & CHR$(34) & \ "' climbs in a tree." & CHR$(34) & "'" & " | " & basename$ PRINT PRINT " " & basename$ & " --dryrun <<EOF" PRINT " A" & CHR$(92) & "$ = " & CHR$(34) & "The cat meows" & CHR$(34) PRINT " B" & CHR$(92) & "$ = REPLACE" & CHR$(92) & "$(REPLACE" & CHR$(92) & \ "$(A$," & CHR$(34) & "cat" & CHR$(34) & "," & CHR$(34) & "dog" & \ CHR$(34) & ")," & CHR$(34) & "meows" & CHR$(34) & "," & CHR$(34) & \ "barks" & CHR$(34) & ")" PRINT " PRINT B" & CHR$(92) & "$" PRINT "EOF" PRINT END 0 END SUB
SUB SELECT_ARGUMENT
LOCAL i FOR i = 1 TO AMOUNT(argument$) option$ = TOKEN$(argument$,i) SELECT option$ CASE "-v"; CASE "--version" PRINT PRINT basename$ & " version : " & BACON_version$ & " using :" PRINT CHOP$(EXEC$(bacon$ & " -v")) PRINT END 0 'BaCon doesn't have long options, so we are free to chose. '--debug is in temporary use to get keycodes. CASE "--debug" debug = 1 argument$ = DEL$(argument$,i) 'argument$ is now 1 item shorter. i = i - 1 CASE "--verbose" verbose = 1 argument$ = DEL$(argument$,i) 'argument$ is now 1 item shorter. i = i - 1 CASE "--dryrun" verbose = 1 dryrun = 1 argument$ = DEL$(argument$,i) 'argument$ is now 1 item shorter. i = i - 1 CASE "--help"; CASE "-h" HELP END 0 'CASE "-e" 'do something with the -e 'blabla DEFAULT 'remove the -y and -s, we'll add it later on. 'somehow I've understood that bacon allows combined options like -ys 'that makes it complicated IF LEFT$(option$,1) = "-" THEN IF option$ = "-y" THEN argument$ = DEL$(argument$,i) 'argument$ is now 1 item shorter. i = i - 1 ENDIF IF option$ = "-s" THEN argument$ = DEL$(argument$,i) 'argument$ is now 1 item shorter. i = i - 1 ENDIF IF INSTR(option$,"y") THEN pos = INSTR(option$,"y") option$ = LEFT$(option$,pos-1) & RIGHT$(option$,LEN(option$)-pos) argument$ = CHANGE$(argument$, i, option$) ENDIF IF INSTR(option$,"s") THEN pos = INSTR(option$,"s") option$ = LEFT$(option$,pos-1) & RIGHT$(option$,LEN(option$)-pos) argument$ = CHANGE$(argument$, i, option$) ENDIF ENDIF END SELECT NEXT i END SUB
FUNCTION GET_STDIN() stndin = WAIT(STDIN_FILENO, 50) RETURN stndin END FUNCTION
FUNCTION editor$() /* key codes for the editor: # 4 : Cntr_d # 127 : Backspace # 10 : enter # 27 : Esc # 91 50 126 : insert # 27 91 51 126 : delete # 27 79 72 : home # 27 79 70 : end # 27 91 65 : up # 27 91 68 : left # 27 91 67 : right # 27 91 66 : down # 27 91 53 126 : PgUp # 27 91 54 126 : PgDn # 27 79 80 : F1 # 27 79 81 : F2 # 27 79 82 : F3 # 27 79 83 : F4 # 27 91 49 53 126 : F6 # 27 91 49 56 126 : F7 # 27 91 49 57 126 : F8 # 27 91 50 48 126 : F9 # F10 = special key for lxterminal # 27 91 50 51 126 : F11 # 27 91 50 52 126 : F12 */
LOCAL text$ LOCAL i WHILE TRUE
stndin = GET_STDIN() IF stndin = 0 THEN CONTINUE IF stndin = 4 THEN BREAK IF stndin = 10 THEN INCR y_pos x_pos = 1 ENDIF IF stndin = 127 THEN : ' backspace IF LEN(y_pos$(STR$(x_pos))) > 0 THEN y_pos$(STR$(x_pos)) = LEFT$(y_pos$(STR$(x_pos)),LEN(y_pos$(STR$(x_pos)))-1) CURSOR BACK PRINT " "; CURSOR BACK CONTINUE ENDIF ENDIF y_pos$(STR$(x_pos)) = y_pos$(STR$(x_pos)) & CHR$(stndin) PRINT CHR$(stndin); WEND
FOR i = 1 TO NRKEYS(y_pos$) text$ = text$ & y_pos$(STR$(i)) & NL$ NEXT i
'skip the last NL$ text$ = LEFT$(text$,LEN(text$)-1)
RETURN text$ END FUNCTION
SELECT_ARGUMENT
IF debug THEN WHILE TRUE var = GET_STDIN() IF var = 0 THEN CONTINUE IF var = 4 THEN END 0 PRINT CHR$(stndin) & " = " & STR$(stndin) WEND END 0 ENDIF
IF AMOUNT(argument$) = 0 THEN fullpath$ = "" ELSE 'check if (last - 1) option needs an extra argument 'if so, the last argument is NOT a bacon filename 'what to do with option -e ??? FOR option$ IN irritating_bacon_options$ IF TOKEN$(argument$,AMOUNT(argument$)-1) = option$ THEN fullpath$ = "" irritating_option_found = 1 BREAK ENDIF NEXT option$ 'if not, then the programname is the last argument. IF NOT(irritating_option_found) THEN fullpath$ = TOKEN$(argument$,AMOUNT(argument$)) argument$ = DEL$(argument$,1) argument$ = DEL$(argument$,AMOUNT(argument$)) ENDIF 'If the program is a #!/bin/BACON program, the options are quoted, 'rid them. argument$ = CHOP$(argument$,CHR$(34)) ENDIF
IF fullpath$ = "" THEN
'test if there is input on the pipe WHILE TRUE character = GET_STDIN() IF character <> 0 THEN program$ = program$ & CHR$(character) ELSE BREAK ENDIF WEND IF program$ = "" THEN program$ = editor$() IF verbose THEN PRINT PRINT CHOP$(program$) PRINT END IF IF NOT(dryrun) THEN SAVE program$ TO tempbac$ & ".bac" END IF ELSE IF RIGHT$(fullpath$,4) <> ".bac" THEN fullpath$ = fullpath$ & ".bac" ENDIF IF NOT(FILEEXISTS(fullpath$)) THEN EPRINT fullpath$ & " not found." END 1 END IF 'check if the program is a #!/bin/BACON file_contents$ = LOAD$(fullpath$) IF LEFT$(TOKEN$(TOKEN$(file_contents$,1,NL$),1),2) = "#!" THEN file_contents$ = DEL$(file_contents$,1,NL$) ENDIF IF verbose THEN PRINT PRINT CHOP$(file_contents$) PRINT END IF IF NOT(dryrun) THEN SAVE file_contents$ TO tempbac$ & ".bac" ENDIF
IF NOT(dryrun) THEN response$ = EXEC$(bacon$ & " -y -s " & argument$ & tempbac$ & ".bac") retval = RETVAL IF retval THEN EPRINT "Program exited with status " & STR$(retval) EPRINT response$ END retval ENDIF
SYSTEM tempbac$ SYSTEM "rm -f " & CHR$(34) & tempbac$ & "*" & CHR$(34) SYSTEM "rm -f " & CHR$(34) & "/tmp/Makefile.bacon" & CHR$(34) END IF
Edit 2020/03/15 06:54:14: - added a --verbose option, that prints the program before compiling, and some other minor changes. Edit 2020/03/15 07:31:17: - added a --dryrun option, that prints the program, but doen't copile neither run, and a minor bug fix in the verbose part. Rik.
|
|
|
Post by vovchik on Mar 14, 2020 21:47:11 GMT 1
Dir Rik, Thanks for the update. I will have to study it... We are accumulating a little arsenal of bacon tools. With kind regards, vovchik
|
|
|
Post by rikky on May 17, 2020 15:30:30 GMT 1
Included a 'real' editor with backspace delete and arrows. Also the UTF8 characters on my keyboard are included like µ ù è and so on. keys like esc, F1 etc are already waiting to shuff your own routines into. I would like, if anybody has strange keys on the keyboard that are not on mine, to have them incorporated also. The wishlist includes syntax highlighting, but with my tempo, do not expect this in the following week, or so, sorry. resuming again what it does: - you can just type BACON (baconoptions) and then the editor will start, and after pressing CNTRL_D the program compiles and runs handy for quickly testing something on the commandline. - BACON (baconoptions) filename will load and compile and run a bacon program, nothing spectacular. - echo some programcode trough a pipe | BACON (baconoptions) This is just a gimmick, I do not see a useful use for this. But it works, and 'real' linux programs do this for a living, like sed and awk, so it is included. and last but not least: - you can make a #!/bin/BACON (baconoptions) script Considering the bacon options, I only tested with -z (lowercase possible), for the other options are to difficult like ldflags, and what compiler to use. I should have tested -d <tmpdir>, but I forgot, so it will probably fail. I will test this soon, and update somewhere in the following weeks, NOT days, ;°) Oh ja, I tested the -e option, and it fails miserably. It gives a clean screen, without the program, and the internal editor doesn't work. I have no idea why, but I want Peter to concentrate on his GUI thing, instead of waisting time on trivialities that nobody care about. example: echo 'print "hallo"' | BACON -z other example: #!/bin/BACON the whole radio-motif.bac program from a few fossils ago unbelievable, but it works. (presuming your BACON program is in the bin directory.) Rik. EDIT: The EDITOR.bac is an include file for BACON.bac, so it does nothing as a stand alone. EDIT: I forgot to mention : BACON (baconoptions) <<EOF type your programtext. EOF Attachments:BACON.tgz (8.09 KB)
|
|
|
Post by rikky on May 29, 2020 12:48:54 GMT 1
Well Bigbass suggested to use nano as an editor, and that proved indeed possible. So I've tossed the editor out, and am now using nano. For I wasn't able to beat the nano professionals. I think it's finished now. Rik. Attachments:BACON.tgz (8.14 KB)
|
|